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的值。

我有三个本地化(意大利语,法语,西class牙语)。 我生成并翻译了所有“Localizable.strings”文件(在它的.lproj,fr.lproj和es.lproj文件夹中),但是当执行应用程序时,我只看到主要的翻译,例如:阈值0,阈值1。 ..

我在哪里做错了?

在模拟器中构build并运行您的应用程序。 在Finder中打开〜/ Library / Application Support / iPhoneSimulator / your-ios-version / Applications / your-app-id /。

然后右键单击.app文件并select“显示包内容”,查看本地化的string是否属于它们的位置。

本地化资源很可能不属于您的目标。 设备也是区分大小写的,所以“Localizable.strings”是正确的,而“localizable.strings”不是。

如果文件存在,但仍然不能正常工作,请打开BBEdit中的文件,确保编码是little-endian UTF-16。 (或者你可以使用Terminal.app并input“file path-to-strings-file”来查看编码types)。