NSLocalizedString as key of NSDictionary

做~自己de王妃 提交于 2019-12-23 03:36:13

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/7860909/nslocalizedstring-as-key-of-nsdictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!