After deleting one language of the project, other existing localizable files in project are not correctly loaded. Files are included in the bundle resources, but they are not load correctly, the load method doesn't return any value ok the string keys included in the localizable.strings file.

enter image description here

Here is the load method:

-(NSString*) languageSelectedStringForKey:(NSString*) key
{

    NSString *path;

    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [currentDefaults objectForKey:@"AppleLanguages"];
    NSString *selectedLanguage = [languages objectAtIndex:0];

    if([selectedLanguage isEqualToString:@"es"])
        path = [[NSBundle mainBundle] pathForResource:@"es" ofType:@"lproj"];
    else if([selectedLanguage isEqualToString:@"en"])
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];

    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];

    path=nil;

    languages=nil;

    selectedLanguage=nil;

    languages=nil;

    languageBundle=nil;

    return str;

}

Many thanks for your help.

有帮助吗?

解决方案

In most cases, you can just use NSLocalizedString to look up the translation of a string. For example:

myLabel.text = NSLocalizedString(@"The String Key", @"Comment for translators");

To explore problems looking up these strings, I have two suggestions:

  • Double-check that the iPhone / simulator is set to the language you want to display. Yep, that should be obvious, but let's be on the safe side.
  • Run your app in the simulator, and log the following path:

    NSLog(@"Bundle path is: %@", [[NSBundle mainBundle] bundlePath]);

From there you can explore the file tree that your app is using to do language-specific lookups (use Finder or Terminal to check which files are in that directory). You should see directories that are directly in the above-logged path that look like {ios-language-code}.lproj and there should be a file called Localizable.strings in each of those directories (assuming you're using the default translation setup). For example, en.lproj for English and fr.lproj for French.

From there, I suggest verifying that these files are in the correct format, which is described here. It's also good to know about tools to work with these files, which are described here.

In case the file structure is not what you expect, one possibility is to check the Build Phases for your target in Xcode, and verify that Localization.strings is listed under the "Copy Bundle Resources" section.

I'm pretty sure that if you have the correct .lproj directories in your bundle, and if the correct .strings files are in those directories, then NSLocalizedString should work as expected -- so even if the above suggestions don't completely solve your problem, you can reduce the problem to getting your main bundle file structure fixed.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top