Multiple Localized .strings Files in iOS App Bundle

前端 未结 5 1229
清酒与你
清酒与你 2020-12-09 13:07

I have a fairly complicated project, consisting of several large localized sub-projects.

Most of my sub-projects are localized through a single Localizable.s

5条回答
  •  孤城傲影
    2020-12-09 13:49

    I was able to reproduce and fix the issue, though the solution does imply there's a bug in NSBundle.

    I reproduced it with the following bundle structure:

    MyApp.app/
     |
      - MyResources.bundle/
          |
           - en.lproj/
          |
           - fr.lproj/
    

    and code:

      NSLog(@"A key: %@", NSLocalizedString(@"A key", nil));
      NSBundle *bundle = [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource: @"MyResources" ofType: @"bundle"]];
      NSLog(@"Current locale: %@", [[NSLocale currentLocale] localeIdentifier]);
      NSLog(@"Bundle localizations: %@", [bundle localizations]);
      NSLog(@"Key from bundle: %@", [bundle localizedStringForKey: @"A key" value: @"Can't find it." table: nil]);
      NSLog(@"Key using bundle macro: %@", NSLocalizedStringFromTableInBundle(@"A key",
                                                                                 nil,
                                                                                 bundle,
                                                                                 nil));
    

    With the locale set to fr_FR (i.e. French) the bundle picked the string from the English strings table - even the string "can't find it" doesn't appear.

    Without changing the code, I was able to get the French string using the following structure for the bundle instead:

    MyApp.app/
     |
      - MyResources.bundle/
          |
           - Resources/
              |
               - en.lproj/
              |
               - fr.lproj/
    

    It looks like NSBundle still expects the old Mac OS X bundle structure instead of what iOS is supposed to use. So a simple change of bundle structure should solve the problem...

提交回复
热议问题