繁体   English   中英

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

[英]Extracting localization strings from Settings.bundle plists

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

这是一个方便的列表 bash 脚本,它将 (i) 查找标签,(ii) 提取以下标签的内容,然后 (iii) output 该值以 ibtool 所需的 "string" = "string" 格式的文本文件.

您将输入和 output 文件名作为参数传递。

#!/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-粘贴到文本文件中。 我将我的保存为 plist2strings。 一定要给它执行权限。 例如,在终端执行:

macbook:~ foo$ chmod 755 plist2strings

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

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

如果 mysettings.strings 不存在于该文件夹中,它将创建它。 如果已经存在,它将在没有警告的情况下自动覆盖它。

希望有人觉得这很有用。 并随意使用(和滥用)您认为合适的(买者自负;-)。 如果您进行了任何有用的更改,请考虑将它们张贴在这里以供其他人欣赏。

快乐本地化!

〜扎克

我使用这个更好的 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>

这是一种旧思路。 尽管如此,多亏了 OP Zack,我已经使用了一段时间的原始脚本。

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

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

这里是:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM