Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use "real" keys in NSLocalizedString()? Is there a guaranteed fallback language?

I know many developers just do it like this: They start developing their app in English, and put NSLoclaizedString(@"Tap this to do that!", @"Telling what to do...") instead of simply @"Tap this to do that!".

Then they run genstrings which creates a Localizable.strings file somehow by extracting all these strings. The messy part: The long text used in code becomes the key. It works. Until one day where you quickly go into your code and change the english string and forget about Localization and that it serves as the key for all those Localizable.strings files.

So I tend to use "real" keys which don't get mixed up with strings. For a quick test I created a project localized to English and French. Then I set the Simulator language to German. Because, you know, it would suck terribly if the user would ever see the key like TTTDT.

So with just English and German in place, I launched the demo app. And what I got was the English text from the English Localizable.strings file.

Conclusion: It seems that NSLocalizedString falls back to the English file if the OS language is not covered by the app.

Quesion: Assuming there IS always an Localizable.strings (English) file, and the keys ARE in the file along with properly formatted values. Are there circumstances under which NSLocalizedString would fail and then display the key directly?

like image 778
dontWatchMyProfile Avatar asked Nov 26 '11 19:11

dontWatchMyProfile


2 Answers

To answer your question: Yes, I have experienced the issue you are worrying about, i.e. the key names showed up even though a localizable.strings file was present and included entries for those key names. This happens when you have more than one localizable.strings files in your project. Which can easily happen if you drop a set of files from an Open Source project that has their own localizable.strings (such as ShareKit) into your project.

Here is a related question that discusses this issue.

But at least if you use ID-style key names, you would notice such a problem when you test your app in any language. If you used the English (or base language) string as the keys, then you wouldn't see this insidious problem until you test the localized versions and it could slip by unnoticed more easily.

So on top of your point on having to remember to update the keys (in all languages) when rewording text, there is the issue of potentially hiding bugs when using the English text as key (English would look OK, but localized versions wouldn't). Therefore it seems to me that using "real" key names rather than actual text is more practical. If you are still worried that for some reason the key names might show up, pick a key that is at least descriptive enough to be understandable.

like image 198
Clafou Avatar answered Oct 31 '22 20:10

Clafou


We tend to use "real" keys but they are usually the English text (or an abbreviated form) and add "Key" to the end. That way it is clear.

like image 45
zaph Avatar answered Oct 31 '22 21:10

zaph