Languages are a sign of humanity's cultural richness, but sometimes they are a real headache for software developers.
In intelligenia, one of the platforms we use to develop web applications is Django . Unfortunately, unlike with translations of static texts, there is no standard module for translating the fields of application models in this framework .
So, after seeing some alternatives like transdb , we have chosen to develop our own application that solves this problem.
ModelTranslation is composed of a model and a series of help functions that, in some way, provide tools that allow the process of managing the translations of each of the objects to be transparent to the developer.
Functioning
First, translations are stored in a template: FieldTranslation . This model contains an object for each field and object whose model is translatable. It works as a store of information on which will be consulted in a transparent way to extract the translation that interests us.
The other tools are the ones that give the union between the translations and each one of the models that we want to translate.
The other tools are the ones that give the union between the translations and each one of the models that we want to translate.
Use
The complete information on how to use it is found in the README file of the repository (both in Spanish and English ).
Let's summarize what you have to do to use this tool:
Let's summarize what you have to do to use this tool:
- Install django-cuser and install modeltranslation (for example from the commitof the cap)
-e git: //github.com/intelligenia/modeltranslation@fbe6dff#egg=modeltranslation
- Include "cuser" and "modeltranslation" in INSTALLED_APPS insettings.py .
- IS_MONOLINGUAL = False in settings.py , indicating that we are dealing with a web that will need all the extra processing for dynamic translations.
- List the modules we want to translate , in TRANSLATABLE_MODEL_MODULES , into the settings.py file.
- Add the call to addtranslations (__ name__) at the end of the template file.
- Indicate in the Meta of each model a list of fields that we want to translatewith the name translatable_fields .
Once we have done this, we will only have to make our ModelForm inherit from TranslatableModelForm and we will already have the fields of each of the languages defined in the LANGUAGES option of the file settings.py of our project.
To get the translation of a field, ModelTranslation injects the get_trans_attr method to all models, so to get the translation into the current language of the web just do:
Event.get_trans_attr ( " name " ) {# Translate the field "name" of the event object #}
To show the translation in our templates we will only have to use a filter defined with that intention. We will include in every template that has models with translatable fields the following:
{% Load modeltranslation_tags %}
And now we can use the translation filter, which we have conveniently called _:
{{Event | _: "name"}} {# Translate the field "name" for the event object #}
Improvements
Currently, we have several ideas to improve the application, basically at the level of performance in the web server and usability.
Translation Cache
We have had no efficiency problems on our servers where this application is currently running, but we are sure to be able to improve its performance if we include an intermediate cache that avoids constant query to the FieldTranslation table for translations that we have already consulted.
New attractive translation editing interface
Currently the interface simply includes extra fields for each of the languages. We intend to make this inclusion usable and that the fields have a pleasant and modern style.
Translation aid / Automatic translation
While it is true that we can not rely on machine translation tools for all texts on our website, it is very helpful to have a preliminary version in which a client can be taught a sketch of the translation of the web.
Creating a pip package
Currently the module has to be installed from the repository, we hope to be able to develop a pip package in the future to make its installation easier and more straightforward.
License
In order to favor the use of this tool, we have released it with a very permissive license ( the MIT license ), which allows the creation of derived code of any type without any limitation.
And you, what tool do you use to manage the translations in your Django projects?
Thank you The international travelers who travel to Kenya need visas Kenya. That they can apply tourist visa for Kenya online and can get the 24*7 customer assistant.
ReplyDelete