国际化生命周期
¥I18n lifecycles
插件使用这些生命周期来加载 i18n 相关数据。
¥Plugins use these lifecycles to load i18n-related data.
getTranslationFiles({content})
插件声明它们想要使用的 JSON 翻译文件。
¥Plugins declare the JSON translation files they want to use.
返回翻译文件 {path: string, content: ChromeI18nJSON}
:
¥Returns translation files {path: string, content: ChromeI18nJSON}
:
-
path
:相对于插件本地化文件夹i18n/[locale]/[pluginName]
。应省略扩展.json
以保持通用。¥
path
: relative to the plugin localized folderi18n/[locale]/[pluginName]
. Extension.json
should be omitted to remain generic. -
content
:使用 Chrome i18n JSON 格式。¥
content
: using the Chrome i18n JSON format.
这些文件将由 write-translations
命令行接口 写入插件 i18n 子文件夹,并在调用 translateContent()
和 translateThemeConfig()
之前在适当的语言环境中读取
¥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:
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})
使用本地化翻译文件翻译插件内容。
¥Translate the plugin content, using the localized translation files.
返回本地化的插件内容。
¥Returns the localized plugin content.
将使用 translateContent()
返回的本地化插件内容来调用 contentLoaded()
生命周期。
¥The contentLoaded()
lifecycle will be called with the localized plugin content returned by translateContent()
.
示例:
¥Example:
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})
使用本地化翻译文件翻译站点 themeConfig
标签。
¥Translate the site themeConfig
labels, using the localized translation files.
返回本地化的 themeConfig
。
¥Returns the localized themeConfig
.
示例:
¥Example:
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()
使用 <Translate>
API 的主题可以提供默认的代码翻译消息。
¥Themes using the <Translate>
API can provide default code translation messages.
它应该返回 Record<string, string>
中的消息,其中键是翻译 ID,值是使用站点当前区域设置本地化的消息(没有描述)。
¥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:
export default function (context, options) {
return {
name: 'my-theme',
async getDefaultCodeTranslationMessages() {
return readJsonFile(`${context.i18n.currentLocale}.json`);
},
};
}