Translations¶
GeoSight allows for multi-lingual interface using built-in Django and React components.
Supported Languages¶
| Language | LANGUAGES code |
React folder | Django folder |
|---|---|---|---|
| English | en-us |
en-US |
en_US |
| Spanish | es-es |
es-ES |
es_ES |
| French | fr-fr |
fr-FR |
fr_FR |
| Portuguese (Portugal) | pt-pt |
pt-PT |
pt_PT |
| Portuguese (Brazil) | pt-br |
pt-BR |
pt_BR |
The same language is written three ways: lowercase with a hyphen in LANGUAGES and in the URL, xx-XX for the React folder, and xx_XX for the Django folder.
How a Language Is Picked Up¶
- React finds every folder in
frontend/locales/react/automatically, so there is no list to update.native.nameandnative.flagin itscommon.jsonare what the language selector shows. - Django serves every page under a language prefix (
/en-us/,/fr-fr/, ...). A language that is missing fromLANGUAGESreturns 404 as soon as a user selects it, even if its React and Django files exist. - Missing React keys fall back to English (
en-US).
Adding a New Language¶
To add a new language, do the following
- Find the language code (language and region, for example
pt-BR) from this website -
In
django_project/core/settings/project.py, add the language with its name in English and the code in all lowercase as follows: -
In
django_project/frontend/locales/react, create a new folder with its name as the new language code as follows: -
Within the new language folder, add a
common.jsonas follows: -
In the new
common.json, copy the dictionary of keys from thecommon.jsonin another already established language: -
Change the
native.nameandnative.flagto the native name of the language and the flag of the country which it is from (this is what will be displayed on the frontend language selector) -
Change all of the values in the dictionary into the new language so that your new
common.jsonlooks like this: -
In
django_project/frontend/locales/django, create a new folder with its name as the new language code but with an underscore instead of a hyphen (- → _) as follows: -
Within the new language folder, add another folder names
LC_MESSAGESas follows: -
Within the
LC_MESSAGESfolder, create a file nameddjango.poas follows: -
In the new
django.po, copy the contents from thedjango.poin another already established language:# English translations for GeoSight package. # Copyright (C) 2024 UNICEF # This file is distributed under the same license as the GeoSight package. msgid "" msgstr "" "Project-Id-Version: GeoSight\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2024-03-21 12:00+0000\n" "PO-Revision-Date: 2024-03-21 12:00+0000\n" "Last-Translator: \n" "Language-Team: English\n" "Language: en_US\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" #: frontend/views/admin/dashboard/list.py:38 msgid "Projects" msgstr "Projects" ... -
Change the
Language-TeamandLanguagelines to the language name (in English) and the folder name, respectively. Also check thatPlural-Formsmatches the language (see Generating the Django Catalog):# English translations for GeoSight package. # Copyright (C) 2024 UNICEF # This file is distributed under the same license as the GeoSight package. msgid "" msgstr "" "Project-Id-Version: GeoSight\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2024-03-21 12:00+0000\n" "PO-Revision-Date: 2024-03-21 12:00+0000\n" "Last-Translator: \n" "Language-Team: Portuguese\n" # <- CHANGED "Language: pt_BR\n" # <- CHANGED "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" #: frontend/views/admin/dashboard/list.py:38 msgid "Projects" msgstr "Projects" ... -
On every line that begins with
msgstr, change the value to the translation in the new language:# English translations for GeoSight package. # Copyright (C) 2024 UNICEF # This file is distributed under the same license as the GeoSight package. msgid "" msgstr "" "Project-Id-Version: GeoSight\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2024-03-21 12:00+0000\n" "PO-Revision-Date: 2024-03-21 12:00+0000\n" "Last-Translator: \n" "Language-Team: Portuguese\n" "Language: pt_BR\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" #: frontend/views/admin/dashboard/list.py:38 msgid "Projects" msgstr "Projetos" # <- CHANGED ... -
In the terminal, run this command inside
django_project:You should now see a new
django.mofile next to thedjango.poas follows:
Now, you should be able to see the new language available, and when you switch to it, all marked strings should be translated.
Generating the Django Catalog¶
Instead of copying a django.po from another language (steps 8–13), you can let Django create it with every current string. Run this inside django_project, using the Django folder name:
makemessages -a only updates languages that already have a folder, so use -l the first time. Then fill in every msgstr and compile as in step 14.
Check these header lines in the new file:
"Language: pt_BR\n""Plural-Forms: ...\n"must match the language, for examplenplurals=2; plural=(n != 1);for English, Spanish and Portuguese (Portugal), andnplurals=2; plural=(n > 1);for French and Portuguese (Brazil).
Fuzzy Entries¶
When a source string changes, makemessages may keep the old translation and mark it #, fuzzy. Django ignores fuzzy entries and shows the English text instead. Review each one, correct the msgstr, and delete the #, fuzzy line.
Before Opening a Pull Request¶
- Every
common.jsonhas the same keys asen-US/common.json, with no extra keys and no English copied in as a placeholder. -
Every
django.pohas no emptymsgstrand no#, fuzzyentries. This command shows the counts: -
Placeholders are unchanged:
{{pageName}}in React, and{host},{layer}and HTML tags in Django. - The
.mofiles are compiled and committed with the.pofiles. - Open
/<code>/(for example/pt-br/) and check that the language selector lists the new language.
Adding a New Translation¶
React¶
In any file that requires translation, do the following:
- Add
import { useTranslation } from 'react-i18next';to the top of the file - Within the component itself, insert
const { t } = useTranslation();(near the top of the component for style) -
For any string requiring translation, do the following:
-
If the string looks like this
-
For each
django_project/frontend/locales/react/[LANGUAGE-CODE]/common.json(where[LANGUAGE-CODE]isen-US,es-ES, etc.), add a key-value pair for you new translation, where the key is a camelCase descriptor of the string and the value is the string in the proper language -
Replace the string in the component with the dynamic dictionary entry
-
-
If the component looks like this
-
For each
django_project/frontend/locales/react/[LANGUAGE-CODE]/common.json(where[LANGUAGE-CODE]isen-US,es-ES, etc.), add a key-value pair for you new translation, where the key is a camelCase descriptor of the string and the value is the string in the proper language with any variables in double curly brackets -
Replace the string in the component with the dynamic dictionary entry
-
-
Nested keys
common.json is grouped by namespace (admin.*, dashboard.*, components.*, ...). Add new keys to the namespace they belong to and reference them with a dot, for example:
Django¶
In any file that requires translation, do the following:
- Add
from django.utils.translation import gettext as _to the top of the file -
For any string requiring translation, change the string to a key entry for the dictionary using the
_function from this……to this
-
In the terminal, run this command inside
django_project: -
For each
django_project/frontend/locales/django/[LANGUAGE-CODE]/LC_MESSAGES/django.po(where[LANGUAGE-CODE]isen_US,es_ES, etc.), add find themsgidcorresponding to the new translation key and input the proper translation in themsgstrfield below it# Spanish translations for GeoSight package. # Copyright (C) 2024 UNICEF # This file is distributed under the same license as the GeoSight package. msgid "" msgstr "" "Project-Id-Version: GeoSight\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2024-03-21 12:00+0000\n" "PO-Revision-Date: 2024-03-21 12:00+0000\n" "Last-Translator: \n" "Language-Team: Spanish\n" "Language: es_ES\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" #: frontend/views/admin/dashboard/list.py:38 msgid "Projects" msgstr "Proyectos" ... # #: frontend/views/admin/indicator/list.py:32 <- JUST APPEARED msgid "Indicators" msgstr "Indicadores" # <- CHANGED -
In the terminal, run this commands inside
django_project: