在我的"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,这解释了为什么每个字符后面跟着a \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”客户端)将无法显示差异,除非我按照我能让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苹果的国际化与本土化指南说:
注意:如果Xcode警告您Localizable.strings文件似乎是Unicode (UtF-16),则可以使用“文件检查器”将其转换为Unicode (UTF-8)。
那么,我应该删除/忽略BOM吗?
似乎没有生成UTF-8文件的genstrings选项。
我应该转换文件吗?
发布于 2021-09-14 08:24:30
genstrings工具是硬编码,以输出字符串文件的编码"UTF-16LE与BOM“。我更喜欢将我的字符串文件保存在UTF-8中,并使用下面的shell脚本来生成它们:
#!/bin/zsh
function convert {
for file in $@; do
print "Converting $file to UTF-8"
iconv -f utf-16 -t utf-8 $file > temp
rm $file; mv temp $file
done
}
genstrings -o en.lproj *.m
convert en.lproj/*.stringshttps://stackoverflow.com/questions/37542388
复制相似问题