localizable.strings不能使用阿拉伯string
我在XCode中创build了一个Localizable.strings文件,然后使用了两种语言(英文+阿拉伯文)
我用这些语言翻译来填充这些文件,但是只用英文显示翻译,当我用阿拉伯语开始键时出现!
在我的代码中:
NSLocalizedString("title", comment: "")
Localizable.strings(英文)
"title" = "Error" ;
Localizable.strings(阿拉伯语)
"title" = "خطأ" ;
我关心样本之一,并试图在目标CI得到它。
我在阿拉伯本地化文件中设置“title”=“خطأ”
“title”=“عنوان”;
现在我必须把英文换成阿拉伯文。
首先,我在故事板中devisedevise
然后单击Project.Choose本地化信息
如果您单击+(低于本地化),它将显示popup视图
现在select阿拉伯语。当阿拉伯语显示窗口。你应该点击完成。
我们现在需要为本地化创buildstring文件。我将string文件名设置为LocalizationArabic
一旦你创build了String文件,它看起来像下面。
然后在按LocalizationArabicstring文件时点击File Inspector。现在点击Localization。显示Empty Checkcheckbox阿拉伯文和英文,如下所示。
在这里,我们必须检查checkbox。当我们检查checkbox时,LocalizationArabic文件夹将创build三个string文件,如下所示
然后,我在string文件中input了我想要从英文翻译成阿拉伯语的语言。
最后,我创build了本地化语言的头文件
头文件的名称是LanguageHeader.It如下所示。
现在代码部分从这里开始
首先是NSObject类的本地化类
Localization.h
#import <Foundation/Foundation.h> #import "LanguageHeader.h" @interface Localization : NSObject +(Localization *)sharedInstance; +(NSString*) strSelectLanguage:(int)curLang; +(NSString*) languageSelectedStringForKey:(NSString*) key; @end
Localization.m
#import“Localization.h”int currentLanguage,selectedrow; @implementation本地化
+(Localization *)sharedInstance { static Localization *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[Localization alloc] init]; }); return sharedInstance; } +(NSString*) strSelectLanguage:(int)curLang{ if(curLang==ARABIC){ [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar", nil]forKey:@"AppleLanguages"]; } else{ [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil]forKey:@"AppleLanguages"]; } [[NSUserDefaults standardUserDefaults] synchronize]; currentLanguage=curLang; NSString *strLangSelect = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0]; return strLangSelect; } +(NSString*) languageSelectedStringForKey:(NSString*) key { NSString *path; NSString *strSelectedLanguage = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0]; //When we check with iPhone,iPad device it shows "en-US".So we need to change it to "en" strSelectedLanguage = [strSelectedLanguage stringByReplacingOccurrencesOfString:@"en-US" withString:@"en"]; if([strSelectedLanguage isEqualToString:[NSString stringWithFormat: @"en"]]){ currentLanguage=ENGLISH; selectedrow=ENGLISH; path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]; } else{ currentLanguage=ARABIC; selectedrow=ARABIC; path = [[NSBundle mainBundle] pathForResource:@"ar" ofType:@"lproj"]; } NSBundle* languageBundle = [NSBundle bundleWithPath:path]; NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizationArabic"]; return str; } @end
然后是ViewController.h
#import <UIKit/UIKit.h> #import "Localization.h" @interface ViewController : UIViewController{ Localization *localization; } @property (strong, nonatomic) IBOutlet UILabel *lblTitle; - (IBAction)actionChangeLanguageToArabic:(id)sender; @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize lblTitle; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. localization = [Localization sharedInstance]; lblTitle.text = [Localization languageSelectedStringForKey:@"title"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)actionChangeLanguageToArabic:(id)sender { [Localization strSelectLanguage:ARABIC]; lblTitle.text = [Localization languageSelectedStringForKey:@"title"]; } @end
以上代码完美工作。
输出屏幕截图如下
首先运行应用程序
点击button后