这段时间没有那么忙整理了一下关于语言国际化的内容,写在这上面,给没做过的当做资料,也当做是自己的笔记,
首先在工程中创建一个PCH文件,在PCH文件中写入一个宏定义
#define Localized(key)[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"appLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:nil table:@"NSLocalizedLanguage"]
现在为工程建立.string的文件
在这里文件的名字必须要是 NSLocalizedLanguage 因为我的宏定义写的是这个名字
然后在工程中添加需要的语种,更具需要选择
回到工程文件下,点击.string的文件在右侧的设置栏中点击Locallzation 点击语言种类
此时在.string的文件下就会看到选择的语种的文件
在文件中将想要转换的文字,依次写到对应的语言文件里就行了
现在就去工程中添加获取获取系统当前语言的代码来到Application.h
声明选择设置语言界面的控制器 在声明一个方法 如图所示为了写block就写的很简单
接下来在Application.m中添加代码
为了方便我将代码单独放在这里
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
/**
* 获取当前语言
*/
if (![[NSUserDefaults standardUserDefaults]objectForKey:@"appLanguage"]) {
NSArray *langusges = [NSLocale preferredLanguages];
NSString *language = [langusges objectAtIndex:0];
if ([language hasPrefix:@"zh-Hans"]) {//开头匹配
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"appLanguage"];
}else{
[[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"appLanguage"];
} }
self.window.rootViewController = [ViewController new];
[self.window makeKeyAndVisible];
return YES;
}
- (void)ToMain
{
self.RootVC = [ViewController new];
self.window.rootViewController = self.RootVC;
}
这样获取系统语言就算完成了,viewController.m
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@property (assign ,nonatomic)BOOL IsEngish;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *LanguageButton = [UIButton buttonWithType:UIButtonTypeSystem];
LanguageButton.frame = CGRectMake(70, 100, 230, 30);
[LanguageButton setTitle:Localized(@"你好") forState:UIControlStateNormal];
LanguageButton.backgroundColor = [UIColor orangeColor];
[self.view addSubview:LanguageButton];
UIButton *English = [UIButton buttonWithType:UIButtonTypeSystem];
English.frame = CGRectMake(70, 160, 60, 30);
[English setTitle:Localized(@"英语") forState:UIControlStateNormal];
English.backgroundColor = [UIColor orangeColor];
[English addTarget:self action:@selector(English:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:English];
UIButton *Chinese = [UIButton buttonWithType:UIButtonTypeSystem];
Chinese.frame = CGRectMake(240, 160, 60, 30);
[Chinese setTitle:Localized(@"汉语") forState:UIControlStateNormal];
[Chinese addTarget:self action:@selector(chinese:) forControlEvents:UIControlEventTouchDown];
Chinese.backgroundColor = [UIColor redColor];
[self.view addSubview:Chinese];
}
- (void)chinese:(UIButton *)sender
{
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"appLanguage"];
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate ToMain];
}
- (void)English:(UIButton *)button
{
[[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"appLanguage"];
AppDelegate *appDelegate =
(AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate ToMain];
}
本地语言国际化大概就是这个流程,在这里我只用了中英文,其他的语言也是一样,
src="" width="510" height="498">