问题
Is it possible to have a multilanguage json file for Firebase Database?
Example: I have the following in json file:
"219" : {
"AnswerA" : "Monarchy",
"AnswerB" : "Aristocracy",
"AnswerC" : "Theocracy",
"AnswerD" : "Anarchy",
"CategoryID" : "01",
"CorrectAnswer" : "Theocracy",
"IsImageQuestion" : "false",
"Question" : "What is the type of Government in Swaziland?"
},
How can i add a translation, so when user choose another language, get the question and answer in the new language?
回答1:
Firebase Multi-Language support: Is possible?
Yes, it is but with some changes in your database structure. A possible multi-language structure for your use-case might be:
Firebase-root
|
--- questions
|
--- en
| |
| --- 219
| |
| --- //English values for your properties
|
--- fr
|
--- 219
|
--- //French values for your properties
Using this structure, you can simply switch the language in the moment when the user chooses another language. The reference for an english question is:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference enQuestionIdRef = rootRef.child("questions").child("en").child("219");
While for the french version, a single simple change must be made:
DatabaseReference enQuestionIdRef = rootRef.child("questions").child("fr").child("219");
来源:https://stackoverflow.com/questions/55435343/firebase-multi-language-support-is-possible