可用項目本文花費半天時間,如果喜歡請給個
背景
對很多開發(fā)者來說,國際市場是事后的想法,但由于App Store提供了無縫的全球分享模式,任何iOS開發(fā)者都可以通過一鍵點擊把應用程序發(fā)布至超過150個國家的市場。亞洲和歐洲代表了潛在客戶不斷增長的市場,這兩個市場中很多人都不是以英語為母語,但是為了讓應用充分利用市場的潛力,你至少要把應用語言國際化。
注意:國際化另一個重要的方面是使用Auto Layout改變文本的大小。不過為了讓本教程盡可能地簡單,我們不會主要關注Auto Layout。對于Auto Layout這個話題,我們另有其他教程。
國際化和本地化
-
國際化是一項你和開發(fā)者通過利用系統(tǒng)提供的API來實現的活動,并在代碼上做一些補充和修改,從而讓應用的中文版、阿拉伯語版本和英文版一樣好。簡單說,國際化是一個應用程序國際兼容性設計的過程,比如:
- 以用戶母語處理文本輸入和輸出;
- 處理不同的日期、時間以及數字格式;
- 利用適當的歷法和時區(qū)處理數據;
本地化(相比之下)僅僅是把應用的用戶界面和資源翻譯成不同的語言,這是你可以也應該交給別人做的工作,除非你能精通app應該支持的每種語言。
我們開始本地化實施(xib)
-
添加項目語言支持
本地化0.png我這里選擇了Spanish(es)
本地化1.png -
觀察項目變化
本地化2.png
觀察發(fā)現本地化的核心文件是.string文件,現在我只是做了對xib文件的支持(接下來對sting文件內部進行分析,對于xib這里只研究Main.Storyboard)
-
在Main.Storyboard中拖入控件UIlabel,然后按照上面的步驟添加語言支持
本地化3.png韓語本地化sting文件
本地化4.png之前默認添加的英語string文件(和西班牙語一樣內容為空)
本地化5.png -
更新Main.Storyboard中的english.string和spanish.string文件
本地化6.png
這里的文件內容都變成了一樣。
本地化7.png -
我們這里稍微修改一下MainStoryboard的string文件內容。
本地化8.png -
用模擬器運行程序
本地化9.png
切換語言系統(tǒng)
本地化10.png
本地化的字符串函數(代碼)
使用Foundation框架本地化字符串函數宏獲取制定區(qū)域的本地化字符串。
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. |
- tableName 指的是.string文件
- 在這里我發(fā)現了Localizable.strings,它是所有沒有制定table或者table不存在的默認table文件。
創(chuàng)建 Localizable.strings

一定要為文件命名為Localizable.strings(重要性已經說過),然后操作如下:

之后可以繼續(xù)操作

語言和地區(qū)
在iOS中,數字格式化是基于地區(qū)/國家,而不是語言。
美國人的100萬是“1,000,000″, 但在西班牙,100萬寫作“1.000.000″。我們通過General -> Lauguge&Region -> Region設置
-
在MainStoryboard添加Label控件
這個控件用于處理語言和數字的本地化
-
在Localizable.strings(Spanish)中添加
"Yesterday you sold %@ apps" = "Ayer le vendió %@ aplicaciones"; // 也可以加入其它鍵值 "You like?" = "~Es bueno?~"; -
在Localizable.strings(English)中添加
"Yesterday you sold %@ apps" = "Yesterday you sold %@ apps(English)";
"You like?" = "You like?(English)";
```
-
在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]; -
測試結果
本地化14.png
修改模擬器設置后運行模擬器
本地化15.png
本地化圖片
思路是通過本地化圖片名稱,用代碼加載不同的圖片
本地化Info.plist文件
本地化相關文件,方法和Localizable.strings一樣創(chuàng)建。
可以在里邊用字符串覆蓋其他語言,為給應用程序一個不同的語名字(例如西班牙)。
"CFBundleDisplayName" = "Me Gusta";
本地化16.png













