Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localizing strings in iOS: default (fallback) language?

Is there a way to set a default language to be used when the device UI language is not supported by an app?

Example: My app is localized into English and German:

// en.lproj: "POWER_TO_THE_PEOPLE_BTN" = "Power"; "POWER_PLUG_BTN" = "Power";  // de.lproj: "POWER_TO_THE_PEOPLE_BTN"  = "Macht"; "POWER_PLUG_BTN" = "Spannung"; 

Now, if I run the app on a device with UI language set to Italian the app will use the key strings POWER_TO_THE_PEOPLE_BTN and POWER_PLUG_BTN.

There must be a way to specify a default (fallback) language to be used by the application in such a case.

From the above example it should be clear that using the English string as a key will not work.

The only option I see right now is to use NSLocalizedStringWithDefaultValue instead of NSLocalizedString.

like image 969
Volker Voecking Avatar asked Jul 16 '10 10:07

Volker Voecking


People also ask

How does localization work in IOS?

Localization is the process of making your app support other languages. In many cases, you make your app with English user interface first and then localize the app to other languages such as Japanese. The process of localization is tedious, and steps of it change little by little as XCode gets updated.

What is IOS fallback?

The system presents a fallback button when biometric authentication fails—for example, because the system doesn't recognize the presented finger, or after several failed attempts to recognize the user's face. Tapping the button lets the user revert to authentication using the device passcode or password instead.

How do you localize a string in Swift?

Select the project and under “Localizations”, click the “+” icon. Add any language you want (I selected Italian) then click on “Finish”. Now go back to Localizable. string file, select it and on the File Inspector (right menu) select “Localize”.

What are localization strings?

A localized string can have different values depending on the language in which the project is being build. There are two categories of localized strings: the strings included in the installation package's UI, common to every MSI file.


1 Answers

To avoid all those lengthy syntax and more having more descriptive var name for translators, I derived my own helper method L() for translation and falling back to English

NSString * L(NSString * translation_key) {     NSString * s = NSLocalizedString(translation_key, nil);     if (![[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"] && [s isEqualToString:translation_key]) {     NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];     NSBundle * languageBundle = [NSBundle bundleWithPath:path];     s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil];     }     return s; } 

My Localizable.strings would look like this

"SOME_ACTION_BUTTON" = "Do action"; 

So in my code, i would use L(@"SOME_ACTION_BUTTON") to get the correct string

Though sometime the key is longer than the translation itself HELP_BUTTON_IN_NAV_BAR = 'Help' but it saves me a lot of time explaining what it is to whoever is helping me doing the translation

like image 188
Kent Nguyen Avatar answered Sep 17 '22 21:09

Kent Nguyen