Firebase Multi-Language support: Is possible?

China☆狼群 提交于 2021-02-07 20:59:38

问题


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

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