Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the current localization of iOS app in code

Connected issue: Non-English default language for iOS App?

I am wondering how to check the current localization of my app in code. For example: I want to check if the app is running with German localization (.strings, storyboard) This line of code:

[[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"de"]

doesn't provide the correct answer. Because if a user's language list in device settings looks like this for example (french, german, english) and we have only localization for english and german, application is launched in german but the above condition isn't fulfilled.

like image 672
Michał Młudzik Avatar asked Mar 14 '14 12:03

Michał Młudzik


People also ask

What is iOS localization?

Localization is the process of translating and adapting your app into multiple languages and regions. Localize your app to provide access for users who speak a variety of languages, and who download from different App Store territories.

What is Lproj Xcode?

lproj folder is a directory that stores language-specific resources. See Localized Resources in Bundles and Language and Locale IDs for details. If none of the user's preferred languages are supported by your app, iOS chooses the language matching your app's development region ( CFBundleDevelopmentRegion ).


2 Answers

My naive solution would be to create a localized string "LanguageCode" which is set to "de" in the german string file and to "en" in the english string file

e.g.:

if ([NSLocalizedString(@"LanguageCode", @"en, de etc.") isEqualToString:@"de"]) {
    // german
}
like image 137
Matthias Bauch Avatar answered Oct 17 '22 01:10

Matthias Bauch


NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

This will return a two letter code for the currently selected language. "en" for English, "es" for Spanish, "de" for German, etc. For more examples, please see this Wikipedia entry (in particular, the 639-1 column):

like image 21
Hitendra Avatar answered Oct 17 '22 01:10

Hitendra