国际化和本地化#

要使您的扩展国际化,需要完成以下任务:

注意

请仔细阅读规则,因为它们是国际化工作的强大约束。

  1. ITranslator令牌从@jupyterlab/translation包添加到您的插件依赖项中。

const extension: JupyterFrontEndPlugin<void> = {
  id: 'jupyterlab-extension',
  autoStart: true,
  requires: [ITranslator],
  activate: (app: JupyterFrontEnd, translator: ITranslator) => {}
};
  1. 从您的扩展程序翻译所在的域获取翻译包。

const trans = translator.load('my_domain');

注意

一个好的做法是仅使用字母、数字和_字符来命名您的扩展。

域名通过将-替换为_字符进行规范化。

  1. 将所有可翻译的字符串用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);

你也可以查看以下关于 spellchecker extension的拉取请求。

  1. 创建并发布您的扩展的翻译。

有两个选项:您可以将您的扩展添加到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

注意

一个示例可在server test中找到

设置翻译#

设置模式也可以翻译。可翻译的字符串是通过在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');