iOS开发之中级iOS Developer

国际化和本地化(iOS开发)

2017-05-01  本文已影响53人  asaBoat

可用项目本文花费半天时间,如果喜欢请给个

参考来源

背景

对很多开发者来说,国际市场是事后的想法,但由于App Store提供了无缝的全球分享模式,任何iOS开发者都可以通过一键点击把应用程序发布至超过150个国家的市场。亚洲和欧洲代表了潜在客户不断增长的市场,这两个市场中很多人都不是以英语为母语,但是为了让应用充分利用市场的潜力,你至少要把应用语言国际化。

注意:国际化另一个重要的方面是使用Auto Layout改变文本的大小。不过为了让本教程尽可能地简单,我们不会主要关注Auto Layout。对于Auto Layout这个话题,我们另有其他教程

国际化和本地化

我们开始本地化实施(xib)

观察发现本地化的核心文件是.string文件,现在我只是做了对xib文件的支持(接下来对sting文件内部进行分析,对于xib这里只研究Main.Storyboard)

本地化的字符串函数(代码)

使用Foundation框架本地化字符串函数宏获取制定区域的本地化字符串。

NSLocalizedString(<#key#>, <#comment#>)
NSLocalizedStringFromTable(<#key#>, <#tbl#>, <#comment#>)
NSLocalizedStringFromTableInBundle(<#key#>, <#tbl#>, <#bundle#>, <#comment#>)
NSLocalizedStringWithDefaultValue(<#key#>, <#tbl#>, <#bundle#>, <#val#>, <#comment#>)
#define NSLocalizedString(key, comment) \
        [NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
        [NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
        [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
        [bundle localizedStringForKey:(key) value:(val) table:(tbl)]        

关键方法还是如下

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName;
参数 描述
key The key for a string in the table identified by tableName.
value The value to return if key is nil or if a localized string for key can’t be found in the table.
tableName The receiver’s string table to search. If tableName is nil or is an empty string, the method attempts to use the table in Localizable.strings.
  1. tableName 指的是.string文件
  1. 在这里我发现了Localizable.strings,它是所有没有制定table或者table不存在的默认table文件。

创建 Localizable.strings

本地化11.png

一定要为文件命名为Localizable.strings(重要性已经说过),然后操作如下:


本地化12.png

之后可以继续操作


本地化13.png

语言和地区

在iOS中,数字格式化是基于地区/国家,而不是语言。
美国人的100万是“1,000,000″, 但在西班牙,100万写作“1.000.000″。我们通过General -> Lauguge&Region -> Region设置

  1. 在MainStoryboard添加Label控件

    这个控件用于处理语言和数字的本地化

  2. 在Localizable.strings(Spanish)中添加

    "Yesterday you sold %@ apps" = "Ayer le vendió %@ aplicaciones";
    // 也可以加入其它键值
    "You like?" = "~Es bueno?~";
    
  3. 在Localizable.strings(English)中添加

"Yesterday you sold %@ apps" = "Yesterday you sold %@ apps(English)";
"You like?" = "You like?(English)";
```

  1. 在ViewController的ViewDidLoad中正式用代码实现

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *numberString = [numberFormatter   stringFromNumber:@(1000000)];
    self.numberTextLabel.text = [NSString   stringWithFormat:NSLocalizedString(@"Yesterday you  sold %@ apps", nil), numberString];
    
  2. 测试结果


    本地化14.png

    修改模拟器设置后运行模拟器


    本地化15.png

本地化图片

思路是通过本地化图片名称,用代码加载不同的图片

本地化Info.plist文件

本地化相关文件,方法和Localizable.strings一样创建。
可以在里边用字符串覆盖其他语言,为给应用程序一个不同的语名字(例如西班牙)。
"CFBundleDisplayName" = "Me Gusta";


本地化16.png
上一篇 下一篇

猜你喜欢

热点阅读