Firebase database and app localization

我怕爱的太早我们不能终老 提交于 2020-01-03 08:21:26

问题


I am building a quiz game for both iOS & Android platforms and I want to be able to handle localization.

I am using Firebase's realtime database solution in order to pull all of the question the game is going to have. I am hardcoding the questions into the Firebase's database and every question object has 2 parameters:

ID - Obviously

Text - The question text itself e.g. "Who was John Kennedy?"

I am having hard time thinking about how to localize the questions I am pulling from Firebase. Obviously if the app is localized as Spanish I want to get the questions text translated to Spanish.

How do I go about it?

Thank you very very much :)


回答1:


Database like this...

{
  "en": { "Q1": "Who was JFK?"    },
  "es": { "Q1": "¿Quién era JFK?" }
}

Accessed like this (JavaScript)...

var locale = fooLocale() || 'en'; // get locale, fallback to English
var firedb = firebase.database(); // init database
var content = firedb.ref(locale); // get reference to locale data

content.child('Q1').on('value', function (snapshot) {       // request Q1
  document.getElementById('Q1').innerHTML = snapshot.val(); // set UI text
});


来源:https://stackoverflow.com/questions/37678086/firebase-database-and-app-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!