Is there a good way to externalize user-facing strings out of objective C files? - ios

I have read a few tutorials about iOS's tools and processes for i18n/l10n, and am unpleasantly surprised with what I'm seeing. It seems there isn't a solid way of externalizing user-facing strings out of objective C files. Am I missing something?
Description of the problem:
There is a tendency to place English strings directly into the .m file, and the Apple documentation seems to encourage this. While this is also possible in Android, as least in Android there is a clear distinction between externalized and non-externalized strings. With iOS, on the other hand, code that calls for a string tends to look like this:
NSLocalizedString(#"There was an error loading the image.", nil)
In this case, "There was an error loading the image." is the key for this string resource. Therefore if I want to make another reference to the same string in some other place we have to again write code like this:
NSLocalizedString(#"There was an error loading the image.", nil)
But now I have to make sure that I spelled these two strings the same and there is no compile time check to help me confirm that. I could write a helper function called createErrorString, but that's not fun. And I could replace "There was an error loading the image." with a more sensible key like "ERROR_IMAGE_LOAD", but that does not seem to be a common practice, and Apple seems to discourage this sensible behavior. Here is what their documentation states:
"A common convention when developing applications is to use a key name
that equals the value in the language used to develop the
application."
It looks like Apple is recommending that you put the full English string in your source code. So I'll have to try to convince my colleagues to go against Apple's guidance.
Now that I've got all of these user-facing English strings (or keys) in the source code, Apple includes a tool called genstrings that parses the .m files, and spits out a Localizable.strings file that I can then send out for translation. This might work if you are only going to get your app localized one time, but in our company localization is an ongoing iterative process. Look at what the Apple documentation recommends:
"For subsequent runs, it is a good idea to save a copy of your current
strings files before running genstrings. You can then diff the new and
old versions to determine which strings were added to (or changed in)
your project. You can then use this information to update any already
localized versions of your strings files, rather than replacing those
files and localizing them again."
That doesn't seem very good. In Android and Windows8, you internationalize your source tree one time, and from that moment on your externalized strings are owned in the xml files where they belong; in iOS they are owned in source code (sort of) and then tabulated into some intermediate file (or is it?) by some crazy tool. Is the Localizable.strings file an intermediate file or should it be committed into git - we are still debating this at my company.
(And from what I can tell, this is only the beginning. In xib-land, which is where 90% of your user-facing strings live, there also seems to be an inefficient mechanism for l10n. Wil Shipley's article describes this at length.)
Does anyone have any suggestions on a good way to externalize strings in iOS? My main question concerns objective-C strings, but answers pertaining to xib files would also be much appreciated. Thanks!

I found the recommendation to name the key like the english string strange, too.
I name the keys, value e.g "Menu1SettingsTitle" = "Settings".
I dont need genstrings tool, just externalize manually.
And no, the string files is not an intermediate step, they should be in git.
However with that approach i noticed three drawbacks:
1) I detected duplicate names, but that can be moved to a common section for strings like "cancel, delete"
2) If you forget to put a string into that language file, it cannot be found and then the key is displayed, which looks very strange, of course. Otherwise with apples reccomendation, If the key is the english word, it looks "only english" but not worse.
3) The translation process is easier if english is always left, instead of "Menu1SettingsTitle". to solve that i put a comment above, but dont know if the translation service would be happy with that.

After a couple of hours searching I decided to use two different approaches: one for the storyboards and one for the text inside the .m files. The result are two files Localizable.strings for the Objective C text and the internationalized storyboard.
The update_storyboard_strings.sh can automatically extract translatable strings from storyboards and update strings files. The source code (by mikezang) can be found at:
http://forums.macrumors.com/showpost.php?p=16060008&postcount=4
For the objective C NLS I use another script around the xcode-tools by Frédéric Sagnes:
https://github.com/ndfred/xcode-tools
I had to call it for each language in order to get the desired results:
python scripts/xcode-tools/update_strings.py --import=MyProject/Base.lproj/Localizable.strings MyProject/Base.lproj/Localizable.strings
python scripts/xcode-tools/update_strings.py --import=MyProject/de.lproj/Localizable.strings MyProject/de.lproj/Localizable.strings
Now put both in one script and add it to your Xcode project as a last build phase.

Related

Storyboard localization excludes a single file in iOS

I am facing a very obscure issue.
I've got an app that needs to be localized in three Languages, English, French, German. There's multiple localization files as I've got multiple storyboards ( each storyboard has one for each language).
Everything works great and things translate properly except for ONE file in ONE storyboard in ONE language. So this specific storyboard I call "Authentication" works for all languages except for the German one.
I've checked. the file is:
included in all targets
is also found in Build Phases copy phase
has the correct name.
has the correct formatting (in fact i am not allowed to build at all if the formatting is not correct in this version of Xcode.)
I am out of ideas and resorting to asking here to see if theres anything i might be missing in my series of checks.
Thanks all
SOLVED.
The answer lies within my third point
has the correct formatting (in fact i am not allowed to build at all
if the formatting is not correct in this version of Xcode.)
Beware everyone. This might seem like it is the case but it does not apply to everything. If there's a syntactic error in the Localizable.strings file, xcode will let you know and refuse to build.
HOWEVER if there's a syntax error in the YourStoryboard.strings file, the process of building will go on UNHINDERED.
Since an issue in syntax was halting everything for Localizable.strings i was wrongfully assuming that such a check would take place for all translation files. It doesnt!
Hope this saves someone in the future

Listing missing language files

I have this project localized in several languages. Xcode shows the following list of files:
You can see that some languages have more files than others. Is there a way to list what files are missing on each language?
Yes, I know that I can use the parameter -NSShowNonLocalizedStrings YES as an argument passed on launch. I did that but that is not showing anything.
This is an indirect answer to your question; If you have that many .strings files you might want to organise them more structurally.
You could put all translatable strings in either a database or a spreadsheet and generate your source files from there. This may sound like more work but I think in the end it saves time.
See this other question about consolidating strings files. Or see my example spreadsheet to manage your localisations and spot missing texts more easily.

How to keep Storyboard strings up to date?

I have the Storyboard, Base language and translated all the app into another language, so far it's working fine.
I would like to know, when I add new elements in the Storyboard that needs to be localized, how do you keep the strings file up to date.
I tried a suggestion, which is to switch this: to Interface Builder Cocoa Storyboard and then back to Localizable Strings, however the results are not good, most of the translation is lost and switched back to the Base language.
Is there a handy tool to regenerate and merge the strings in Storyboard? I tried few scripts but they always ended up wiping everything.
I've modified a skript that i've found here on stackoverflow to be able to also handle the localized strings in storyboards.
The skript only adds new key-value pairs to the .strings file, but keeps the ones that you have already translated.
You can get it from my github, for more details about my localization workflow read my blogpost

iPhone localization at one go

Is there any other way to localize iPhone apps beside the use of Localizable.strings? Can I use Google Translate or something like that?
I have my application written in English, and I have created Localizable.strings files for about twenty languages. I have the English Localizable.strings working perfectly, and I have tried Italian as well. Is there any way to translate these Localizable.strings to the rest of the languages automatically?
I mean is there any program or something to do the job for me?
In short: No, there is no program for that. It's the same problem as translating any text: You need to understand the meaning to give useful translations.
As a quick fix, you can of course simply take the strings in your Localizable.strings file and copy them into a Spreadsheet, then run one column through Google Translate and copy the result back. Then reverse the procedure and you have a translated Localizable.strings. A regex for doing this would be:
^"(.*)"\s*=\s*"(.*)";$
that works fine in eg. TextWranger with grep mode on, then you can replace the text with
\1\t\2
to create a the tab-delimited file from a strings file.
I still suggest you invest in a native speaker of each language to double-check the translations, or your app will become a laughing stock. Google Translate just can't replace a real human yet...
To localize your app you can use Localizable.strings or use localized XIB.
I don't use the second approach because it is more cumbersome to manage.
With Localizable.strings you have to code a little bit more but I prefere it.
To translate I think you can use google translate (I do that) and build the file for each language.
If you want to use an online translator, remember that it can be not always available and your user must be connected all the time.
So my advice is to build all the language files since they are managed by the OS and you will have a more reliable solution.
You should create Localizable.strings for each language and translate them with any translation tool

J2ME Properties

J2ME lacks the java.util.Properties class. Although it is possible to put application settings in the JAD file this is not recommended for many properties. (Since, some platforms limits the size of JAD file.) I want to put a configuration file inside my jar file and parse it. And I do not want to go with XML because it will be overshooting for my case.
Question is, is there an already existing library for J2ME that can parse properties files or something similar such as INI file. Or would you recommend another method to solve the initial problem?
The best solution probably depends on what is going to be generating the properties files.
If you've got other non-JavaME projects using the same properties files, then stick with them, and write or find a parser. (There is a simple one from GoBible available on Google Code)
However you might find it just as easy to keep your configuration as static final String myproperty="myvalue"; in a Configuration.java file which you compile, and include in the jar instead, since you then do not need any special code to locate, open, read, and parse them.
You do then pick up a limitation on what you call them though, since you can no longer use the common dot separated namespacing idiom.

Resources