Implementing Localization in DRF.
In an ongoing development project of DRF, I have to support the translation for multiple languages.
My question is how should I do it.
Like upgrading existing APIs with the translated response (need to alter the viwesets and serializers).
Having a separate translation APIs.
The data which is added from the CMS is how it's translation to be done, cause from the CMS the translation can be updated as well and storing so much of the data for each language in the DB doesn't sound good to me.
I know how the translation works with django and the Rosetta as well for the updatable interface.
My question is how should I approach it for the entire project.
Please guide me, if you can it would be very helpful.
Thanks.
We use default django translations module
settings.py
LANGUAGES = (
('ru', 'Russian'),
('kk', 'Kazakh'),
('en', 'English'),
)
In your project
from django.utils.translation import gettext_lazy as _
text = _('Your text that need translate')
After create folder locale in prjects root path, add text in your project and use command:
./manage.py makemessages --no-location --all
You need to fill created localization files
When deploy server or run project use:
./manage.py compilemessages
If there are a lot of texts for localization, then you can use autotranslation using the google api and modeltranslation library
# Auto translate settings:
# A workaround for the error:
# import collections
# collections.MutableSequence = collections.abc.MutableSequence
# GOOGLE_TRANSLATE_KEY = ''
# AUTOTRANSLATE_TRANSLATOR_SERVICE = 'autotranslate.services.GoogleAPITranslatorService'
# Then call:
# pip install django-autotranslate
# pip install google-api-python-client
# python manage.py translate_messages -s ru -l en
# python manage.py translate_messages -s ru -l ru
After that, when a request is sent, you need to specify Accept-Language: en in the Header
Damir
Minko
Silverhand
Maxim
Doragonsureiyā
Bhavin
Leonardo
Charly
mahshid
Nikita
Maroua
奈姆
you7
Shaikh Ahmad Raza
Otabek
Allexiusw
𝑰𝑳𝑰𝒀𝑨
Saydullo