IOS基础学习(3)------国际化

来源:互联网 发布:傲剑升级数据大全2014 编辑:程序博客网 时间:2024/06/03 21:18


1、新建一个工程,新建一个Utils文件,继承自NSobjects ,添加方法:

@interface Utils : NSObject#pragma  mark - nowlanuages+(NSString *)nowlanguages;#pragma  mark - alllanguages+(NSArray *)alllanguages;@end

+(<span style="font-family: Arial, Helvetica, sans-serif;">NSArray *</span><span style="font-family: Arial, Helvetica, sans-serif;">)alllanguages</span>{    NSUserDefaults *defaults = [ NSUserDefaults standardUserDefaults ];    // 取得 iPhone 支持的所有语言设置    NSArray *languages = [defaults objectForKey : @"AppleLanguages" ];    NSLog ( @"languages:%@" , languages);}+(void)nowlanguages{    NSArray *languages = [NSLocale preferredLanguages];    NSString *currentLanguage = [languages objectAtIndex:0];    NSLog ( @"当前设备的语言环境currentLanguage:%@" , currentLanguage);}

在AppDelegate.m文件中调用两个方法,

#import "Utils.h"
<span style="color:#3333ff;"></span>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    [Utils alllanguages];    [Utils nowlanguages];    return YES;}<span style="color:#3333ff;"></span>
这样就可以得到设备的当前语言环境和设备所支持的语言环境。

然后设计界面,Storyboard界面如图所示:



2、新建一个FristController文件,继承自UITableViewController:


3、将FristController的Storyboard ID设置为:FristController


4、在FristController中引入tableview的delegate,并将tableview进行关联

@interface FristController ()<UITableViewDataSource,UITableViewDelegate>@property (strong, nonatomic) IBOutlet UITableView *tableview;@end

- (void)viewDidLoad{    [super viewDidLoad];    self.tableview.delegate=self;    self.tableview.dataSource=self;}

6、OK,现在更改设备的语言环境后,运行Demo,可以肯定的是,无论怎么修改设备的文字环境,也不会有变化的,因为现在并没有对整个工程进行国际化设置.

首先将工程设置国际化,如图所示:

7、根据同样的方法,选择工程支持中文简体、繁体、英文、日文:

8、然后,可以发现在工程中storyboard和InfoPlist.strings中多了四个文件,如图所示:


9、打开InfoPlist.strings文件,在InfoPlist.strings(English)中添加代码,

CFBundleDisplayName="Bearish's Demo";

InfoPlist.strings(Simplidied)中添加代码:

CFBundleDisplayName="国际化 Demo";
InfoPlist.strings(Japanese)中添加代码:
CFBundleDisplayName="がさつな Demo";

InfoPlist.strings(Traditional)中添加代码:

CFBundleDisplayName="國際化 Demo";

然后运行Demo,可以看到应用在桌面上的显示名字已经改变了,


10、打开Main_iPhone.storyboard.string(China(Simplidied))添加代码:

/* Class = "IBUIButton"; normalTitle = "Sign in"; ObjectID = "8Lv-aa-f0O"; */"8Lv-aa-f0O.normalTitle" = "登陆";/* Class = "IBUILabel"; text = "電子メール:"; ObjectID = "GUh-SA-XRx"; */"GUh-SA-XRx.text" = "邮箱:";/* Class = "IBUILabel"; text = "    再入力:"; ObjectID = "QD4-DE-gkG"; */"QD4-DE-gkG.text" = "    再次输入:";/* Class = "IBUILabel"; text = "パスワード:"; ObjectID = "zzR-J4-WRH"; */"zzR-J4-WRH.text" = "密码:";

在Main_iPhone.storyboard.string(China(Japanese))中添加代码:

/* Class = "IBUIButton"; normalTitle = "Sign in"; ObjectID = "8Lv-aa-f0O"; */"8Lv-aa-f0O.normalTitle" = "サインイン";/* Class = "IBUILabel"; text = "電子メール:"; ObjectID = "GUh-SA-XRx"; */"GUh-SA-XRx.text" = "電子メール:";/* Class = "IBUILabel"; text = "    再入力:"; ObjectID = "QD4-DE-gkG"; */"QD4-DE-gkG.text" = "    再入力:";/* Class = "IBUILabel"; text = "パスワード:"; ObjectID = "zzR-J4-WRH"; */"zzR-J4-WRH.text" = "パスワード:";


在Main_iPhone.storyboard.string(China(Traditional))中添加代码:

/* Class = "IBUIButton"; normalTitle = "Sign in"; ObjectID = "8Lv-aa-f0O"; */"8Lv-aa-f0O.normalTitle" = "登陸";/* Class = "IBUILabel"; text = "電子メール:"; ObjectID = "GUh-SA-XRx"; */"GUh-SA-XRx.text" = "郵箱:";/* Class = "IBUILabel"; text = "    再入力:"; ObjectID = "QD4-DE-gkG"; */"QD4-DE-gkG.text" = "    再次輸入:";/* Class = "IBUILabel"; text = "パスワード:"; ObjectID = "zzR-J4-WRH"; */"zzR-J4-WRH.text" = "密碼:";


在Main_iPhone.storyboard.string(China(English))中添加代码:

/* Class = "IBUIButton"; normalTitle = "Sign in"; ObjectID = "8Lv-aa-f0O"; */"8Lv-aa-f0O.normalTitle" = "Sign in";/* Class = "IBUILabel"; text = "電子メール:"; ObjectID = "GUh-SA-XRx"; */"GUh-SA-XRx.text" = "    E-mail:";/* Class = "IBUILabel"; text = "    再入力:"; ObjectID = "QD4-DE-gkG"; */"QD4-DE-gkG.text" = "  Re-enter:";/* Class = "IBUILabel"; text = "パスワード:"; ObjectID = "zzR-J4-WRH"; */"zzR-J4-WRH.text" = "  Password:";

OK运行Demo,此时我的模拟器为英文环境,运行效果如图:

11、打开ViewController.m导入头文件,

#import "FristController.h"

@interface ViewController ()@property (nonatomic, strong) FristController *fristController;@end

11、将按钮的点击函数命名为JumpBtn,完成点击事件:

self.fristController=[[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil]instantiateViewControllerWithIdentifier:@"FristController"];[self.navigationController pushViewController:self.fristController animated:YES];
12、数据:

(1)、本文中我的数据是从JSON中读取的,新建一个JSON文件,里面有四个数组,对应四种语言:

完成后将JSON文件导入工程,

(2)、JSON解析,存储

在AppDelegate.m问价中添加方法:然后在程序开始时调用:

-(void)readJson{    NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];    NSString *path=[mainBundleDirectory stringByAppendingPathComponent:@"tableviewdemo.json"];    NSURL *url=[NSURL fileURLWithPath:path];    NSData *data = [[NSData alloc] initWithContentsOfURL:url];    NSError *error = nil;    id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];    NSLog(@"======%@",userDefaults);    [userDefaults setObject:jsonObject forKey:@"json"];    [userDefaults synchronize];}


(3)、从JSON获取来的数据是为了显示在Tableview中,现在打开之前所建的Utils文件,有获取到当前设备的语言环境的方法,FristController.m文件中,添加方法:根据设备不同语言环境,从JSON中读取不同的数据:

#import "Utils.h"
<p class="p1">@property<span class="s1"> (</span>strong<span class="s1">, </span>nonatomic<span class="s1">) </span><span class="s2">NSMutableArray</span><span class="s1"> *Variablearray;</span></p>
-(void)parsingJson{    NSUserDefaults *Store_Update = [NSUserDefaults standardUserDefaults];    id jsonObject=[Store_Update objectForKey:@"json"];    if ([jsonObject isKindOfClass:[NSDictionary  class]])    {       NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:jsonObject];        if([[Utils nowlanguages] isEqualToString:@"en"])        {          self.dicarray=[dictionary objectForKey:@"EngName"];        }else if([[Utils nowlanguages] isEqualToString:@"ja"])        {            self.dicarray=[dictionary objectForKey:@"Name"];        }else if([[Utils nowlanguages] isEqualToString:@"zh-Hans"])        {            self.dicarray=[dictionary objectForKey:@"ChinaSName"];        }else if([[Utils nowlanguages] isEqualToString:@"zh-Hant"])        {            self.dicarray=[dictionary objectForKey:@"ChinaTName"];        }       self.Variablearray=[NSMutableArray arrayWithArray:self.dicarray];    }else    {        NSLog(@"ぶしで");    }}

(4)、调用tableview自带的delegate,将数据显示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellId=@"cellid";    BOOL B_addCell=(indexPath.row==self.Variablearray.count);    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];    if(cell==nil){        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];    }    if(!B_addCell)    {        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;        cell.textLabel.text=[self.Variablearray objectAtIndex:indexPath.row];            }else    {        NSLog(@"ばcううおl");    }    return cell;}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{#warning Potentially incomplete method implementation.    // Return the number of sections.    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{#warning Incomplete method implementation.    // Return the number of rows in the section.    return [self.Variablearray count];}
13、现在运行Demo,由图可以看出模拟器在英文环境下显示的内容:


Demo下载,请戳:http://download.csdn.net/detail/ningshuang520/8063987














0 0
原创粉丝点击