6 Replies
      Latest reply: Oct 3, 2016 1:12 AM by eskimo RSS
      wpreetham Level 1 Level 1 (0 points)

        To support Action extension, have created an embedded framework so that all common code can be there. Have moved Localizable.strings to embedded framework so that have single locazaiton file for both app and extension. But when tested I only see english (development langage ) being used.  I am using following call.

        NSLocalizedString(key, bundle: <bundle>, comment: <comment>)

        • Re: Localization not working when used in embeded framework
          eskimo Apple Staff Apple Staff (7,845 points)

          In general NSLocalizedString works in framework code.  The most obvious gotcha is that you have to pass in the right value for the bundle parameter.  The standard approach for this is to call +bundleForClass:, so, in Swift:

          NSBundle(forClass: self.dynamicType)
          

          Are you sure you’re doing that correctly?

          The other thing to watch out for is that the system tries to make sure that your app is consistently localised.  So, if you have a framework contain English and German localisations, and you call it from an app that only contains an English localisation, the framework will use the English resources.  If things didn’t work that way you run the risk of having half the app displaying English and the other half displaying German, which is clearly not good.

          So, make sure that the app you’re using to test your framework is localised appropriately.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Localization not working when used in embeded framework
              Macho Man Randy Savage Level 3 Level 3 (355 points)

              I was just about to ask this same question.

               

              I wrapped a tiny little open source component in a framework...which at a certain point displays an alert. The open source component for this is localized, so I went ahead and set up those files on the framework project.

               

              Now in my main app when I present the alert (coming out of the framework) the alert is english (i'm testing with french). I used:

               

              NSString *strannggg =   NSLocalizedStringFromTableInBundle(@"No, Thanks",
                                                                         @"AppiraterLocalizable",
                                                                         [NSBundle bundleForClass:[self class]],
                                                                         @"");
              
              
              

               

              Thought I was going crazy for a bit...so I just tried moving the localization file to another test project (not in a framework) and copied and pasted that same code. Xcode forces me to set up localization for the storyboard when I do this..

               

              In the test project, I get french (when I NSlog the string out). I'm not going to localize my entire storyboard file  because I don't have the money to pay translators, but i'd like to let the alert use the localized text. Is there no way to achieve this without localizing the storyboard?

                • Re: Localization not working when used in embeded framework
                  eskimo Apple Staff Apple Staff (7,845 points)

                  … but i'd like to let the alert use the localized text.

                  Why?  This will result in an app that presents half of its UI in one language and half in another.  That seems like a weird goal to me.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Localization not working when used in embeded framework
                      lightbow Level 1 Level 1 (10 points)

                      What's the recommended workaround then to handle localization for apps where most of their code/resources live in a shared framework? I recently refactored the majority of my app code into a shared framework to handle core functionality and resources, with the iOS / tvOS / watchOS / macOS apps and various app extensions all depending on that framework.  The framework has thousands of words localized into 17 languages, but none of those strings apply just to one app, so I'd prefer to keep all my localized strings in just that framework.

                       

                      I could do a fake app-level Localizable.strings with just a "hello world" in there for all my supported languages, but I'd love to know if there's a better way, or some checkbox I messed in Xcode somewhere that would be a cleaner solution. I try to keep adding languages as soon as I gain users in that demographic, and it'll be a pain to go through each app / extension target and add fake localizations each release just to get the framework loc calls to return non-English words.

                       

                      It seems unfortunate to have a silently-failing call to [myAwesomeValidatedNonNilFrameworkBundleWithAllMyLocalizedStrings localizedStringForKey:@"Something I know exists" value:@"" table:nil] always return English when those localized values definitely exist. Any tips?

                        • Re: Localization not working when used in embeded framework
                          lightbow Level 1 Level 1 (10 points)

                          I can confirm that my app localization starts working again if I just make a dummy (empty) "Passthrough.strings" file, and check off all the language checkboxes that I had in my shared framework. After that, calls to NSLocalizedStringFromTableInBundle return the properly-localized phrases. I'd be interested in hearing any downsides to this workaround, or any suggestions of a maintenance-free workaround.

                          • Re: Localization not working when used in embeded framework
                            eskimo Apple Staff Apple Staff (7,845 points)

                            If your app has localisations that aren’t obviously based on the app’s xxx.lproj resources — in your case this is because the localisations are in your framework — you can tell the OS about those localisations via the CFBundleLocalizations key in your Info.plist.

                            Share and Enjoy

                            Quinn “The Eskimo!”
                            Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                            let myEmail = "eskimo" + "1" + "@apple.com"