简体   繁体   English

使用 React Native 和 django-modeltranslation 更改语言

[英]Changing language with React Native and django-modeltranslation

I would like to add option to mobile app to change language.我想向移动应用程序添加选项以更改语言。 Im using django on backend and RN as mobile.我在后端使用 django 并在移动设备上使用 RN。 So I installed django-modeltranslation and added my model I want to translate.所以我安装了django-modeltranslation并添加了我想要翻译的 model。 On mobile I display available languages.在移动设备上,我显示可用的语言。 When user clicks on specific language I would like to get translated data.当用户点击特定语言时,我想获得翻译数据。 I have huge problem to create logic how to do it.我有很大的问题来创建逻辑如何做到这一点。 I'm not asking about code just some hints and idea我不是在问代码,只是一些提示和想法

EDIT: For example: I added translation from django-modeltranslation to my model (ie GameTask with field title, description etc).编辑:例如:我添加了从django-modeltranslation到我的 model 的翻译(即带有字段标题、描述等的 GameTask)。 In my settings.py I have declared languages ('en','de','uk',etc) and added translations in database (for every field of GameTask, I added title(en), title(de) etc).在我的settings.py ,我声明了语言('en'、'de'、'uk'等)并在数据库中添加了翻译(对于 GameTask 的每个字段,我添加了 title(en)、title(de) 等)。 When I change language in settings.py , values on mobile are changing too (so working as intended).当我更改settings.py中的语言时,移动设备上的值也会发生变化(因此按预期工作)。 So im not storing any translated text in app files, just in database (except of static errors and informations).所以我没有在应用程序文件中存储任何翻译文本,只是在数据库中(static 错误和信息除外)。 Now I just want to send info from mobile with chosen language and activate this language on backend to return content in specific language现在我只想使用所选语言从手机发送信息并在后端激活该语言以返回特定语言的内容

If you want to completely make your application multi-language, you need two things.如果你想完全让你的应用程序多语言,你需要两件事。

  • Translation system for your app您的应用程序的翻译系统
  • Translation system for your api. api 的翻译系统。

First, use a pre-built context api or create your own to support changing language in-app.首先,使用预构建的上下文 api 或创建自己的上下文以支持在应用程序内更改语言。 Something like this: https://medium.com/@ally_20818/multi-language-text-with-react-native-react-context-b76d5677346d像这样的东西: https://medium.com/@ally_20818/multi-language-text-with-react-native-react-context-b76d5677346d

When user changes the language, store the language name or key in async-storage or some other database.当用户更改语言时,将语言名称或密钥存储在 async-storage 或其他数据库中。

Change the texts in the react-native side based on the selected language.根据选择的语言更改 react-native 侧的文本。

When you're making a api call, send the selected language too.当您拨打 api 电话时,请同时发送所选语言。 Get the selected language on api side and return appropriate texts based on language.在 api 端获取所选语言,并根据语言返回适当的文本。

UPDATE:更新:

Since you're not storing any text on react-native side, you only need to add a picker ( react-native-picker/picker is a native picker) and store the selected language key (en, de, uk etc in your case) in a database like react-native-async-storage .由于您没有在 react-native 端存储任何文本,因此您只需添加一个选择器( react-native-picker/ picker 是本地选择器)并将所选语言键(在您的情况下为 en、de、uk 等)存储在像react-native-async-storage这样的数据库。 When you're making api requests with react-native, include an additional header or post data which includes selected language key.当您使用 react-native 发出 api 请求时,请包含额外的 header 或发布包含所选语言密钥的数据。 And you can get and use that key in your django back-end.您可以在 django 后端获取并使用该密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM