在我的“ViewController.swift”中,我有一个本地化字符串:
TheOutLabel.text = NSLocalizedString("hello", comment: "The \"hello\" word")
在终端中,为了生成“Localized.strings”文件,我输入:
cd Base.lproj/; genstrings ../*.swift; cat Localizable.strings
并得到以下结果:
??/* The \"hello\" word */
"hello" = "hello";
打字时od -c Localizable.strings
, I get:
0000000 377 376 / \0 * \0 \0 T \0 h \0 e \0 \0
0000020 \ \0 " \0 h \0 e \0 l \0 l \0 o \0 \ \0
0000040 " \0 \0 w \0 o \0 r \0 d \0 \0 * \0
0000060 / \0 \n \0 " \0 h \0 e \0 l \0 l \0 o \0
0000100 " \0 \0 = \0 \0 " \0 h \0 e \0 l \0
0000120 l \0 o \0 " \0 ; \0 \n \0 \n \0
当我打字时file Localizable.strings
, 它说:
Localizable.strings: Little-endian UTF-16 Unicode c program text
当我用“emacs”打开文件时,它不显示这些字符,当我输入M-x describe-current-coding-system RET
, 它说:
Coding system for saving this buffer:
U -- utf-16le-with-signature-unix (alias: utf-16-le-unix)
因此,文件开头的这些八进制字符 \377 和 \376 看起来像是 utf-16-le BOM,这解释了为什么每个字符后面跟着一个 \0(UTF-16 比在本例中为 UTF-8)。
这是正常/有用/有害的吗?
另外,标准的 *nix 工具(grep
, sed
, awk
)不能很好地处理 utf-16 文件:
grep '=' Localizable.strings
Binary file Localizable.strings matches
grep -a '=' Localizable.strings | sed -e 's/ = //'
"hello" = "hello";
另外,我编辑了 Localized.strings 来替换"hello";
by "Hello";
。然后“SourceTree”(我的“git”客户端)无法显示差异,除非我这样做,如建议的我可以让 git 将 UTF-16 文件识别为文本吗?:
echo '*.strings diff=localizablestrings' > .../.git/../.gitattributes
echo '[diff "localizablestrings"]' >> .../.git/config
echo ' textconv = "iconv -f utf-16 -t utf-8"' >> .../.git/config
Apple's 国际化和本地化指南 says:
注意:如果 Xcode 警告您 Localized.strings 文件似乎
如果是 Unicode (UTF-16),您可以使用以下命令将其转换为 Unicode (UTF-8):
文件检查员。
那么,我应该删除/忽略 BOM 吗?
好像没有genstrings
生成 UTF-8 文件的选项。
我应该转换文件吗?