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

Iphone 从Settings.bundle plists提取本地化字符串,iphone,cocoa-touch,localization,internationalization,settings.bundle,Iphone,Cocoa Touch,Localization,Internationalization,Settings.bundle,如果您在Settings.bundle中构建子窗格,那么最终将得到几个.plist文件。当需要本地化项目时,您会发现创建相应的.strings文件有点乏味(我知道我是这样做的) 下面是一个方便的列表bash脚本,它将(i)查找标记,(ii)提取以下标记的内容,然后(iii)以ibtool所需的“string”=“string”格式将该值输出到文本文件 将输入和输出文件名作为参数传递 #!/bin/sh echo "// Generated by plist2strings. Manual edi

如果您在Settings.bundle中构建子窗格,那么最终将得到几个.plist文件。当需要本地化项目时,您会发现创建相应的.strings文件有点乏味(我知道我是这样做的)

下面是一个方便的列表bash脚本,它将(i)查找标记,(ii)提取以下标记的内容,然后(iii)以ibtool所需的“string”=“string”格式将该值输出到文本文件

将输入和输出文件名作为参数传递

#!/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
要从Settings.bundle目录的根目录执行脚本(如果保存到用户文件夹),只需使用以下语法:

macbook:~ foo$ ~/plist2strings mysettings.plist en.lprog/mysettings.strings
如果mysettings.strings不在该文件夹中,它将创建它。如果它已经存在,它将在没有警告的情况下自动覆盖它

希望有人觉得这个有用。并且可以随意使用(和滥用)您认为合适的(注意清空;-)。如果你做任何有用的改变,考虑把它们放回这里,让其他人也享受。

快乐本地化


~Zack

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

我使用这个更好的quickfixed版本(它只是删除重复项)

#/垃圾箱/垃圾箱
echo“//由plist2strings生成。下次运行此脚本时,将覆盖手动编辑。\n/*单个字符串文件,其标题在首选项架构中指定。字符串文件提供本地化内容,以向用户显示每个首选项。*/\n“>plist2stringstmp
sed-n'
#在这行的末尾找一个“#”
/标题$/{
#找到一个-请阅读下一行
N
#删除“#”和新行字符,
s/*\(.*\)/“\2”=“\2”/gp
}'$1>plist2stringstmp2
cat plist2stringstmp2 |排序| 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>
标题
某物
STH2
STH3

这是一个旧线程。尽管如此,多亏了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
#/垃圾箱/垃圾箱
echo“/”由plist2strings生成。下次运行此脚本时,将覆盖手动编辑。\n/*单个字符串文件,其标题在首选项架构中指定。字符串文件提供本地化内容,以向用户显示每个首选项的内容。*/\n\n/*标题元素的字符串*/“>$2
sed-n'/Title$/,/$/s/*\(.*\)/“\2”=“\2”/gp'$1>>$2
echo“\n/*标题数组元素的字符串*/”>>$2
sed-n'/Titles$/,/$/s/*\(.*\)/“\2”=“\2”/gp'$1>>$2

有一个非常类似的问题-它有一个非常好的解决方案。很高兴你发现它很有用,谢谢你的回馈!
<key>Titles</key>
            <array>
                <string>STH</string>
                <string>STH2</string>
                <string>STH3</string>
            </array>
#!/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