Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS:How to get current application language

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

like image 242
gpopov Avatar asked Sep 10 '25 02:09

gpopov


2 Answers

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.

like image 104
mgcm Avatar answered Sep 12 '25 16:09

mgcm


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.

like image 38
Jonas Schnelli Avatar answered Sep 12 '25 16:09

Jonas Schnelli