Iphone sdk - Get localized text from a specific localized.strings file

Is it possible to get a localized string from a specific localized.strings file, and not from the selected localized.strings file, ONLY ONCE. I do not need to change all localized texts, only some of them.

I want localized strings to be determined from language preferences, but also for localization. For a user from Brazil with English to receive the application in English, but some texts will be specific to the region, so I want them in Portuguese.

But a user from Argentina, also with an iPhone in English, will receive the application in English, but some texts will be in Spanish.

Something like

NSLocalizedStringFromTable("string.key","pt_BR",nil) 

I thought sending this parameter to table would work, but this is not the case, as it searches for the file name, not the language.

+7
source share
3 answers

You can use a different set to select a specific language:

 NSString * path = [[NSBundle mainBundle] pathForResource:@"pt-PT" ofType:@"lproj"]; NSBundle * bundle = nil; if(path == nil){ bundle = [NSBundle mainBundle]; }else{ bundle = [NSBundle bundleWithPath:path]; } NSString * str = [bundle localizedStringForKey:@"a string" value:@"comment" table:nil]; 

Swift 3.0:

 extension Bundle { static let base: Bundle = { if let path = Bundle.main.path(forResource: "Base", ofType: "lproj") { if let baseBundle = Bundle(path: path) { return baseBundle } } return Bundle.main }() } let localizedStr = Bundle.base.localizedString(forKey: key, value: nil, table: table) 
+4
source

Maybe you want to use NSBundle localizedStringForKey:value:table: rather than NSLocalizedString() . This method will give you the opportunity to specify a different table.

 [[NSBundle mainBundle] localizedStringForKey:@"stringKey" value:defaultString table:tableName]; 

By the way, don't forget your @ before the objective-C lines; -).

+3
source

All Articles