- Library React Intl
- API
- Localization files
- Scripts
App uses React Intl library to manage support of multi languages.
Locale API located in locale.js file:
- getMessages - loads localisation for active language
- List of rtl languages - languages with right-to-left direction
- isRtlLocale - returns true if active language supports right-to-left direction
There are 2 scripts which work with localisation:
npm build:locale
- watching /locales/ folder and generates files for the app to public/_translations foldernpm mock:locales
- creates mock localisation files from en.json to ar-mock.json and en-mock.json. Good to use mock files to check if all key:value pares for English were added and that app works good with other languages too.
There are 2 places in the app with localization: /locales/ and /public/_translations/.
- /locales/ - you should work with localization files only here (creating/removing/editing files). You can use markdown there and it will be translated to HTML automatically
- /public/_translations/ - App uses these files. They will be generated automatically after you launch
npm build:locale
command.
Usually, you should launch npm build:locale
command after you started the project in the DEV mode - it works in a watch mode and will updated
localisation files in public folder every time you change something.
To add new language you should do several things:
- Create new valid JSON file with appropriate name (e.g.
ru.json
for Russian language, because ISO code of language isru
) in /locales/ folder - Add localisation data (key:value pairs)
- Add new language to the component responsible for language selection by user:
- open the file
- find array
languages
in the constructor - add new object to the array with fields:
key
- should be the same as file name without extensioncode
- language code (usually, the same askey
)name
- language name which will be displayed to the user (usually in the same language, not in English)
- Add an import of required localisation for React Intl at the top of App.js.
- Set it as a member of an array for method addLocaleData. Library will use it to localize dates, numbers, date pickers, etc.
- Launch
npm build:locale
if it has not been launched before - Check the result - if everything was done right, you would found new file in /public/_translations/ folder with the same name
- Check that you can see added language in the language dropdown in browser
To update these codes based on the values in the GBIF Registry, use:
curl -fSs https://api.gbif.org/v1/enumeration/country | jq '.[] | {("country."+.iso2): .title}' | grep country | sort
and replace the values in /locales/en.json.