如何跟踪iPad中的语言变化?

时间:2021-10-24 20:09:53

I'm new in developing iOS app. So I've recently faced a problem with localization of my app. I would like to perform some actions in response to language change in system settings. To get language ID I use

我是开发iOS应用程序的新手。所以我最近遇到了我的应用程序本地化问题。我想针对系统设置中的语言更改执行一些操作。获取我使用的语言ID

[[NSLocale preferredLanguages] objectAtIndex:0];

in applicationDidFinishLaunchingWithOptions method. First time it returns language ID right (e.g. "en" for English). Then, I close my application (or send it to background) and change language to German. After that I restart my app, but instead of "de", it still returns "en". I think my app might probably store old settings somewhere... but where exactly? How to retrieve valid current value?

在applicationDidFinishLaunchingWithOptions方法中。第一次它返回语言ID(例如英语的“en”)。然后,我关闭我的应用程序(或将其发送到后台)并将语言更改为德语。之后,我重新启动我的应用程序,但不是“de”,它仍然返回“en”。我认为我的应用程序可能会在某处存储旧设置......但究竟在哪里?如何检索有效的当前值?

3 个解决方案

#1


0  

take a look at this old answer:

看看这个老答案:

Getting current device language in iOS?

在iOS中获取当前的设备语言?

EDIT:

编辑:

NSString *testLang = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *testLang1 = [[NSLocale preferredLanguages] objectAtIndex:1];
NSString *testLang2 = [[NSLocale preferredLanguages] objectAtIndex:2];
NSString *testLang3 = [[NSLocale preferredLanguages] objectAtIndex:3];
NSString *testLang4 = [[NSLocale preferredLanguages] objectAtIndex:4];
NSLog(@"LINGUA SYS:%@:%@:%@:%@:%@",testLang,testLang1,testLang2,testLang3,testLang4);

in debugger console:

在调试器控制台中:

LINGUA SYS:en:it:fr:de:ja

LINGUA SYS:en:it:fr:de:ja

or:

要么:

LINGUA SYS:es:en:it:fr:de

LINGUA SYS:es:en:it:fr:de

or:

要么:

LINGUA SYS:pt-PT:en:es:it:fr

LINGUA SYS:pt-PT:en:es:it:fr

so the code you posted is right, the error should be somewhere else...

所以您发布的代码是正确的,错误应该在其他地方......

#2


0  

if you do something like that:

如果你做那样的事情:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en",@"es", nil] forKey:@"AppleLanguages"];

the value is stored on NSUSerDefaults and changes on settings don't apply.

该值存储在NSUSerDefaults上,并且设置上的更改不适用。

#3


0  

I found the source of my problem. It was iOS Simulator. I installed my app on device and all problems disappeared.

我找到了问题的根源。这是iOS模拟器。我在设备上安装了我的应用程序,所有问题都消失了。

#1


0  

take a look at this old answer:

看看这个老答案:

Getting current device language in iOS?

在iOS中获取当前的设备语言?

EDIT:

编辑:

NSString *testLang = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *testLang1 = [[NSLocale preferredLanguages] objectAtIndex:1];
NSString *testLang2 = [[NSLocale preferredLanguages] objectAtIndex:2];
NSString *testLang3 = [[NSLocale preferredLanguages] objectAtIndex:3];
NSString *testLang4 = [[NSLocale preferredLanguages] objectAtIndex:4];
NSLog(@"LINGUA SYS:%@:%@:%@:%@:%@",testLang,testLang1,testLang2,testLang3,testLang4);

in debugger console:

在调试器控制台中:

LINGUA SYS:en:it:fr:de:ja

LINGUA SYS:en:it:fr:de:ja

or:

要么:

LINGUA SYS:es:en:it:fr:de

LINGUA SYS:es:en:it:fr:de

or:

要么:

LINGUA SYS:pt-PT:en:es:it:fr

LINGUA SYS:pt-PT:en:es:it:fr

so the code you posted is right, the error should be somewhere else...

所以您发布的代码是正确的,错误应该在其他地方......

#2


0  

if you do something like that:

如果你做那样的事情:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en",@"es", nil] forKey:@"AppleLanguages"];

the value is stored on NSUSerDefaults and changes on settings don't apply.

该值存储在NSUSerDefaults上,并且设置上的更改不适用。

#3


0  

I found the source of my problem. It was iOS Simulator. I installed my app on device and all problems disappeared.

我找到了问题的根源。这是iOS模拟器。我在设备上安装了我的应用程序,所有问题都消失了。