NSLocalizedString作为NSDictionary的键 [英] NSLocalizedString as key of NSDictionary
问题描述
我在这个论坛上读到有可能将NSLocalizedString用作NSDictionary的键.
I read in this forum that is possible to use a NSLocalizedString as key of a NSDictionary.
这是我的NSDictionary:
This is my 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的代码:
This is the code trying to access the NSDictionary:
NSString *key = [NSString stringWithFormat: @"threshold_%d",{MY_VARIABLE}];
NSString *text = [LABELS objectForKey: key];
{MY_VARIABLE}可以容纳0到4之间的值.
Where {MY_VARIABLE} can hold values from 0 to 4.
我有三个本地化版本(意大利语,法语,西班牙语).我生成并翻译了所有"Localizable.strings"文件(在it.lproj,fr.lproj和es.lproj文件夹中),但是,当应用程序执行时,我只能看到主要的翻译,例如:Threshold 0,Threshold 1,.... ..
I have three localizations (italian, french, spanish). I generated and translated all the "Localizable.strings" files (in it.lproj, fr.lproj and es.lproj folders) but, when application executes, I see only the main translation, for example: Threshold 0, Threshold 1, ...
我在哪里做错了?
推荐答案
在模拟器中构建并运行您的应用.在Finder中打开〜/Library/Application Support/iPhoneSimulator/your-ios-version/Applications/your-app-id/.
Build and run your app in the simulator. In the Finder open ~/Library/Application Support/iPhoneSimulator/your-ios-version/Applications/your-app-id/.
然后右键单击.app文件,然后选择显示包内容",然后查看本地化的字符串是否属于它们.
Then right-click the .app file and select "Show Package Contents" and see if the localized strings are where they belong.
本地化资源很可能不属于您的目标.这些设备还区分大小写,因此"Localizable.strings"正确,而"localizable.strings"则不正确.
Most likely the localized resources weren't part of your target. The devices are also case sensitive, so "Localizable.strings" is correct while "localizable.strings" is not.
如果文件存在但仍无法使用,请在BBEdit中打开文件,并确保编码为小尾数UTF-16. (或者,您可以使用Terminal.app并键入"file path-to-strings-file"以查看编码类型.)
If the files are present but it's still not working, open the files in BBEdit and make sure the encoding is little-endian UTF-16. (Alternatively you can use Terminal.app and type "file path-to-strings-file" to see the encoding type).
这篇关于NSLocalizedString作为NSDictionary的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!