我想要的很简单:
我的应用只有两种语言:英语和简体中文。我希望如果系统语言是简体中文或繁体中文,那么使用简体中文;否则使用英语。
似乎大多数用户不知道他们可以设置他们的辅助首选语言。所以我不能依赖它们。
答案 0 :(得分:0)
您可以使用此代码检索用户语言并设置应用语言:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Reset system defaults to get the complete language list.
[defaults removeObjectForKey:@"AppleLanguages"];
// Default language choosen by user.
NSString *defLanguage = [defaults objectForKey:@"Language"];
NSArray *sysLangugages = [defaults arrayForKey:@"AppleLanguages"];
// System default language: first element of array.
NSString *sysLanguage = [sysLangugages objectAtIndex:0];
NSArray *array = [NSArray arrayWithObjects:defLanguage, sysLanguage, nil];
LocalizationSetLanguage(defLanguage);
[defaults setObject:array forKey:@"AppleLanguages"];
//To set and get the saved language
// in the .h
#define LocalizationSetLanguage(language) \
[[LocalizationSystem sharedLocalSystem] setLanguage:(language)]
//in the .m
- (void) setLanguage:(NSString*) language
{
lang = language;
}
- (NSString*) getLanguage
{
if (!lang) {
NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
NSString *preferredLang = [languages objectAtIndex:0];
lang = preferredLang;
}
return lang;
}
答案 1 :(得分:0)
使用AMLocalizedString
,您可以使用它来设置本地语言。