简体   繁体   English

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

[英]Extracting localization strings from Settings.bundle plists

If you are building child panes in your Settings.bundle, you'll end up with several.plist files.如果您在 Settings.bundle 中构建子窗格,您最终会得到几个 .plist 文件。 When it comes time to localize your project, you find the creation of the corresponding.strings file a bit tedious (I know I do).当需要本地化您的项目时,您会发现相应的 .strings 文件的创建有点乏味(我知道我这样做)。

Here's a handy list bash script which will (i) find tags, (ii) extract the contents of the following tag, and then (iii) output that value to a text file in the "string" = "string" format needed for ibtool.这是一个方便的列表 bash 脚本,它将 (i) 查找标签,(ii) 提取以下标签的内容,然后 (iii) output 该值以 ibtool 所需的 "string" = "string" 格式的文本文件.

You pass the input and output file names as parameters.您将输入和 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

Copy-N-Paste into a text file.复制-N-粘贴到文本文件中。 I saved mine as plist2strings.我将我的保存为 plist2strings。 Be sure to give it execute permissions.一定要给它执行权限。 For example, in Terminal execute:例如,在终端执行:

macbook:~ foo$ chmod 755 plist2strings

To execute the script (if saved to your user folder) from root of your Settings.bundle directory, just use the syntax:要从 Settings.bundle 目录的根目录执行脚本(如果保存到您的用户文件夹),只需使用以下语法:

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

If mysettings.strings is not already present in that folder, it will create it.如果 mysettings.strings 不存在于该文件夹中,它将创建它。 If it is already there, it will automatically overwrite it without warning.如果已经存在,它将在没有警告的情况下自动覆盖它。

Hope someone finds this useful.希望有人觉得这很有用。 And feel free to use (and abuse) as you see fit (caveat emptor;-).并随意使用(和滥用)您认为合适的(买者自负;-)。 If you make any useful changes, consider posting them back up here for others to enjoy too.如果您进行了任何有用的更改,请考虑将它们张贴在这里以供其他人欣赏。

Happy Localizing!快乐本地化!

~ Zack 〜扎克

I use this better quickfixed version (it just deletes the duplicates).我使用这个更好的 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: extract strings from title array like this: TODO:像这样从标题数组中提取字符串:

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

This is an old thread.这是一种旧思路。 Nevertheless, I've been using the original script for some time, thanks to OP Zack.尽管如此,多亏了 OP Zack,我已经使用了一段时间的原始脚本。

I decided to edit the script to extract also strings from Titles arrays as suggested by descent89.我决定编辑脚本以按照 descent89 的建议从标题 arrays 中提取字符串。

Also, by using 2addr sed syntax it is now more readable.此外,通过使用 2addr sed 语法,它现在更具可读性。 My version do not sort nor remove duplicates, because whith Titles Arrays, keeping strings order is useful.我的版本不排序也不删除重复项,因为使用标题 Arrays,保持字符串顺序很有用。

Here it is:这里是:

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

FYI, you might end up with a few duplicate strings here or there.仅供参考,您可能会在这里或那里得到一些重复的字符串。 If someone feels motivated, it might be nice to see (a) duplicate checking, and/or (b) merging the results from an existing file.如果有人感到有动力,很高兴看到 (a) 重复检查,和/或 (b) 合并现有文件的结果。

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

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