Zack
Zack

Reputation: 1254

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. When it comes time to localize your project, you find the creation of the corresponding .strings file a bit tedious (I know I do).

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.

You pass the input and output file names as parameters.

#!/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. I saved mine as 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:

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

If mysettings.strings is not already present in that folder, it will create it. 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

Upvotes: 3

Views: 1278

Answers (3)

Max_B
Max_B

Reputation: 1027

This is an old thread. Nevertheless, I've been using the original script for some time, thanks to OP Zack.

I decided to edit the script to extract also strings from Titles arrays as suggested by descent89.

Also, by using 2addr sed syntax it is now more readable. My version do not sort nor remove duplicates, because whith Titles Arrays, keeping strings order is useful.

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

Upvotes: 1

descent89
descent89

Reputation: 61

I use this better quickfixed version (it just deletes the duplicates).

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

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

Upvotes: 3

Zack
Zack

Reputation: 1254

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.

Upvotes: 0

Related Questions