【问题标题】:How to translate language strings - zenity command-line utility如何翻译语言字符串 - zenity 命令行实用程序
【发布时间】:2014-08-30 14:48:40
【问题描述】:

我在 ubuntu 上的 shell 脚本中使用 zenity 实用程序来显示 GUI 对话框。我想知道当系统区域设置改变时如何实现语言翻译。

zenity --question --title="" --text="Hello World. How was the day today..Good?" --width="500" --height="20"

在上面的命令中,如何实现对英文文本“Hello World.How are today..Good?”的语言翻译用不同的语言。如果我将系统区域设置从英语更改为其他语言,zenity 默认附带的“是”、“否”按钮文本会自动更改,但是如何翻译我的自定义文本?

【问题讨论】:

    标签: user-interface localization locale language-translation zenity


    【解决方案1】:

    我也在寻找这个问题的答案。我在寻找答案时发现了这个问题。我正在寻找如何在 bash 脚本中使用 .po 和 .pot 文件的正确轨道。

    看起来gettext 程序和朋友都可以做到这一点。

    我发现这个很好的 blog post 解释得很好。虽然他将它用于echo,但它也可以用于 Zenity 的字符串。

    基础是这样的。要使用gettext,您需要在脚本顶部设置两个环境变量。像这样的行:

    export TEXTDOMAIN=$(basename $0) # Name of this script export TEXTDOMAINDIR==$(dirname "$(readlink -f "$0")")/locale # Location of this script 这将允许您将翻译放在脚本旁边的文件夹中

    并在您的脚本中获取gettext.sh 脚本

    source gettext.sh

    之后,您可以通过在子命令中用eval_gettext 包装它们来更改需要翻译的 Zenity 文本字符串。

    例如:

    zenity --info --title="Now opening programs!" --text="I will now open the starup programs for you. This will help you get the computer setup quicker. I will let you know when I am finished" --timeout=5

    变成:

    zenity --info --title="$(eval_gettext "Now opening programs!")" --text="$(eval_gettext "I will now open the starup programs for you. This will help you get the computer setup quicker. I will let you know when I am finished")" --timeout=5

    所以用于 Zenity 文本选项的语法,如 --title--text,如下所示:

    "$(eval_gettext "Text string")"

    "Text string" 用双引号括起来。

    这是在 eval_gettext 子命令中:$(eval_gettext "Text string") 用双引号括起来,以便 Zenity 获取从双引号中的子命令返回的字符串。 "$(eval_gettext "Text string")"

    接下来您需要使用xgettext 创建一个翻译模板文件(.pot)。

    xgettext -L Shell -o myscript.pot myscript

    生成的 .pot 文件可以提供给您的翻译人员,他们可以使用 Poedit 等程序为其语言创建 .po 文件。然后,翻译人员可以将 .po 文件发送给您,以包含在您的项目中。

    如果您使用 Poedit,在您保存时也会为您创建 .mo 文件。在上面的TEXTDOMAINDIR 中,您可以在您的脚本是以下文件夹结构的同一文件夹中创建:

    locale/<LANG>/LC_MESSAGES/

    替换为翻译的语言代码。我将要翻译的 .po 文件放在 LC_MESSAGES 文件夹中,然后用 Poedit 保存它以创建 .mo 文件。 .po 应该与上面的TEXTDOMAIN 变量加上 .mo 命名相同。如果您的脚本以 .sh 结尾,请将其包含到。 IE。对于myscript.sh,.mo 文件将为myscript.sh.mo

    如果你不使用Poedit你也可以使用msgfmt来制作.mo文件:

    msgfmt -v myscript.sh.po -o myscript.sh.mo

    要使用一种语言测试您的脚本,您可以像这样运行它。 IE。对于德语语言代码 de LANGUAGE=de ./myscript.sh

    myscript.sh、德语 (de)、.po、.mo 和文件夹的文件结构:

    • myscript.sh
    • 语言环境
      • LC_MESSAGES
        • myscript.sh.mo
        • myscript.sh.po

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多