Translate messages with react-i18next
The console-plugin-template plugin template demonstrates how you can translate messages with react-i18next.
-
You must have the plugin template cloned locally.
-
Optional: To test your plugin locally, run the Red Hat OpenShift Container Platform web console in a container. You can use either Docker or Podman 3.2.0 or later.
-
Prefix the name with
plugin__to avoid any naming conflicts. The plugin template uses theplugin__console-plugin-templatenamespace by default, and you must update when you rename your plugin for example,plugin__my-plugin. You can use theuseTranslationhook, for example:conster Header: React.FC = () => { const { t } = useTranslation('plugin__console-demo-plugin'); return <h1>{t('Hello, World!')}</h1>; };You must match the
i18nnamespace with the name of theConsolePluginresource. -
Set the
spec.i18n.loadTypefield based on needed behavior.plugin__console-demo-pluginExamplespec: backend: service: basePath: / name: console-demo-plugin namespace: console-demo-plugin port: 9001 type: Service displayName: OpenShift Console Demo Plugin i18n: loadType: PreloadloadType: Preload-
Loads all the plugin’s localization resources from the
i18nnamespace after the dynamic plugin during loading.
-
Use the format
%plugin__console-plugin-template~My Label%for labels inconsole-extensions.json. The console replaces the value with the message for the current language from theplugin__console-plugin-templatenamespace. For example:{ "type": "console.navigation/section", "properties": { "id": "admin-demo-section", "perspective": "admin", "name": "%plugin__console-plugin-template~Plugin Template%" } } -
Include a comment in a TypeScript file for the
i18next-parsertool to add the message fromconsole-extensions.jsonto your message catalog. For example:// t('plugin__console-demo-plugin~Demo Plugin') -
To update the JSON files in the
localesfolder of the plugin template when adding or changing a message, run the following command:$ yarn i18n