国际化和本地化#
要对您的扩展进行国际化,需要完成以下任务
注意
请仔细阅读规则,因为它们是国际化工作的强约束。
将
@jupyterlab/translation
包中的ITranslator
令牌添加到您的插件依赖项中。
const extension: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-extension',
autoStart: true,
requires: [ITranslator],
activate: (app: JupyterFrontEnd, translator: ITranslator) => {}
};
从您的扩展已翻译的域获取翻译包。
const trans = translator.load('my_domain');
注意
一个好的做法是,您的扩展名称只使用字母、数字和 _
字符。
域通过将 -
替换为 _
字符进行规范化。
使用gettext 函数之一包装所有可翻译字符串。
示例
this._trans.__('String to be translated');
this._trans.__('%1 is argument of a translated string', adjective);
this._trans._n('Singular string for %1', 'Plural string for %1', n);
您还可以查看拼写检查扩展上的以下拉取请求。
为您的扩展创建并发布翻译。
有两种选择:您可以将您的扩展添加到 JupyterLab 语言包中,或者您可以创建一个 python 包来分发您的扩展翻译(见下文)。
创建翻译 Python 包#
JupyterLab 遵循 Gettext 的翻译方法。Gettext 从源代码中提取字符串,并将其与提供的翻译一起编译(在 Python 文档中查找更多信息)。
通过使用 jupyterlab-translate,您可以提取、更新和编译您的翻译。
之后,您必须将编译后的翻译(.json、.mo)包含到您的 python 包中。这可以通过编辑这两个文件来完成。
setup.py
from setuptools import setup
setup(
# ...
entry_points={"jupyterlab.locale": ["jupyterlab_some_package = jupyterlab_some_package"]},
)
MANIFEST.in
recursive-include jupyterlab_some_package *.json
recursive-include jupyterlab_some_package *.mo
注意
服务器测试中提供了一个示例
设置翻译#
设置模式也可以翻译。可翻译字符串使用 JSON 路径上的正则表达式选择器提取。默认情况下,使用以下选择器
title
:设置标题description
:设置描述properties/.*/title
:属性标题properties/.*/description
:属性描述definitions/.*/properties/.*/title
:定义中的属性标题definitions/.*/properties/.*/description
:定义中的属性描述jupyter\.lab\.setting-icon-label
:JupyterLab 中的设置图标标签jupyter\.lab\.menus/.*/label
:JupyterLab 中的菜单标签jupyter\.lab\.toolbars/.*/label
:JupyterLab 中的工具栏项目标签
这些选择器可以使用模式中的 jupyter.lab.internationalization
键进行配置。以下示例将为 myprop
属性选择默认值
"jupyter.lab.internationalization": {
"selectors": [
"properties/myprop/default",
],
"domain": "my_jlab_extension"
}
在上面的示例中,还指定了一个定义翻译的特定域(此处为 my_jlab_extension
)。如果未指定域,则默认为 jupyterlab
。
规则#
为了从代码中提取字符串,必须遵循以下规则。
域名称通过将
-
替换为_
进行规范化翻译包变量必须是以下之一
trans
this.trans
this._trans
this.props.trans
props.trans
有效的示例
trans.__('This translatable string will be found');
this.trans.__('This translatable string will be found');
this._trans.__('This translatable string will be found');
this.props.trans.__('This translatable string will be found');
props.trans.__('This translatable string will be found');
将不起作用的示例
translator.__('This translatable string WONT be found');
__('This translatable string WONT be found');
this.__('This translatable string WONT be found');
要解决此问题,请更改您的变量以使用接受的名称
const trans = translator;
trans.__('This translatable string will be found');
字符串必须直接传递给函数;不要使用变量或常量
将不起作用的示例
const errorMessage = 'This translatable string WONT be found'
trans.__(errorMessage);
要解决此问题,请直接传递字符串
trans.__('This translatable string will be found');