Ir para o conteúdo principal
Version: Canary 🚧

I18n lifecycles

Plugins use these lifecycles to load i18n-related data.

getTranslationFiles({content})

Os plugins declaram os arquivos de tradução JSON que eles querem usar.

Retorna os arquivos de tradução {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()

Exemplo:

my-plugin.js
export default function (context, options) {
return {
name: 'my-plugin',
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,
},
},
];
},
};
}

translateContent({content,translationFiles})

Traduza o conteúdo do plugin, usando os arquivos de tradução localizados.

Retorna o conteúdo do plugin localizado.

O ciclo de vida contentLoaded() será chamado com o conteúdo do plugin localizado retornado por translateContent().

Exemplo:

my-plugin.js
export default function (context, options) {
return {
name: 'my-plugin',
translateContent({content, translationFiles}) {
const myTranslationFile = translationFiles.find(
(f) => f.path === 'myTranslationFile',
);
return {
...content,
someContentLabel: myTranslationFile.someContentLabel.message,
};
},
};
}

translateThemeConfig({themeConfig,translationFiles})

Traduza os rótulos do site themeConfig, usando os arquivos de tradução localizados.

Retorna o themeConfig localizado.

Exemplo:

my-plugin.js
export default function (context, options) {
return {
name: 'my-theme',
translateThemeConfig({themeConfig, translationFiles}) {
const myTranslationFile = translationFiles.find(
(f) => f.path === 'myTranslationFile',
);
return {
...themeConfig,
someThemeConfigLabel: myTranslationFile.someThemeConfigLabel.message,
};
},
};
}

async getDefaultCodeTranslationMessages()

Temas usando a <Translate> API podem fornecer mensagens de tradução de código padrão.

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.

Exemplo:

my-plugin.js
export default function (context, options) {
return {
name: 'my-theme',
async getDefaultCodeTranslationMessages() {
return readJsonFile(`${context.i18n.currentLocale}.json`);
},
};
}