【发布时间】:2016-09-29 06:29:47
【问题描述】:
在我的“ViewController.swift”中,我有一个本地化字符串:
TheOutLabel.text = NSLocalizedString("hello", comment: "The \"hello\" word")
在终端中,为了生成“Localizable.strings”文件,我输入:
cd Base.lproj/; genstrings ../*.swift; cat Localizable.strings
得到如下结果:
??/* The \"hello\" word */
"hello" = "hello";
输入od -c Localizable.strings 时,我得到:
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";
另外,我编辑了 Localizable.strings 以将 "hello"; 替换为 "Hello";。然后“SourceTree”(我的“git”客户端)无法显示差异,除非我这样做,正如Can I make git recognize a UTF-16 file as text? 中所建议的那样:
echo '*.strings diff=localizablestrings' > .../.git/../.gitattributes
echo '[diff "localizablestrings"]' >> .../.git/config
echo ' textconv = "iconv -f utf-16 -t utf-8"' >> .../.git/config
Apple 的 Internationalization and Localization Guide 说:
注意:如果 Xcode 警告您 Localizable.strings 文件似乎 是 Unicode (UtF-16),您可以使用 文件检查器。
那么,我应该删除/忽略 BOM 吗?
似乎没有 genstrings 选项来生成 UTF-8 文件。
我应该转换文件吗?
【问题讨论】:
-
是的,这是一个带有 BOM 的 UTF-16 编码文件,根据stevestreeting.com/2010/05/18/… 和其他资源,genstrings 无法更改此选项。 Xcode 可以毫无问题地处理它。如果您需要使用其他工具,则可以将其转换为 UTF-8(例如在 Xcode 的文件检查器中)。
标签: ios swift unicode character-encoding genstrings