메인 컨텐츠로 이동
버전: 3.6.3

I18n 수명 주기

플러그인은 i18n 관련 데이터를 로드하기 위해 이런 수명 주기를 사용합니다.

getTranslationFiles({content})

플러그인에서 사용할 JSON 번역 파일을 설정합니다.

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()

예:

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})

번역 파일을 사용해 플러그인 콘텐츠를 번역합니다.

로케일에 해당하는 플러그인 콘텐츠를 반환합니다.

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

예:

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})

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

Returns the localized themeConfig.

예:

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()

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.

예:

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