2011-10-22 99 views
1

我在這個論壇上讀到,可以使用NSLocalizedString作爲NSDictionary的關鍵。NSLocalizedString作爲NSDictionary的關鍵

這是我的NSDictionary:

LABELS = [[NSDictionary alloc] initWithObjectsAndKeys: 
NSLocalizedString(@"Threshold 0", @"Description for threshold 0") , @"threshold_0", 
NSLocalizedString(@"Threshold 1", @"Description for threshold 1"), @"threshold_1", 
NSLocalizedString(@"Threshold 2", @"Description for threshold 2"), @"threshold_2", 
NSLocalizedString(@"Threshold 3", @"Description for threshold 3"), @"threshold_3", 
NSLocalizedString(@"Threshold 4", @"Description for threshold 4"), @"threshold_4", 
      nil]; 

這是代碼試圖訪問的NSDictionary:

NSString *key = [NSString stringWithFormat: @"threshold_%d",{MY_VARIABLE}]; 
NSString *text = [LABELS objectForKey: key]; 

其中{} MY_VARIABLE可容納從0值到4

我有三個本地化(意大利,法國,西班牙)。我生成並翻譯了所有「Localizable.strings」文件(在它的.lproj,fr.lproj和es.lproj文件夾中),但是當執行應用程序時,我只能看到主要的翻譯,例如:閾值0,閾值1。 ..

我在哪裏做錯了?

回答

0

在模擬器中構建並運行您的應用程序。在Finder中打開〜/ Library/Application Support/iPhoneSimulator/your-ios-version/Applications/your-app-id /。

然後右鍵單擊.app文件並選擇「顯示包內容」,查看本地化的字符串是否屬於它們的位置。

最有可能的本地化資源不是您的目標的一部分。這些設備也是區分大小寫的,所以「Localizable.strings」是正確的,而「localizable.strings」則不是。

如果文件存在但它仍然不能正常工作,請打開BBEdit中的文件並確保編碼是little-endian UTF-16。 (或者你可以使用Terminal.app並輸入「file path-to-strings-file」來查看編碼類型)。

+0

謝謝你的回答。問題其實是我忘了將文件Localizable.strings添加到項目中。下次我會更聰明:)。 – MarcoSciamanna