iOS:How to get current application language
Asked Answered
K

5

25

The application that I'm working on supports 3 languages: English, French and German.

How I can get the current application language (NOT the device language)?

The problem is that I have to get the current language of the application in order to send it with a request to the server and the respond should be in the right language. The device language is useless because if the user switch the os language to Italian, the app is running in English and I need to send english to the server.

Thanks

Keir answered 6/4, 2012 at 16:9 Comment(2)
How does the user choose the app language?Domela
From your own settings, add list of languages you support.Wilmott
R
46

What i always do:

Add a string entry into the Localizable.strings files. I always use the key "lang"="de"; (or "lang"="en", etc.).

Then you can use it in your NSURLRequest by adding the language over NSLocalizedString(@"lang", @"")

With that method you have absolute control what is going to be sent to you backend.

Remediosremedy answered 6/4, 2012 at 16:40 Comment(4)
Yea, simple solution :) Thanks.Keir
I use this since [[NSBundle mainBundle] preferredLocalizations] is not reliable.Shaniqua
Silly hack - see answer by @Cottier for the correct way using iOS system APIsGould
thanks a lot, this is very simple answer and helped me a lot! @Jonas SchnelliMeenen
C
49

The accepted answer is a workaround.

Regarding language preferences in the device itself, you have the

[NSLocale preferredLanguages]

which will give you an ordered array of the preferred languages as defined in the system's General Settings.

The Cocoa Touch framework will take this list of preferred languages into account when loading the app's localization resources and filter it according to the translations you provide in the bundle.

This filtered and ordered list of localized languages can be obtained with

[[NSBundle mainBundle] preferredLocalizations]

Your server requests should match the first value in this array or you will have a language mismatch between app and server data.

Cottier answered 23/7, 2014 at 14:18 Comment(1)
This definitely seems like the correct way to do it.. should be the accepted answer!Melanite
R
46

What i always do:

Add a string entry into the Localizable.strings files. I always use the key "lang"="de"; (or "lang"="en", etc.).

Then you can use it in your NSURLRequest by adding the language over NSLocalizedString(@"lang", @"")

With that method you have absolute control what is going to be sent to you backend.

Remediosremedy answered 6/4, 2012 at 16:40 Comment(4)
Yea, simple solution :) Thanks.Keir
I use this since [[NSBundle mainBundle] preferredLocalizations] is not reliable.Shaniqua
Silly hack - see answer by @Cottier for the correct way using iOS system APIsGould
thanks a lot, this is very simple answer and helped me a lot! @Jonas SchnelliMeenen
A
14

You may use the preferredLocalizations method of the NSBundle class:

NSString *currentLocalization = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
Anstus answered 17/3, 2013 at 15:6 Comment(2)
Does this really work? I have an app that supports English (en) and Swedish (sv) and if I set the simulator to use German, the value above becomes "de".Maxwellmaxy
@DanielSaidi Of course you do. That's why Apple provides documentation: "..strings are ordered according to the user's language preferences.."Gould
D
6

Since iOS 13 the language can be set for each app individually.

In the session "Creating Great Localized Experiences with Xcode 11" at WWDC19 they showed two options for determining the user settings for the application.

Get the currently running application language:

Bundle.main.preferredLocalizations.first

Get the best language, given an external language code list:

let availableLanguages = Server.requestLanguages()
Bundle.preferredLocalizations(from: availableLanguages).first
Drifter answered 4/1, 2020 at 10:52 Comment(1)
this method will only work if we implement localization via apple way.Copenhagen
U
-2

The app language will change when the user change the device language and you can get it from the NSUserDefaults (i will show you how if you want to ) unless there are an option to change the language inside the app then you can easy save the current used language and send it to the server when ever you want.

Ule answered 6/4, 2012 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.