本地化字符串需要一个Localize.settings文件,在代码中需要输入字符串的地方使用NSLocalizeString宏来定义字符串。
原理:
在代码中将加入的字符串加入NSLocalizeString宏中
启动程序时,系统将根据语言加载相应的文件得到其对应的字符串文件,这个字符串可以通过系统将NSLocalizedString中的宏生成名为“Localizable.strings”的文件
当然你可以自己去创建这个文件,然后手动是添加对应的字符串
步骤:
1.
创建文件:command+N
如果之前没有添加过其他语言,则需要执行:
http://blog.csdn.net/xiaomi_dalianmifen/article/details/13019387
前三步,添加多种语言
2.
然后选中创建的Localizable.strings文件,在属性检查器的localization项点击-Localizae
就ok啦
就会看到之前创建的Localizable.strings文件有两个语言版本了
然后对其内部进行操作
Localizable.strings(English)
"username"="username";
"password"="password";
"email"="email";
"usernameTF"="input username";
"passwordTF"="input password";
"emailTF"="input email";
"reg"="reg";
"back"="back";
"username"="用户名";
"password"="密码";
"email"="邮箱";
"usernameTF"="输入用户名";
"passwordTF"="输入密码";
"emailTF"="输入邮箱";
"reg"="注册";
"back"="返回";
其余代码:
#pragma mark -
#pragma mark viewDidLoad =====
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self buildLayout];
}
#pragma mark -
#pragma mark buildLayout =====
-(void)buildLayout
{
UILabel *usernamelab=[[UILabel alloc]initWithFrame:CGRectMake(20, 50, 100, 40)];
usernamelab.text=NSLocalizedString(@"username", "please input your name");
usernamelab.backgroundColor=[UIColor clearColor];
[self.view addSubview:usernamelab];
UITextField *usernameTF=[[UITextField alloc]initWithFrame:CGRectMake(150, 50, 150, 40)];
usernameTF.borderStyle=UITextBorderStyleRoundedRect;
usernameTF.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
usernameTF.placeholder=NSLocalizedString(@"usernameTF", "please input your name");
[self.view addSubview:usernameTF];
UILabel *passwordlab=[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 100, 40)];
passwordlab.text=NSLocalizedString(@"password", "please input your password");
passwordlab.backgroundColor=[UIColor clearColor];
[self.view addSubview:passwordlab];
UITextField *passwordTF=[[UITextField alloc]initWithFrame:CGRectMake(150, 100, 150, 40)];
passwordTF.borderStyle=UITextBorderStyleRoundedRect;
passwordTF.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
passwordTF.placeholder=NSLocalizedString(@"passwordTF", "please input your name");
[self.view addSubview:passwordTF];
UILabel *emaillab=[[UILabel alloc]initWithFrame:CGRectMake(20, 150, 100, 40)];
emaillab.backgroundColor=[UIColor clearColor];
emaillab.text=NSLocalizedString(@"email", "pleaase input your email");
[self.view addSubview:emaillab];
UITextField *emaillabTF=[[UITextField alloc]initWithFrame:CGRectMake(150, 150, 150, 40)];
emaillabTF.borderStyle=UITextBorderStyleRoundedRect;
emaillabTF.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
emaillabTF.placeholder=NSLocalizedString(@"emailTF", "please input your name");
[self.view addSubview:emaillabTF];
UIButton *regBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
regBtn.frame=CGRectMake(40, 200, 100, 40);
[regBtn setTitle:NSLocalizedString(@"reg", "please click reg") forState:UIControlStateNormal];
[self.view addSubview:regBtn];
UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
backBtn.frame=CGRectMake(170, 200, 100, 40);
[backBtn setTitle:NSLocalizedString(@"back", "please click back") forState:UIControlStateNormal];
[self.view addSubview:backBtn];
}
@end
效果图片