msgfmt 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

msgfmt 命令安装:


-bash/zsh: msgfmt: command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install gettext

# Debian
apt-get install gettext

# Ubuntu
apt-get install gettext

# Alpine
apk add gettext

# Arch Linux
pacman -S gettext

# Kali Linux
apt-get install gettext

# CentOS
yum install gettext

# Fedora
dnf install gettext

# OS X
brew install gettext

# Raspbian
apt-get install gettext

# Dockerfile
 dockerfile.run/msgfmt

# Docker
docker run cmd.cat/msgfmt msgfmt

msgfmt 命令补充说明:


msgfmt 命令用于从文本翻译描述生成二进制消息目录,大型项目会有成千上万的字符串要翻译,相比于基于文本的 PO 文件,使用(编译为)二进制的 MO 消息文件可能更加划算。二进制 MO 文件比对应的 PO 文件更小、读起来更快,用 msgfmt 可以生成 MO 文件。

msgfmt 命令语法:


msgfmt [OPTION] filename.po ...

msgfmt 命令选项:


Input file location:
  filename.po ...             input files
  -D, --directory=DIRECTORY   add DIRECTORY to list for input files search
If input file is -, standard input is read.

Operation mode:
  -j, --java                  Java mode: generate a Java ResourceBundle class
      --java2                 like --java, and assume Java2 (JDK 1.2 or higher)
      --csharp                C# mode: generate a .NET .dll file
      --csharp-resources      C# resources mode: generate a .NET .resources file
      --tcl                   Tcl mode: generate a tcl/msgcat .msg file
      --qt                    Qt mode: generate a Qt .qm file
      --desktop               Desktop Entry mode: generate a .desktop file
      --xml                   XML mode: generate XML file

Output file location:
  -o, --output-file=FILE      write output to specified file
      --strict                enable strict Uniforum mode
If output file is -, output is written to standard output.

Output file location in Java mode:
  -r, --resource=RESOURCE     resource name
  -l, --locale=LOCALE         locale name, either language or language_COUNTRY
      --source                produce a .java file, instead of a .class file
  -d DIRECTORY                base directory of classes directory hierarchy
The class name is determined by appending the locale name to the resource name,
separated with an underscore.  The -d option is mandatory.  The class is
written under the specified directory.

Output file location in C# mode:
  -r, --resource=RESOURCE     resource name
  -l, --locale=LOCALE         locale name, either language or language_COUNTRY
  -d DIRECTORY                base directory for locale dependent .dll files
The -l and -d options are mandatory.  The .dll file is written in a
subdirectory of the specified directory whose name depends on the locale.

Output file location in Tcl mode:
  -l, --locale=LOCALE         locale name, either language or language_COUNTRY
  -d DIRECTORY                base directory of .msg message catalogs
The -l and -d options are mandatory.  The .msg file is written in the
specified directory.

Desktop Entry mode options:
  -l, --locale=LOCALE         locale name, either language or language_COUNTRY
  -o, --output-file=FILE      write output to specified file
  --template=TEMPLATE         a .desktop file used as a template
  -d DIRECTORY                base directory of .po files
  -kWORD, --keyword=WORD      look for WORD as an additional keyword
  -k, --keyword               do not to use default keywords
The -l, -o, and --template options are mandatory.  If -D is specified, input
files are read from the directory instead of the command line arguments.

XML mode options:
  -l, --locale=LOCALE         locale name, either language or language_COUNTRY
  -L, --language=NAME         recognise the specified XML language
  -o, --output-file=FILE      write output to specified file
  --template=TEMPLATE         an XML file used as a template
  -d DIRECTORY                base directory of .po files
The -l, -o, and --template options are mandatory.  If -D is specified, input
files are read from the directory instead of the command line arguments.

Input file syntax:
  -P, --properties-input      input files are in Java .properties syntax
      --stringtable-input     input files are in NeXTstep/GNUstep .strings
                              syntax

Input file interpretation:
  -c, --check                 perform all the checks implied by
                                --check-format, --check-header, --check-domain
      --check-format          check language dependent format strings
      --check-header          verify presence and contents of the header entry
      --check-domain          check for conflicts between domain directives
                                and the --output-file option
  -C, --check-compatibility   check that GNU msgfmt behaves like X/Open msgfmt
      --check-accelerators[=CHAR]  check presence of keyboard accelerators for
                                menu items
  -f, --use-fuzzy             use fuzzy entries in output

Output details:
  -a, --alignment=NUMBER      align strings to NUMBER bytes (default: 1)
      --endianness=BYTEORDER  write out 32-bit numbers in the given byte order
                                (big or little, default depends on platform)
      --no-hash               binary file will not include the hash table

Informative output:
  -h, --help                  display this help and exit
  -V, --version               output version information and exit
      --statistics            print statistics about translations
  -v, --verbose               increase verbosity level

msgfmt 命令实例:


msgfmt 可以检查 gettext 文件的语法是否有效:

msgfmt fr.po --check

上面的命令会在 PO 模板所在的目录下创建一个名为 fr.po 的文件。

msgfmt 生成 MO 文件,如果 PO 文件有效,该命令将在 PO 文件之外创建一个 fr.mo 文件:

msgfmt fr.po --no-hash -o fr.mo

msgfmt 还原 PO 文件,应该在版本控制中保留原始 PO 文件,这样以后就可以更新翻译。如果你丢失了原始的 PO 文件,希望将 MO 文件反编译为基于文本的 PO 文件,你可以用 msgfmt 这样做:

msgunfmt fr.mo > fr.po

注意:反编译出的文件不包含注释和模糊字符串,因为它们一开始就没有编译进 MO 文件里。

msgfmt 扩展阅读:




msgfmt 命令评论