2012年9月6日

[iPhone]多國語言的支援

首先在Resource中增加一個Strings檔案,
以Xcode4.4來說(New File> Resource > Strings File),
新增完成之後,在Localization中點選"Make localized..."的button,選擇要新增的語言,

在String 檔案中,輸入key及對應語言的值


"Configuration" = "設定";
"More" = "更多";



在要顯示字串的地方使用

NSLocalizedString(@"Configuration", nil);

就會顯示出目前iPhone設定語言對應的String

若要拿到指定國家(以繁體中文為例)的字串則使用

NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:
@"zh-Hant" ofType:@"lproj"]];
先取得bundle,再使用NSLocalizedStringFromTableInBundle取得該國家的字串
NSLocalizedStringFromTableInBundle(@"Configuration", nil, bundle, nil);