關(guān)于國際化已經(jīng)有很多博文杂数,就是使用Localizable.strings文件和
NSLocalizedString函數(shù)矛绘,根據(jù)不同的系統(tǒng)語言環(huán)境,會使用不同的語言文本货矮。
但是這樣要修改系統(tǒng)設(shè)置才能修改語言,如果需要在App內(nèi)修改語言囚玫,就要自己去指定語言文件了。
下面定義一個宏來指定使用的語言文件抓督。
#define LSTR(STRING, LOC) CustomLocalizedString((STRING), @"", LOC)
#define CustomLocalizedString(key, comment, LOC) [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:LOC ofType:@"lproj"]] localizedStringForKey:key value:@"" table:nil]
說明一下铃在,這里的LOC就是語言文件的名字阵具,是設(shè)置了國際化之后系統(tǒng)生成的
en:英語
zh-Hans:簡體中文
zh-Hant:繁體中文
一個Demo:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_simpleLabel.text = LSTR(@"test_text", @"zh-Hans");
_traditionalLabel.text = LSTR(@"test_text", @"zh-Hant");
_enLabel.text = LSTR(@"test_text", @"en");
_simpleLabelB.text = LSTR(@"test_textB", @"zh-Hans");
_traditionalLabelB.text = LSTR(@"test_textB", @"zh-Hant");
_enLabelB.text = LSTR(@"test_textB", @"en");
}
由于系統(tǒng)默認一定要使用Localizable.strings文件阳液,改了名字也不行(除非指定table帘皿,但是這樣用起來就要加上table名),此時文件內(nèi)容應(yīng)該是這樣的(簡體中文為例):
"test_text" = "測試文本";
"test_textB" = "測試文本B";
那如果當項目代碼越來越多鹰溜,所有模塊的國際化文本都會摻雜在一起,十分難管理曹动,這時候就需要對Localizable.strings進行分割。我找不到系統(tǒng)提供的配置方法仁期,硬要用這個死名字。
所以這里我把文本分割到新建的strings文件中,在使用python腳本痊硕,在項目build的時候清空Localizable.strings文件,再依次讀入模塊的strings文件岔绸,寫到Localizable.strings中,解決了這個問題晋被。
這里分了a.strings和b.strings出來:
依然是簡體中文為例
a.strings:
/*
a.strings
HVWSubLocalizableStrings
Created by SimonHuang on 16/8/7.
Copyright ? 2016年 hellovoidworld. All rights reserved.
*/
"test_text" = "測試文本";
b.strings:
/*
b.strings
HVWSubLocalizableStrings
Created by SimonHuang on 16/8/7.
Copyright ? 2016年 hellovoidworld. All rights reserved.
*/
"test_textB" = "測試文本B";
python腳本:
在targets中創(chuàng)建一個Run Script羡洛,優(yōu)先級排到上面
代碼在這里:
#!/usr/bin/python
#-*-coding:utf-8-*-
import os
import fnmatch
for parent, folders, files in os.walk("./HVWSubLocalizableStrings/Localizable"):
if fnmatch.fnmatch(parent, "*.lproj"):
#clear Localizable.strings first
try:
localizableStrings = open(parent + "/Localizable.strings", "w")
localizableStrings.truncate()
except:
pass
finally:
localizableStrings.close()
try:
localizableStrings = open(parent + "/Localizable.strings", "a")
#load other .strings files
for file in files:
if fnmatch.fnmatch(file, "*.strings") and file != "Localizable.strings" and file != "InfoPlist.strings":
try:
moduleStrings = open(parent + "/" + file, "r")
localizableStrings.write("\n")
localizableStrings.write(moduleStrings.read())
except:
pass
finally:
moduleStrings.close()
except:
pass
finally:
localizableStrings.flush()
localizableStrings.close()
print "Generate Localizable.strings done"
build完之后欲侮,Localizable.strings文本的內(nèi)容就生成了。
/*
a.strings
HVWSubLocalizableStrings
Created by SimonHuang on 16/8/7.
Copyright ? 2016年 hellovoidworld. All rights reserved.
*/
"test_text" = "測試文本";
/*
b.strings
HVWSubLocalizableStrings
Created by SimonHuang on 16/8/7.
Copyright ? 2016年 hellovoidworld. All rights reserved.
*/
"test_textB" = "測試文本B";