Changing language with React Native and django-modeltranslation - django

I would like to add option to mobile app to change language. Im using django on backend and RN as mobile. So I installed django-modeltranslation and added my model I want to translate. 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 (i.e GameTask with field title, description etc). 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). When I change language in settings.py, values on mobile are changing too (so working as intended). So im not storing any translated text in app files, just in database (except of static errors and informations). 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.
First, use a pre-built context api or create your own to support changing language in-app. Something like this: 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.
Change the texts in the react-native side based on the selected language.
When you're making a api call, send the selected language too. Get the selected language on api side and return appropriate texts based on language.
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. When you're making api requests with react-native, include an additional header or post data which includes selected language key. And you can get and use that key in your django back-end.

Related

Convert webpage in other language?

I have created one django appEngine website in english language i want to convert that site in other language like hindi gujrati and much more language and give just one button on top of that converet in gujrati, hindi and any other language and hole site will be convert in to that perticular language.
Like in google crome that provide that facility to convert site in any language.
You can do it in two ways -
Use Django's own i18n and l10n feature. Provide your own translations and urls to use them.
Use Google's translation toolbar, that shows an option like you mentioned happens in Google chrome. It'll show a toolbar with translate button with a language option and using it will translate whatever text you have on the page.

Implementation of Multiple language based on url specific

I am using language translation in my project.But it only translate the strings if I set the highest priority to respective language in my browser.
e.g.
If i am using two language en and es for translation then it will display data in spanish if I have set spanish as my default language in my browser.
My translation is working because it only displayed that data in spanish which i have specified in my Django.po file.
So What i want is if i specify the language in url as www.asd.com/?lang=es then it must display the data in spanish.
So please suggest me what i have to do for that.
I think you are looking django-localeurl.
A Django application that allow you to specify the language of a page in the URL.
For Installation click here.

default languages for items and folders in sitecore

I'm working in a multi-site, multi-lingual sitecore project, and from time to time, someone adds lots of content on certain sub-site, fight hours with the publishing and being unable to see the updated content, and eventually realise after a couple of hours that all the input was in the wrong language for the sub-site.
So i'm wondering if there is a way in sitecore to support the following:
a folder (for a sub-site) that supports that after clicking an existing item below it, the language automatically changes to the default language for the site folder
since the language is automatically changed, new items created below that folder will be created in the default sub-site language, unless the user explicitly changes it to something else
If the above is impossible or not supported, an excellent workaround would be to be able to change the language of an item language version (say, i created an english version for a page on a spanish site with all the content in spanish, so i want to tell sitecore that i want to make this version actually the spanish version, without going into copying fields manually in the translate ui
It sounds like you might want to consider a few things:
The Language Fallback Provider - this allows you to have have content fallback from one language to another if it doesn't exist.
A custom extension (maybe an event handler for item:creating or item:created) to Sitecore to switch the context language based on where an editor is creating an item.
Just ideas right now...
An alternative approach would be to work with language read and language write security settings for your content contributors.
If your contributor for your Spanish site should only be adding items in Spanish then configure their access settings so that they aren't allowed to create content in other languages.
If you have a lot of content that has been entered in the wrong language then you can use the Globalization - Export language tool (from the Start menu) to export it to an XML file, manually replace the language tags (this is as simple as a search and replace on "en>" -> "es-ES>" for example) and then reimport it in the new language using the Globalization - Import language tool. Warning though: this WILL overwrite any existing versions.

Disabling localization for specific apps in Django

Is there a way to disable localization for specific apps in settings for Django?
There are two possible reasons for this:
Switch off localization in admin apps eg contrib/admin because admins would prefer to use English rather than the local language.
Some app default translations are really bad and confusing, and we would like to keep them off during development until we get to doing our own proper translation.
I know we can just delete the translation files or other hacks maybe, but having this in settings is more convenient when we're doing frequent upgrades.
Regards.
There is no such setting to disable translation for specific apps.
Regarding your first use-case, having admin in another language is easily achived with custom middleware, check this snippet:
http://source.mihelac.org/2009/11/12/django-set-language-for-admin/
If url scheme of 3rd party apps are simple, maybe you can use same approach to set different (default) language for them.
If you enable LocaleMiddleware, you can then set the appropriate cookie or session variable for that user. I usually implement a drop down with the available languages for the system. User selects the language, and based on that I set the session or cookie for that user. This allows the admin to remain in English while the other areas are translated.
Here is the order of lookup (from the docs):
First, it looks for a django_language key in the current user's session.
Failing that, it looks for a cookie.
The name of the cookie used is set by the LANGUAGE_COOKIE_NAME setting.
(The default name is django_language.)
Failing that, it looks at the Accept-Language HTTP header. This header is sent
by your browser and tells the server which language(s) you prefer, in order by
priority. Django tries each language in the header until it finds one with
available translations.
Failing that, it uses the global LANGUAGE_CODE setting.
EDIT:
It is not possible through settings; however depending on how you are using the app - you can "force" translation of only the language you want. This can cause problems if your languages are RTL (display problems).
The solution you hinted at (deleting the actual gettext translation files), is one way, but not recommended.
Since the displaying of translated text is a function of the template engine, simply overriding the templates of those applications and disabling the translation tags should do what you want, and is more portable.

Django: django-transmeta - sorting comments

I have created an article site, where articles are published in several languages. I am using transmeta (http://code.google.com/p/django-transmeta/) to support multiple languages in one model.
Also I am using generic comments framework, to make articles commentable. I wonder what will happen if the same article will be commented in one language and then in another. Looks like all comments will be displayed on both variants....
The question actually is:
Is there a possibility to display only comments submitted with current language of the article?
I tried the approach of transmeta for translation of dynamic texts and I had the following experience:
You want another language, you need to change the database model which is generally undesirable
You need every item in both languages, which is not flexible
You have problems linking with other objects (as you point out in your question)
If you take the way of transmeta you will need two solutions:
The transmeta solution for translating fields in a model
For objects connected to a model using transmeta you will need an additional field to determine the language, say CharField with "en", "de", "ru" etc.
These were major drawbacks that made me rethink the approach and switch to another solution: django.contrib.sites. Every model that needs internationalization inherits from a SiteModel:
class SiteModel(models.Model):
site = models.ForeignKey(Site)
Every object that would need transmeta translation is connected to a site. Every connected object can determine its language from the parent object's site attribute.
I basically ran the wikipedia approach and had a Site object for every language on a subdomain (en., de., ru.). For every site I started a server instance that had a custom settings file which would set the SITE_ID and the language of the site. I used django.contrib.sites.managers.CurrentSiteManagerto display only the items in the language of the current site. I also had a manager that would give you objects of every language. I constructed a model that connects objects of the same model from different languages denoting that they are semantically the same (think languages left column on wikipedia). The sites all use the same database and share the same untranslated User model, so users can switch between languages without any problem.
Advantages:
Your database schema doesn't need to change for additional languages
You are flexible: add languages easily, have objects in one language only etc.
Works with (generic) foreign keys, they connect to an object and know what language it is. You can display the comments of an object and they will be in one language. This solves your problem.
Disadvantages:
It's a greater deal to setup: you need a django server instance for every site and some more glue code
If you need e.g an article in different languages, you need another model to connect them
You may not need the django Site model and could implement something that does the same without the need of multiple django server instances.
I don't know what you are trying to build and what I described might not fit to your case, but it worked out perfectly for my project (internationalized community platform built upon pinax: http://www.bpmn-community.org/ ). So if you disclose some more about your project, I might be able to advise an approach.
To finally answer your question: No, the generic comments will not work out of the box with transmeta. As you realised you will have to display comments in both languages for the article that is displayed in one language. Or you will have to hack into the comments and change the model and do other dirty stuff (not recommended). The approach I described works with comments and any other pluggable app.
To answer your questions:
Two Django instances can share one database, no problem there.
If you don't want two Django instances, but one, you will have to do the following: A middleware checks the incoming request, extracts desired language from URL (en.example.com or example.com/en/ etc.) and saves the language preference in the request object. The view will have to take the request object with the language and take care of the filtering of objects accordingly. Since there is no dedicated server for the language (like in the sites approach where the language is stored in the settings.py file), you can only get the language from the request and you will have to pass attributes from the request object to Model managers to filter objects.
You could try to fake a global language state in the django application with an approach like threadlocals middleware, however I don't know if this plays out nicely with django I18N engine (which is also does some thread magic).
If you want to go big with your site in multiple languages, I recommend going for the sites-approach.