-
Notifications
You must be signed in to change notification settings - Fork 255
Internationalization (i18n)
Mirador's user facing text is internationalized using react-18next. There are translation.json
files for each supported locale in the locales
directory.
An implementer may override text in an existing, configured language via the config. For example, the menu option text, "Download / export settings", can be replaced with the text "gimme".
translations: {
en: { downloadExportWorkspace: 'gimme' }
},
- Create a directory with the locale code for the language you're adding in the
locales
directory. - Create a
translation.json
file in the newly created directory. - Translate all the keys found in an existing language's
translation.json
file. - Import your
translation.json
file in thei18n.js
file and add it to theresources
object under the appropriate locale.
There are several different ways to internationalize a component (Hook, HOC, Render Prop, etc) that you can choose to best fit your needs and the kind of component you're working with. The react-18next documentation has detailed information on all of these scenarios, but we'll document some of our most common usage here.
- import the hook
import { useTranslation } from 'react-i18next';
- get the
{t}
const { t } = useTranslation();
- use the
{t}
<CollapsibleSection
id={`${id}-currentItem-${index}`}
label={t('currentItem', { context: `${index + 1}/${totalSize}` })}
>
This example is taken from the CanvasInfo
component; you can view the code here. All of our components with translatable text use this same pattern.
As of writing this, the pattern we're using here is fluid. It'll be best to look at what the established pattern in the code is and try to use that.
- All user facing text must be internationalized (screen-reader only text, relevant aria attributes, etc. included)
- Keys should be camelCase
- Keys should be alphabetized in the
translation.json
file