Multiple Localizable.strings files in one iOS app

前端 未结 4 1476
余生分开走
余生分开走 2020-12-24 05:17

Is it possible to have multiple components that have their separate localizations in an app?

For example I want to localize strings in my app but also use ShareKit w

相关标签:
4条回答
  • 2020-12-24 06:00

    There is a way to do this that is quite simple.

    There are several macros for localizing, one of which is NSLocalizedStringFromTable(). This takes three parameters:

    1. The string to localize
    2. The table name, which is just a file name
    3. The comment, just like in NSLocalizedString()

    If you use a table name then you can have another strings file, i.e. if I do NSLocalizedStringFromTable(@"name", @"MyStrings", @"whatever"); then put my strings in MyStrings.strings it will use that.

    See Matt Gallagher's post about this.

    0 讨论(0)
  • 2020-12-24 06:02

    It is not possible to do what you are asking. You can have multiple Localizable.strings files, but only one per language in their respective .lproj folders.

    I understand that you do not want to edit the ShareKit Localizable.strings files because that would be a pain when updating but I think you need to look at how much work it would actually take. Depending on how many languages you need to support it may be less work to localize your own strings and add them to the bottom of the ShareKit Localizable.strings files than to implement your own localization scheme.

    BTW, to detect what language the device is currently set to you can use:

    NSArray *preferedLocalizations = [[NSBundle mainBundle] preferredLocalizations];
    

    This gives you an array of two letter language code strings. The item at index 0 is the currently selected language. This could be helpful if you need to make logic decisions based on the language.

    0 讨论(0)
  • 2020-12-24 06:19

    every file/resource can be localizable, also the IB .xib files or images jpg/png, you just need to select a file in your project, open info, go in the "general" tab/section and press the "make file localizable"... then you can add any language you want, xCode will create a new file duplicating the first one and will put it in the right folder...

    luca

    0 讨论(0)
  • 2020-12-24 06:19

    oh ok, I don't know ShareKit, but i guess you cannot edit its text files and just add your new words to that files, isn't it?

    then you may consider to create your own "dictionary" file and try to localize it, then read it and put it in a NSDictionary where you can read the single words/value... you can insert a record in your file "myDict.plist" with key "greeting" of type String with value "ciao" for the italian one and "hallo" in the english one

    NSString *bundle = [[ NSBundle mainBundle] pathForResource:@"myDict" ofType:@"plist"];
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:bundle];
    NSString *greetingsTranslatedInTheGoodLanguage = [savedStock objectForKey:@"greeting"];
    // just to try what you have read:
    NSLog (@"translated by your own dictionary: %@", greetingsTranslatedInTheGoodLanguage);
    [savedStock release];
    

    luca

    ps read the comments too...

    0 讨论(0)
提交回复
热议问题