我的应用程序包含两个用于英语和阿拉伯语的XIB文件。我希望在语言更改时更改XIB文件,而无需重新启动应用程序。
答案 0 :(得分:0)
我在最近的项目中完成了同样的任务需要支持阿拉伯语&英语。我需要根据列表中的选定选项更改语言。我为阿拉伯语创建了两个.xib,为英语创建了一个。然后选择
//用于设置包
+(void) setContentBundle{
SS AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSString *currentLanguage;
if([delegate.strLanguage length]> 0)
{
currentLanguage = delegate.strLanguage;
}else {
currentLanguage = @"en";
}
if ([currentLanguage isEqualToString:@"ar"]) { // If ARABIC.
languageBundle = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"ar" ofType:@"lproj"];
languageBundle = [NSBundle bundleWithPath:path];
}else if ([currentLanguage isEqualToString:@"en"]){ // If ENGLISH.
languageBundle = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
languageBundle = [NSBundle bundleWithPath:path];
}
}
// getting localiation key value
+(NSString *)getLocalizedStringForKey:(NSString *)key value:(NSString *)v table:(NSString *)t{
if([key isEqualToString:@"FollowUp.DepartmentKey"]){
NSLog(@"%@",key);
}
SSAppDelegate *delegate = (SSAppDelegate *)[UIApplication sharedApplication].delegate;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"KEY LIKE[c] %@",
key];
NSArray *filteredArray = [delegate.arrSystemMessages filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filteredArray);
if(filteredArray!=nil && [filteredArray count]>=1){
NSMutableDictionary *dict=[filteredArray objectAtIndex:0];
if([[ContentBundleManager selectLanguage] isEqualToString:@"ar"]){
return [dict valueForKey:@"AR"];
}else{
return [dict valueForKey:@"EN"];
}
}else{
return [[ContentBundleManager getContentBundle] localizedStringForKey:key value:v table:t];
}
}
//for getting bundle
+(NSBundle *) getContentBundle{
SSAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSString *currentLanguage;
if([delegate.strLanguage length]> 0)
{
currentLanguage = delegate.strLanguage;
}else {
currentLanguage = @"en";
}
if ([currentLanguage isEqualToString:@"ar"]) { // If ARABIC.
languageBundle = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"ar" ofType:@"lproj"];
languageBundle = [NSBundle bundleWithPath:path];
}else if ([currentLanguage isEqualToString:@"en"]){ // If ENGLISH.
languageBundle = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
languageBundle = [NSBundle bundleWithPath:path];
}
return languageBundle;
}
/// and call .xib
SSBaseViewController *baseView= [SSLBaseViewController getBaseViewObjectWithBundle:nil];
/// and call string file
[baseView setHeaderTitle:[ContentBundleManager getLocalizedStringForKey:@"FollowUp.FollowUpKey" value:@"" table:nil]];