第一次进入应用获取本机语言.1.假设本机语言在软件实现语言列表里.则默认选中本机语言,可是用户还是能够选择其他语言.2.假设本机语言不在软件实现语言列表里,则默认选中英语.用户还是能够选择其他语言.
全部要更换语言的controller里注冊通知.当用户选择了一个语言后发送语言改动的通知.控制器收到语言改动通知后,对须要改动语言的控制进行又一次设置.
/** 初始化语言 */
- (void)initLanguage{
//1. 获取系统支持的语言
_languages = [NSMutableArrayarrayWithCapacity:5];
NSURL *url = [[NSBundlemainBundle]URLForResource:kLanguagePlistwithExtension:kTypePlist];
NSDictionary *dic = [NSDictionarydictionaryWithContentsOfURL:url];
//2 生成语言对象
[dicenumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop) {
DYCXLanguage *language = [DYCXLanguagelanguageWithKey:keyAndValue:obj];
[_languagesaddObject:language];
}];
//3. 获取用户保存的语言[0] = 中文简体, [1] = zh-Hans;
NSString *userLanguageValue = [selfgetUserLanguage];
if (!userLanguageValue) {
userLanguageValue =kDefaultUserLanguage;
//3.1 获取系统语言
NSString * localLanguage = [selfgetLocalLanguageValue];
//3.2. 确定默认语言
for (DYCXLanguage *languagein_languages) {
//3.3.假设软件语言包涵系统语言默觉得系统语言,假设不包涵默认设置为英语
if ([localLanguageisEqualToString:language.languageValue]) {
userLanguageValue = localLanguage;
}
}
}
//4. 设置当前语言
for (DYCXLanguage *languagein_languages) {
if ([userLanguageValueisEqualToString:language.languageValue]) {
language.used =YES;
_userLanguage = language;
break;
}
}
}
- (NSString *)getLocalLanguageValue{
NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
NSArray *languages = [defaultsobjectForKey:@"AppleLanguages"];
return [languagesobjectAtIndex:0];
}
#pragma mark - 语言更改,又一次载入
- (void)reloadStringWithLanguage{
[_confirmsetTitle:[NSStringlocalizedWithString:@"dialog_positive_key"]forState:UIControlStateNormal];
_selectLanguage.text = [NSStringlocalizedWithString:@"select_language_key"];
if ([self.delegaterespondsToSelector:@selector(onLanguageChange)]) {
[self.delegateonLanguageChange];
}
}
#pragma mark -NSString 的分类,载入相应文件夹里的string
+ (NSString *)localizedWithString:(NSString *)key{
NSString *area = [DYCXConfigsharedDYCXConfig].userLanguage.languageValue;
if (!area) {
area = kDefaultUserLanguage;
}
NSString *table = [NSStringstringWithFormat:@"%@.lproj/Localizable", area];
NSString *localizedString = [[NSBundlemainBundle] localizedStringForKey:(key)value:nil table:(table)];
NSAssert(localizedString,@"localized error, table = %@, key = %@", table, key);
return localizedString;
}
以本地化方式获取国际化信息的显示名称
NSLocale *curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"] ;
NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );// 法文(法国)
curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hant"] ;
NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );//法文(法國)