Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CyberMew/4fc3d83e021893b85fba4d072fc08452 to your computer and use it in GitHub Desktop.
Save CyberMew/4fc3d83e021893b85fba4d072fc08452 to your computer and use it in GitHub Desktop.
Generate a basic .strings file for each .plist of an iOS Settings.bundle
#!/bin/bash
# NB: next line assumes that this script is in the root
# of your Settings.bundle directory. Feel free to adapt
# accordingly.
# Reference from https://gist.github.com/simonwhitaker/2310061
#base_dir=$(cd `dirname $0` && pwd)
base_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "base dir is $base_dir"
cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd
for plist in *.plist; do
# Guard against empty file
[ -f "$plist" ] || break
# Generate the name of the matching .strings file
outfile=en.lproj/${plist%.*}.strings
echo "Generating $outfile from $plist"
# Step 1: convert plist to formatted JSON for easy parsing
# Step 2: use Perl (old skool!) to pick out the values for the Title keys
# Step 3: convert output to UTF-16
# Debugging to see converted json output
echo "plutil -convert json -r -o - $plist" | bash > "$base_dir/$outfile.json.txt"
# Converts plist file content to json pretty format, then regex to extract and format to a bunch of strings, which is then output to a file. The regex greedy grabs until the last quotation (ignoring commas/braces that comes after), and ignores empty spaces as well.
plutil -convert json -r -o - $plist \
| perl -ne '/"Title" : ([^\w].+")/ && print "$1 = $1;\n"' \
| iconv -t UTF-16 > "$base_dir/$outfile"
#sed -i "/* 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. */" "$base_dir/$outfile"
done
read -n 1 -s -r -p "Press any key to continue"
@rolleric
Copy link

How about this:

perl -ne '/"(?:FooterText|Title)" : (".+")/ && print "$1 = $1;\n"; if (/"(ShortTitles|Titles)"/ ... /\],/) { /(".+"),?$/ && print "$1 = $1;\n"; }'

It also handles "FooterText" and "ShortTitles".

@mmysliwiec
Copy link

It's a short and concise implementation for sure. I did my own in Swift. It's much longer but generates proper comments for a translator (considers group structure as well).
genstringsforsettingsbundle.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment