docusaurus/website/versioned_docs/version-2.0.0-beta.21/api/plugin-methods/i18n-lifecycles.md
Sébastien Lorber 613d938cfc
chore: prepare v2.0.0-beta.21 release (#7513)
* add beta 21 version

* v2.0.0-beta.21

* typo
2022-05-27 18:29:11 +02:00

3.2 KiB

sidebar_position
3

I18n lifecycles

Plugins use these lifecycles to load i18n-related data.

getTranslationFiles({content})

Plugins declare the JSON translation files they want to use.

Returns translation files {path: string, content: ChromeI18nJSON}:

  • path: relative to the plugin localized folder i18n/[locale]/[pluginName]. Extension .json should be omitted to remain generic.
  • content: using the Chrome i18n JSON format.

These files will be written by the write-translations CLI to the plugin i18n subfolder, and will be read in the appropriate locale before calling translateContent() and translateThemeConfig()

Example:

module.exports = function (context, options) {
  return {
    name: 'my-plugin',
    // highlight-start
    async getTranslationFiles({content}) {
      return [
        {
          path: 'sidebar-labels',
          content: {
            someSidebarLabel: {
              message: 'Some Sidebar Label',
              description: 'A label used in my plugin in the sidebar',
            },
            someLabelFromContent: content.myLabel,
          },
        },
      ];
    },
    // highlight-end
  };
};

translateContent({content,translationFiles})

Translate the plugin content, using the localized translation files.

Returns the localized plugin content.

The contentLoaded() lifecycle will be called with the localized plugin content returned by translateContent().

Example:

module.exports = function (context, options) {
  return {
    name: 'my-plugin',
    // highlight-start
    translateContent({content, translationFiles}) {
      const myTranslationFile = translationFiles.find(
        (f) => f.path === 'myTranslationFile',
      );
      return {
        ...content,
        someContentLabel: myTranslationFile.someContentLabel.message,
      };
    },
    // highlight-end
  };
};

translateThemeConfig({themeConfig,translationFiles})

Translate the site themeConfig labels, using the localized translation files.

Returns the localized themeConfig.

Example:

module.exports = function (context, options) {
  return {
    name: 'my-theme',
    // highlight-start
    translateThemeConfig({themeConfig, translationFiles}) {
      const myTranslationFile = translationFiles.find(
        (f) => f.path === 'myTranslationFile',
      );
      return {
        ...themeConfig,
        someThemeConfigLabel: myTranslationFile.someThemeConfigLabel.message,
      };
    },
    // highlight-end
  };
};

async getDefaultCodeTranslationMessages()

Themes using the <Translate> API can provide default code translation messages.

It should return messages in Record<string, string>, where keys are translation ids and values are messages (without the description) localized using the site's current locale.

Example:

module.exports = function (context, options) {
  return {
    name: 'my-theme',
    // highlight-start
    async getDefaultCodeTranslationMessages() {
      return readJsonFile(`${context.i18n.currentLocale}.json`);
    },
    // highlight-end
  };
};