从Settings.bundle plists中提取本地化字符串

时间:2009-06-17 14:20:58

标签: iphone cocoa-touch localization internationalization settings.bundle

如果要在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

将-N-Paste复制到文本文件中。我把我当作plist2strings。一定要给它执行权限。例如,在终端执行:

macbook:~ foo$ chmod 755 plist2strings

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

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

如果mysettings.strings尚未出现在该文件夹中,则会创建它。如果 已经存在,则会自动覆盖它而不会发出警告。

希望有人觉得这很有用。并随意使用(和滥用)你认为合适(警告经理;-)。如果您进行了任何有用的更改,请考虑将其重新发布到此处以供其他人使用。

快乐本地化!

~Zack

3 个答案:

答案 0 :(得分:3)

我使用这个更好的快速修复版本(它只删除重复版本)。

#!/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 :(得分:1)

这是一个老线程。尽管如此,感谢OP Zack,我已经使用原始脚本了一段时间。

我决定编辑脚本以按照descent89的建议从标题数组中提取字符串。

此外,通过使用2addr sed语法,它现在更具可读性。 我的版本不排序也不删除重复,因为标题数组保持字符串顺序是有用的。

这是:

#!/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

答案 2 :(得分:0)

仅供参考,你可能会在这里或那里找到一些重复的字符串。如果有人感到有动力,可能会很高兴看到(a)重复检查,和/或(b)合并现有文件的结果。