从Settings.bundle的plist文件中提取本地化字符串

3
如果您正在Settings.bundle中构建子窗格,则会得到多个.plist文件。在本地化项目时,创建相应的.strings文件可能会让您感到有点繁琐(我知道我是这样的)。
下面是一个方便的bash脚本列表,它将(i)查找标签,(ii)提取以下标签的内容,然后(iii)将该值输出到文本文件中,“string”=“string”格式需要ibtool。
您可以将输入和输出文件名作为参数传递。
#!/bin/sh
echo "// Generated by plist2strings. Manual edits will be overwritten the next time this script runs.\n/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */\n" > plist2stringstmp
sed -n '
# look for a "#" at the end of the line
/<key>Title<\/key>$/ {
# Found one - now read in the next line
 N
# delete the "#" and the new line character, 
 s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2"/gp
}' $1 > plist2stringstmp2
cat plist2stringstmp plist2stringstmp2 > $2
rm plist2stringstmp plist2stringstmp2

将文本复制并粘贴到一个文本文件中。我把我的保存为plist2strings。确保赋予它执行权限。例如,在终端执行:

macbook:~ foo$ chmod 755 plist2strings

如果脚本保存在您的用户文件夹中,要从Settings.bundle目录的根目录执行脚本,只需使用以下语法:

macbook:~ foo$ ~/plist2strings mysettings.plist en.lprog/mysettings.strings

如果 mysettings.strings 在该文件夹中不存在,它将被创建。 如果它已经存在,它会自动覆盖而无需警告。

希望有人觉得这个有用。 随意使用和滥用,但需注意(买家自负)。 如果您做出任何有用的更改,请考虑将它们发布在此处供其他人享受。

祝本地化愉快!

〜Zack


这里有一个非常相似的问题 - 它有一个相当不错的解决方案。https://dev59.com/b1jUa4cB1Zd3GeqPNQIn#6958482 - gamma
3个回答

3
我使用这个更好的quickfixed版本(它只删除重复项)。
#!/bin/sh
echo "// Generated by plist2strings. Manual edits will be overwritten the next time this script runs.\n/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */\n" > plist2stringstmp
sed -n '
# look for a "#" at the end of the line
/<key>Title<\/key>$/ {
# Found one - now read in the next line
 N
# delete the "#" and the new line character, 
 s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2"/gp
}' $1 > plist2stringstmp2
cat plist2stringstmp2 | sort | uniq > tmp
cat plist2stringstmp2 >> plist2stringstmp 
mv plist2stringstmp $2
rm plist2stringstmp2

TODO:从标题数组中提取字符串,如下所示:

<key>Titles</key>
            <array>
                <string>STH</string>
                <string>STH2</string>
                <string>STH3</string>
            </array>

1

这是一个旧的帖子。尽管如此,我一直在使用原始脚本,感谢楼主Zack。

我决定编辑脚本,按照descent89的建议,也从Titles数组中提取字符串。

此外,通过使用2addr sed语法,现在更易读。我的版本不会排序或删除重复项,因为对于Titles数组来说,保持字符串顺序是有用的。

这就是它:

#!/bin/sh
echo "// Generated by plist2strings. Manual edits will be overwritten the next time this script runs.\n/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */\n\n/* String for Title elements */" > $2

sed -n '/<key>Title<\/key>$/,/<\/string>$/ s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2";/gp' $1 >> $2

echo "\n/* String for Titles arrays elements */" >> $2

sed -n '/<key>Titles<\/key>$/,/<\/array>$/ s/.*<\(string\)>\(.*\)<\/\1>/"\2" = "\2";/gp' $1 >> $2

很高兴你觉得有帮助,也感谢你的贡献! - Zack

0

请注意,您可能会在某些地方得到一些重复的字符串。如果有人感到有动力,那么检查重复和/或合并现有文件中的结果可能是不错的选择。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接