问题
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