Xcode 4.6 localization within plist file

后端 未结 1 2030
我寻月下人不归
我寻月下人不归 2020-12-28 10:45

I have started adding more languages to a project of mine and got strings & graphics localized without much trouble.

I have one last problem and it is with a pli

相关标签:
1条回答
  • 2020-12-28 11:07

    Localized Plist files

    Easier solution here would be to localize the entire plist. By doing so, you will have a different plist file for each supported language.

    Select the plist file in your project, and select Localize in the File Inspector menu.

    File localization image

    It will create a new folder containing a Plist file for each supported language.

    From:

    dummy.plist

    To:

    > en.lproj
    >  >  dummy.plist
    > es.lproj
    >  >  dummy.plist
    > de.lproj
    >  >  dummy.plist
    

    Localized Plist contents

    Another solution would be to use localized strings inside the plist, and simply call NSLocalizedString before printing out the extracted string.

    Imagine you had a Plist like this: Original plist

    You can simply localize its strings by adding the keys to your Localizable.strings file. For example, in Spanish:

    "My menu title" = "Mi título del menú";
    "My menu description" = "Mi descripción del menú";
    

    Or, my recommendation, move also your native language strings out of the Plist to a string file and replace the Plist strings with a localizable key: Localized plist contents

    And your Localizable.strings for Engligh:

    "MY_MENU_TITLE" = "My menu title";
    "MY_MENU_DESCRIPTION" = "My menu description";
    

    and Spanish:

    "MY_MENU_TITLE" = "Mi título del menú";
    "MY_MENU_DESCRIPTION" = "Mi descripción del menú";
    

    I've found the latest easier to maintain and easier to localize for new languages, as all the required strings are in the same file.

    And finally change your code to use NSLocalizableString instead of the plain string read from the Plist file. For example, imagine you have the code:

    NSDictionary* plistDict = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menuElements" ofType:@"plist"]];
    
    menuTitleLabel.text = plistDict[@"menuTitle"];
    menuDescriptionLabel.text = plistDict[@"menuDescription"];
    

    Simply change it to:

    NSDictionary* plistDict = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"menuElements" ofType:@"plist"]];
    
    menuTitleLabel.text = NSLocalizedString(plistDict[@"menuTitle"], nil);
    menuDescriptionLabel.text = NSLocalizedString(plistDict[@"menuDescription"], nil);
    

    If this is your case you could get rid of the plist file completely:

    menuTitleLabel.text = NSLocalizedString(@"MY_MENU_TITLE", nil);
    menuDescriptionLabel.text = NSLocalizedString(@"MY_MENU_DESCRIPTION", nil);
    
    0 讨论(0)
提交回复
热议问题