【问题标题】:Jupyterlab i18next: How to extract translatable strings from JupyterLab notebooks?Jupyterlab i18next:如何从 JupyterLab 笔记本中提取可翻译的字符串?
【发布时间】:2022-01-26 23:25:36
【问题描述】:

我想以多种语言(de-de、en-us)提供我的 JupyterLab 笔记本。

为了这样做,我标记了一些字符串

import gettext

domain = 'my_application_name'
localedir = '.'
translate = gettext.translation(domain, localedir, fallback=True)
_ = translate.gettext # using _ as name for the translation function is kind of standard in python
                      # do not confuse with private markers or underscore library
                      # https://github.com/serkanyersen/underscore.py

print(_('Hello World'))

print(_('another translation key'))

然后我下载了 xgettext.exe for windows 从 https://github.com/mlocati/gettext-iconv-windows/releases/download/v0.21-v1.16/gettext0.21-iconv1.16-static-64.zip

并尝试使用以下控制台命令提取字符串:

xgettext.exe my_notebook.ipynb

我收到了警告

xgettext.exe: warning: file 'my_notebook.ipynb' extension 'ipynb' is unknown; will try C

并且没有生成输出文件。

=> 从 JupyterLab 笔记本中提取可翻译字符串的推荐方法是什么?

我更喜欢一种解决方案,在 Windows 上不需要额外的二进制文件(如 xgettext.exe)。

JupyterLab 本身是否提供一些翻译功能/扩展(不是针对 UI,而是针对笔记本)?

作为一种可能的解决方法,可以先使用 nbconvert 将笔记本转换为 python 文件,然后将其传递给 xgettext.exe。然而,这似乎太复杂了。应该有一个更优雅的解决方案。

(从 python 文件中提取可翻译字符串确实适用于 windows,例如

xgettext.exe my_python_file.py

)

翻译的粗略工作流程似乎是:

  1. 标记所有应翻译的字符串
  2. 从字符串生成翻译文件模板(="master list" 或可移植对象模板 (POT) 文件)
  3. 翻译翻译文件模板
  4. 应用翻译文件

相关:

https://github.com/jupyterlab/jupyterlab/issues/11753
https://docs.python.org/3/library/i18n.html
https://www.mattlayman.com/blog/2015/i18n/

【问题讨论】:

    标签: python internationalization jupyter-lab i18next


    【解决方案1】:

    这是已经提到的基于 nbconvertxgettext 的解决方法:

    jupyter nbconvert --to script my_notebook.ipynb --output z_temp_file_for_translation
    xgettext.exe z_temp_file_for_translation.py -o translations.pot
    del z_temp_file_for_translation.py
    

    作为xgettext的替代,可以使用python包Babel

    pip install Babel
    

    另见:http://babel.pocoo.org/en/latest/cmdline.html#extract

    jupyter nbconvert --to script my_notebook.ipynb --output z_temp_file_for_translation
    pybabel extract z_temp_file_for_translation.py -o translations.pot
    del z_temp_file_for_translation.py
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2020-12-01
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 2020-08-01
      • 2014-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多