Testing localizations in an iOS playground

Viewed 1038

Is there a way to get localized strings into an iOS playground? I have a number of right-to-left language translations that include format codes that I had to edit with a hex editor (to ensure the codes were in the correct byte order), and I wish to manually examine the output of the formatted string to make sure it worked.

As an aside, are there any Mac OS text editors that allow you to display Arabic and Hebrew in left-to-right mode?

3 Answers

I was able to get this working by making an en.lproj in the Resources folder of a playground (note that my system is in English, you may have to put it in the lproj for your language to get this to work on your system).

One thing to note is that if you're using a plurals stringsDict, I always forget that you have to specify that the key is a localized string. You can't just do:

String.localizedStringWithFormat("next_step", beer)

You have to do:

String.localizedStringWithFormat(NSLocalizedString("next_step", comment: ""), beer)

Here's a gist of what I did: https://gist.github.com/designatednerd/fdfb916cc4d4ad3f33c25e917a95a2be

For all folks, that will end up here digging for answers, it's pretty easy:

let locale = Locale(identifier: "ru_RU")
let format = NSLocalizedString("localizedStringKey", comment: "")
let argument = 100 // some argument for localised string
print(String(format: format, locale: locale, argument))

Works with plurals, just put your .stringsdict in Resources subfolder.

Related