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 之间的值
。我有三种本地化版本(意大利语、法语、西班牙语)。我生成并翻译了所有“Localized.strings”文件(在 it.lproj、fr.lproj 和 es.lproj 文件夹中),但是当应用程序执行时,我只看到主要翻译,例如: Threshold 0、Threshold 1、. ..
我哪里做错了?
I read in this forum that is possible to use a NSLocalizedString as key of a 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];
This is the code trying to access the NSDictionary:
NSString *key = [NSString stringWithFormat: @"threshold_%d",{MY_VARIABLE}];
NSString *text = [LABELS objectForKey: key];
Where {MY_VARIABLE} can hold values from 0 to 4.
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, ...
Where I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模拟器中构建并运行您的应用程序。在 Finder 中打开 ~/Library/Application Support/iPhoneSimulator/your-ios-version/Applications/your-app-id/。
然后右键单击 .app 文件并选择“显示包内容”并查看本地化字符串是否位于其所属位置。
本地化资源很可能不属于您的目标。设备也区分大小写,因此“Localized.strings”是正确的,而“localized.strings”则不正确。
如果文件存在但仍无法正常工作,请在 BBEdit 中打开文件并确保编码为小端 UTF-16。 (或者,您可以使用 Terminal.app 并输入“文件路径到字符串文件”来查看编码类型)。
Build and run your app in the simulator. In the Finder open ~/Library/Application Support/iPhoneSimulator/your-ios-version/Applications/your-app-id/.
Then right-click the .app file and select "Show Package Contents" and see if the localized strings are where they belong.
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.
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).