NSLocalizedString 作为 NSDictionary 的键

发布于 2024-12-11 12:08:38 字数 990 浏览 0 评论 0原文

我在这个论坛中读到可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-12-18 12:08:38

在模拟器中构建并运行您的应用程序。在 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文