The application that I'm working on supports 3 languages: English, French and German.
我正在开发的应用程序支持3种语言:英语,法语和德语。
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.
问题是我必须获取应用程序的当前语言,以便向服务器发送请求,并且响应应该使用正确的语言。设备语言没用,因为如果用户将os语言切换为意大利语,则应用程序以英语运行,我需要将英语发送到服务器。
Thanks
谢谢
4 个解决方案
#1
33
What i always do:
我一直在做什么:
Add a string entry into the Localizable.strings files. I always use the key "lang"="de";
(or "lang"="en"
, etc.).
将字符串条目添加到Localizable.strings文件中。我总是使用键“lang”=“de”; (或“lang”=“en”等)。
Then you can use it in your NSURLRequest by adding the language over NSLocalizedString(@"lang", @"")
然后你可以通过NSLocalizedString(@“lang”,@“”)添加语言,在NSURLRequest中使用它
With that method you have absolute control what is going to be sent to you backend.
使用该方法,您可以绝对控制将要发送给您的后端。
#2
32
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.
Cocoa Touch框架将在加载应用程序的本地化资源时将此首选语言列表考虑在内,并根据您在捆绑中提供的翻译对其进行过滤。
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.
您的服务器请求应与此数组中的第一个值匹配,否则应用程序和服务器数据之间的语言将不匹配。
#3
14
You may use the preferredLocalizations method of the NSBundle class:
您可以使用NSBundle类的preferredLocalizations方法:
NSString *currentLocalization = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
#4
-1
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.
当用户更改设备语言时,应用程序语言将会更改,您可以从NSUserDefaults获取它(我将告诉您如何使用),除非有更改应用程序内部语言的选项,您可以轻松保存当前使用语言并在需要时将其发送到服务器。
#1
33
What i always do:
我一直在做什么:
Add a string entry into the Localizable.strings files. I always use the key "lang"="de";
(or "lang"="en"
, etc.).
将字符串条目添加到Localizable.strings文件中。我总是使用键“lang”=“de”; (或“lang”=“en”等)。
Then you can use it in your NSURLRequest by adding the language over NSLocalizedString(@"lang", @"")
然后你可以通过NSLocalizedString(@“lang”,@“”)添加语言,在NSURLRequest中使用它
With that method you have absolute control what is going to be sent to you backend.
使用该方法,您可以绝对控制将要发送给您的后端。
#2
32
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.
Cocoa Touch框架将在加载应用程序的本地化资源时将此首选语言列表考虑在内,并根据您在捆绑中提供的翻译对其进行过滤。
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.
您的服务器请求应与此数组中的第一个值匹配,否则应用程序和服务器数据之间的语言将不匹配。
#3
14
You may use the preferredLocalizations method of the NSBundle class:
您可以使用NSBundle类的preferredLocalizations方法:
NSString *currentLocalization = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
#4
-1
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.
当用户更改设备语言时,应用程序语言将会更改,您可以从NSUserDefaults获取它(我将告诉您如何使用),除非有更改应用程序内部语言的选项,您可以轻松保存当前使用语言并在需要时将其发送到服务器。