【问题标题】:NSLocalizedString as key of NSDictionaryNSLocalizedString 作为 NSDictionary 的键
【发布时间】:2011-10-22 16:28:08
【问题描述】:

我在这个论坛上读到可以使用 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”文件(在 it.lproj、fr.lproj 和 es.lproj 文件夹中),但是当应用程序执行时,我只看到主要翻译,例如:Threshold 0、Threshold 1、. ..

我哪里做错了?

【问题讨论】:

    标签: iphone ios localization nsdictionary


    【解决方案1】:

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

    然后右键单击 .app 文件并选择“显示包内容”并查看本地化字符串是否在它们所属的位置。

    本地化资源很可能不是您的目标的一部分。这些设备也区分大小写,因此“Localizable.strings”是正确的,而“localizable.strings”则不是。

    如果文件存在但仍无法正常工作,请在 BBEdit 中打开文件并确保编码为 little-endian UTF-16。 (或者,您可以使用 Terminal.app 并键入“文件路径到字符串文件”来查看编码类型)。

    【讨论】:

    • 感谢您的回答。问题实际上是我忘记将文件 Localizable.strings 添加到项目中。下次我会更聪明:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多