我在XCode中创建了一个Localizable.strings文件,然后在其中创建了两种语言。(英语+阿拉伯语)
我用语言翻译填写了这些文件,但只用英语显示翻译,当我用阿拉伯语开始时,键出现了!
在我的代码中:
NSLocalizedString("title", comment: "")
Localizable.strings(英文)
"title" = "Error" ;
Localizable.strings(阿拉伯语)
"title" = "خطأ" ;
答案 0 :(得分:0)
我关心样本一并尝试了目标C.我得到了它。
我在阿拉伯语本地化文件中设置了“title”=“خطأ”
“title”=“عنوان”;
现在我必须将英语改为阿拉伯语。
首先我在故事板中设置设计
然后单击Project.Choose Info in Info
如果单击+(在本地化下方),则会显示弹出视图
现在选择阿拉伯语。当点击阿拉伯语时,它会显示窗口。你应该点击完成。
我们现在需要为本地化创建字符串文件。我将字符串文件名设置为 LocalizationArabic
创建String文件后,它将如下所示。
然后在按LocalizedArabic string file时单击File Inspector。现在单击Localization.It显示Empty复选框阿拉伯语和英语,如下所示。
这里我们必须选中复选框。当我们勾选复选框时,LocalizationArabic文件夹会创建三个字符串文件,如下所示
然后我输入了我想在字符串文件中从英语翻译成阿拉伯语的语言。
最后,我创建了本地化语言的头文件
Header文件名是LanguageHeader.It如下所示。
现在代码部分从这里开始
首先是NSObject类的Localization类
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
+(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
以上代码完美无缺。
输出屏幕截图位于
之下首次运行应用
点击按钮