iphone - 从 Settings.bundle plist 中提取本地化字符串

标签 iphone cocoa-touch localization internationalization settings.bundle

如果您在 Settings.bundle 中构建子 Pane ,您最终会得到几个 .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 尚未存在于该文件夹中,它将创建它。如果它已经存在,它会自动覆盖它而不发出警告。

希望有人觉得这很有用。并随意使用(和滥用)您认为合适的(买者自负;-)。如果您做出任何有用的更改,请考虑将其发布回此处以供其他人也享受。

本地化愉快!

~扎克

最佳答案

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

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

关于iphone - 从 Settings.bundle plist 中提取本地化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007342/

相关文章:

java - 我应该使用 Unicode 符号的哪个子集来标记文本中的特殊子字符串?

iphone - XCode/iOS 中特定于区域设置的默认设置

iphone - 互斥锁上的阻塞是否相当于空 while 循环?

iphone - 如何以用户位置为中心设置 map 矩形?

iphone - NSFetchedResultsController objectAtIndex 的内存泄漏

iphone - 如何检查变量的 NSAssert 是否为 (null)?

xcode - 如何在Xcode 4.4中添加本地化语言?

iphone - 何时更改 UITableViewController 的数据源合适

objective-c - 如何在 tableView :viewForFooterInSection 中添加 UIButton 或 UISwitch

objective-c - 为 UIImagePickerController 裁剪设置最小缩放级别