-
Notifications
You must be signed in to change notification settings - Fork 261
Internationalization (i18n) in Mirador 4
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
localesdirectory. - Create a
translation.jsonfile in the newly created directory. - Translate all the keys found in an existing language's
translation.jsonfile. - Import your
translation.jsonfile in thei18n.jsfile and add it to theresourcesobject 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';import { useTranslation } from 'mirador';Mirador Core re-exports the useTranslation hook from the react-i18next for downstream consumers. See also Creating a Mirador 4 plugin
-
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.jsonfile