Localization for plugins outside the Kibana repo

To introduce localization for your plugin, use our i18n tool to create IDs and default messages. You can then extract these IDs with respective default messages into localization JSON files for Kibana to use when running your plugin.

Adding localization to your plugin

You must add a translations directory at the root of your plugin. This directory will contain the translation files that Kibana uses.

  1. .
  2. ├── translations
  3. ├── en.json
  4. ├── ja-JP.json
  5. └── zh-CN.json
  6. └── .i18nrc.json

Using Kibana i18n tooling

To simplify the localization process, Kibana provides tools for the following functions:

  • Verify all translations have translatable strings and extract default messages from templates
  • Verify translation files and integrate them into Kibana

To use Kibana i18n tooling, create a .i18nrc.json file with the following configs:

  • paths. The directory from which the i18n translation IDs are extracted.
  • exclude. The list of files to exclude while parsing paths.
  • translations. The list of translations where JSON localizations are found.
  1. {
  2. "paths": {
  3. "myPlugin": "src/ui",
  4. },
  5. "exclude": [
  6. ],
  7. "translations": [
  8. "translations/zh-CN.json",
  9. "translations/ja-JP.json"
  10. ]
  11. }

An example Kibana .i18nrc.json is {kib-repo}blob/7.9/.i18nrc.json[here].

Full documentation about i18n tooling is {kib-repo}blob/7.9/src/dev/i18n/README.md[here].

Extracting default messages

To extract the default messages from your plugin, run the following command:

  1. node scripts/i18n_extract --output-dir ./translations --include-config ../kibana-extra/myPlugin/.i18nrc.json

This outputs a en.json file inside the translations directory. To localize other languages, clone the file and translate each string.

Checking i18n messages

Checking i18n does the following:

  • Checks all existing labels for violations.
  • Takes translations from .i18nrc.json and compares them to the messages extracted and validated.

    • Checks for unused translations. If you remove a label that has a corresponding translation, you must also remove the label from the translations file.
    • Checks for incompatible translations. If you add or remove a new parameter from an existing string, you must also remove the label from the translations file.

To check your i18n translations, run the following command:

  1. node scripts/i18n_check --fix --include-config ../kibana-extra/myPlugin/.i18nrc.json

Implementing i18n in the UI

Kibana relies on several UI frameworks (ReactJS and AngularJS) and requires localization in different environments (browser and NodeJS). The internationalization engine is framework agnostic and consumable in all parts of Kibana (ReactJS, AngularJS and NodeJS).

To simplify internationalization in UI frameworks, additional abstractions are built around the I18n engine: react-intl for React and custom components for AngularJS. React-intl is built around intl-messageformat, so both React and AngularJS frameworks use the same engine and the same message syntax.

i18n for vanilla JavaScriptedit
  1. import { i18n } from '@kbn/i18n';
  2. export const HELLO_WORLD = i18n.translate('hello.wonderful.world', {
  3. defaultMessage: 'Greetings, planet Earth!',
  4. });

Full details are here.

i18n for Reactedit

To localize strings in React, use either FormattedMessage or i18n.translate.

  1. import { i18n } from '@kbn/i18n';
  2. import { FormattedMessage } from '@kbn/i18n/react';
  3. export const Component = () => {
  4. return (
  5. <div>
  6. {i18n.translate('xpack.someText', { defaultMessage: 'Some text' })}
  7. <FormattedMessage id="xpack.someOtherText" defaultMessage="Some other text">
  8. </FormattedMessage>
  9. </div>
  10. );
  11. };

Full details are here.

i18n for Angularedit

You are encouraged to use i18n.translate() by statically importing i18n from @kbn/i18n wherever possible in your Angular code. Angular wrappers use the translation service with the i18n engine under the hood.

The translation directive has the following syntax:

  1. <ANY
  2. i18n-id="{string}"
  3. i18n-default-message="{string}"
  4. [i18n-values="{object}"]
  5. [i18n-description="{string}"]
  6. ></ANY>

Full details are here.

Resources

To learn more about i18n tooling, see {kib-repo}blob/7.9/src/dev/i18n/README.md[i18n dev tooling].

To learn more about implementing i18n in the UI, use the following links:

  • {kib-repo}blob/7.9/packages/kbn-i18n/README.md[i18n plugin]
  • {kib-repo}blob/7.9/packages/kbn-i18n/GUIDELINE.md[i18n guidelines]

Most Popular