Localizing your US States array

Suppose you have a whiny co-worker who insists that all user-facing strings be localized. That’s what scripting tools are for, right? Specifically sed, in this case.

sed 's/@"[^"]*"/NSLocalizedString(&, nil)/g'

This little script will match NSStrings and replace them with a naive NSLocalizedString call. If you’re unfamiliar with sed (or even if you’re not), it matches regular expressions in an input and performs operations on them, usually substitution. In this case s is for “substitute”, @[^"]*" is the expression to match (if you’re unfamiliar with regular expressions, [^"]* means “any number of characters that are not a double quote”), and the NSLocalizedString bit is the replacement, with & meaning the previously matched string. g means apply it globally, ie, more than once. Let’s see what happens when we use it on our states array from the previous post:

$ echo '[NSArray arrayWithObjects:@"Alabama", @"Alaska", …, nil]' | sed 's/@"[^"]*"/NSLocalizedString(&, nil)/g'

[NSArray arrayWithObjects:NSLocalizedString(@"Alabama", nil), NSLocalizedString(@"Alaska", nil), NSLocalizedString(@"Arizona", nil), NSLocalizedString(@"Arkansas", nil), NSLocalizedString(@"California", nil), NSLocalizedString(@"Colorado", nil), NSLocalizedString(@"Connecticut", nil), NSLocalizedString(@"Delaware", nil), NSLocalizedString(@"Florida", nil), NSLocalizedString(@"Georgia", nil), NSLocalizedString(@"Hawaii", nil), NSLocalizedString(@"Idaho", nil), NSLocalizedString(@"Illinois", nil), NSLocalizedString(@"Indiana", nil), NSLocalizedString(@"Iowa", nil), NSLocalizedString(@"Kansas", nil), NSLocalizedString(@"Kentucky", nil), NSLocalizedString(@"Louisiana", nil), NSLocalizedString(@"Maine", nil), NSLocalizedString(@"Maryland", nil), NSLocalizedString(@"Massachusetts", nil), NSLocalizedString(@"Michigan", nil), NSLocalizedString(@"Minnesota", nil), NSLocalizedString(@"Mississippi", nil), NSLocalizedString(@"Missouri", nil), NSLocalizedString(@"Montana", nil), NSLocalizedString(@"Nebraska", nil), NSLocalizedString(@"Nevada", nil), NSLocalizedString(@"New Hampshire", nil), NSLocalizedString(@"New Jersey", nil), NSLocalizedString(@"New Mexico", nil), NSLocalizedString(@"New York", nil), NSLocalizedString(@"North Carolina", nil), NSLocalizedString(@"North Dakota", nil), NSLocalizedString(@"Ohio", nil), NSLocalizedString(@"Oklahoma", nil), NSLocalizedString(@"Oregon", nil), NSLocalizedString(@"Pennsylvania", nil), NSLocalizedString(@"Rhode Island", nil), NSLocalizedString(@"South Carolina", nil), NSLocalizedString(@"South Dakota", nil), NSLocalizedString(@"Tennessee", nil), NSLocalizedString(@"Texas", nil), NSLocalizedString(@"Utah", nil), NSLocalizedString(@"Vermont", nil), NSLocalizedString(@"Virginia", nil), NSLocalizedString(@"Washington", nil), NSLocalizedString(@"West Virginia", nil), NSLocalizedString(@"Wisconsin", nil), NSLocalizedString(@"Wyoming", nil), nil]

Like magic! Now you can tell your coworker to suck it, sed solved all your problems. As it tends to do.

About Joel Kin

Developing on Apple platforms for, holy shit, like twenty years now. Find me on linkedin and twitter. My personal website is joelk.in.
This entry was posted in Code and tagged , , , , . Bookmark the permalink.