天天看点

ios 多语言支持

xcode4中添加多国语言支持已经与前期的版本方式不太相同,这里进行简要的介绍。

一,在你的工程中找到supporting files,在其上右键,找到如下 所示的蓝色高亮项,单击。

注意,你已经看到了我生成的多个语言支持文件,你在没有添加以前这里是没有的!!!!。

ios 多语言支持

二,如下,选择ios目录下的资源文件,找到string文件选择。

ios 多语言支持

三,将新建的文件命名为localizable,这个是一定的,不能命名为其他的名称。这里要注意。完成后打开inspector,选中localizable.strings,你可以看到如下所示的界面。

ios 多语言支持

四,找到localization,点击左下角的+号按钮如下:添加你想要的语言支持。

ios 多语言支持
ios 多语言支持

五,最终生成的语言文件如下:

ios 多语言支持

此时你将在你的工程文件下面找到如下所示的文件目录:每一个文件目录中都有对应的语言文件支持。现在需要做的就是在这些对应的文件中添加字符串支持。最终在要设置字符串的代码中调用nslocalizedstring(@"string2",nil);方法进行字符串的显示,一切搞写。

ios 多语言支持

范例如下: localizable.strings (english)

"demotitle"="this is english version";

"string1"="apple";

"string2"="banana";

"string3"="orange";

"string4"="watermelon";

"string5"="strawberry";

localizable.strings (zh_cn)

"demotitle"="這是中文的版本";

"string1"="苹果";

"string2"="香蕉";

"string3"="橘子";

"string4"="西瓜";

"string5"="草莓";

代码:

multilanguagedemoviewcontroller.h

#import <uikit/uikit.h>

@interface multilanguagedemoviewcontroller : uiviewcontroller {

        iboutlet uilabel *lbldemotitle;

        iboutlet uilabel *lblstr1;

        iboutlet uilabel *lblstr2;

        iboutlet uilabel *lblstr3;

        iboutlet uilabel *lblstr4;

        iboutlet uilabel *lblstr5;

}

@property (nonatomic,retain) uilabel *lbldemotitle;

@property (nonatomic,retain) uilabel *lblstr1;

@property (nonatomic,retain) uilabel *lblstr2;

@property (nonatomic,retain) uilabel *lblstr3;

@property (nonatomic,retain) uilabel *lblstr4;

@property (nonatomic,retain) uilabel *lblstr5;

@end

multilanguagedemoviewcontroller.m

#import "multilanguagedemoviewcontroller.h"

@implementation multilanguagedemoviewcontroller

@synthesize lbldemotitle,lblstr1,lblstr2,lblstr3,lblstr4,lblstr5;

- (void)viewdidload {

    [super viewdidload];

        //系统设定取得多国语言字符串

        lbldemotitle.text=nslocalizedstring(@"demotitle",nil);

        lblstr1.text= nslocalizedstring(@"string1",nil);

        lblstr2.text=nslocalizedstring(@"string2",nil);

        lblstr3.text=nslocalizedstring(@"string3",nil);

        lblstr4.text=nslocalizedstring(@"string4",nil);

        lblstr5.text=nslocalizedstring(@"string5",nil);        

2. xib文件国际化

在需要国际化的xib文件上get info 添加多语言版本,

修改各语言版本相应的界面文字及图片。

模拟器测试记得要在模拟器上恢复原厂设置,xcode也要clean一下,否则可能出错。

国际码查询:http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm

3. 程序名称国际化

新建一个infoplist.strings文件

然后国际化它,get info。。

在下面生成的语言文件里写上cfbundledisplayname="中文名";

然后编辑info.plist,添加一个新的属性application has localized display name, 设置其类型为boolean,并将其value设置为选中状态。

在程序中使用字符串的地方可以调用nslocalizedstring来取出对应语言的字符串

如果你的strings文件不是标准的localizable.strings,假设叫my.strings 则可以用

nslocalizedstringfromtable (@"button",@"my", nil);来取得字符串

继续阅读