localizable.strings无法与阿拉伯字符串一起使用
我先用XCode创建了Localizable.strings文件,然后用其中的2种语言.(英语+阿拉伯语)
I created a Localizable.strings file in XCode and then 2 languages in it.(english + Arabic )
我用语言翻译填充了这些文件,但是当我以阿拉伯语开头时,显示的只是英语翻译,因此会出现密钥!
I filled up these files with the language translations, but the just show translation in english, when I start with Arabic the key appear!
在我的代码中:
NSLocalizedString("title", comment: "")
Localizable.strings(english)
Localizable.strings(english)
"title" = "Error" ;
Localizable.strings(阿拉伯语)
Localizable.strings(Arabic)
"title" = "خطأ" ;
我精心挑选了一个样本,并在Objective C中进行了尝试.
I careated sample one and tried in Objective C.I got it.
我在阿拉伯语本地化文件中设置了"title" =خطأ"
I set "title" = "خطأ" in Arabic Localization files
"title" =عنوان";
"title" = "عنوان";
现在我必须将英语更改为阿拉伯语.
Now I have to change English to Arabic.
首先,我在情节提要中设置设计
First I set the design in storyboard
然后单击Project.在信息中选择本地化
Then Click Project.Choose Localization in Info
如果您点击+(位于本地化下方),则会显示弹出视图
If you click the +(Below Localization) it shows the pop up view
现在选择阿拉伯语.单击阿拉伯语"将显示窗口.您应单击完成".
Now choose Arabic.When click Arabic it shows window.You should click finish.
我们现在需要为本地化创建字符串文件.我将字符串文件名设置为 LocalizationArabic
We need to create the string file for the localization now.I set string file name as LocalizationArabic
创建String文件后,其外观如下所示.
Once you create the String file it looks like below.
然后在按下LocalizationArabic字符串文件时单击File Inspector.现在单击Localization.它将显示Empty复选框,阿拉伯和英语如下所示.
Then click File Inspector when pressing LocalizationArabic string file.Now click the Localization.It shows Empty Check box Arabic and English like below.
这里我们必须选中该复选框.此外,当我们选中该复选框时,LocalizationArabic文件夹将创建带有如下所示的三个字符串文件的
Here we must check the check box.Also when we check the check box the LocalizationArabic folder creates with three string files like below
然后我在字符串文件中输入了要从英语翻译为阿拉伯语的语言.
Then I entered the language which I want to translate from English to Arabic in string file.
最后,我为本地化语言创建了头文件
Finally I created the Header file for the Localization Language
Header文件名为LanguageHeader,如下所示.
The Header file name is LanguageHeader.It looks like below.
现在代码部分从这里开始
Now the code part starts here
首先是NSObject类的Localization类
First the Localization class of NSObject class
Localization.h
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
Localization.m
#import"Localization.h" int currentLanguage,selectedrow; @implementation本地化
#import "Localization.h" int currentLanguage,selectedrow; @implementation Localization
+(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
Then 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
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
以上代码可完美运行.
输出屏幕截图如下
第一次运行应用程序时
点击按钮后