Xcode 4: Framework localization does not work

I am developing my own infrastructure using Xcode 4, and I use it in two sample applications (console and Mac OS X Cocoa).

I am trying to add localization to the framework, so I created two versions of the Localizable.strings file (en and fr versions), but every time I try to print a localized string from sample applications, I get only its technical name. For example, with the following line inside the frame code:

 NSLog(NSLocalizedString(@"LOC_TEST", nil)); 

I get only "LOC_TEST" displayed on the output ...

Localization works great with the Cocoa application itself (meaning that the localized strings of the Cocoa application are shown accordingly).

Following this article , I tried to add localization to the plist framework file:

 <key>CFBundleLocalizations</key> <array> <string>en</string> <string>fr</string> </array> 

But that didn’t change anything ...

What am I missing?

+6
source share
4 answers

The reason for not choosing the right .strings file with the framework is the NSLocalizedString macro:

 #define NSLocalizedString(key, comment) [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] 

The platform location of the .strings file is not [NSBundle mainBundle] , as indicated in the macro.

From what I see, you will need to use NSLocalizedStringFromTableInBundle instead and indicate the location of the package structure.

+10
source

The accepted answer is correct, NSLocalizedString will not work using [NSBundle mainBundle] , so I use this macro in my structure

 #define LOCALIZED_STRING(key) [[NSBundle bundleForClass:[self class]] localizedStringForKey:(key) value:@"" table:nil] 
+2
source

The “technical name,” as you call it, is actually the key. Your .strings files will look something like this:

/ * no comment * / "LOC_TEST" = "LOC_TEST";

In en.lproj / Localizable.strings, replace the second LOC_TEST (the one after =) with your English string. Do the same in fr.lproj with your French text.

+1
source

Example:

put it in your .m file:

 #ifndef NSFrameworkLocalizedStrings #define NSFrameworkLocalizedStrings(key) \ NSLocalizedStringFromTableInBundle(key, @"YOUR_BUNDLE_NAME", [NSBundle bundleWithPath:[[[NSBundle frameworkBundle] resourcePath] stringByAppendingPathComponent:@"YOUR_BUNDLE_NAME.bundle"]], nil) #endif 

and use this method:

 + (NSBundle *)frameworkBundle { static NSBundle* frameworkBundle = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ frameworkBundle = [NSBundle bundleForClass:[self class]]; }); //NSLog(@"frameworkBundle %@",frameworkBundle); return frameworkBundle; } 

Enjoy √ http://www.LegoTechApps.com (Download Framework, which will save you time )

0
source

Source: https://habr.com/ru/post/888746/


All Articles