10

Is it possible to get a localized string from a specific localized.strings file, rather than from the system chosen localized.strings file, ONLY ONE TIME. I do not need to change all the localized texts, only some of them.

What I want to do is to have localized strings defined from language preferences but also localization. So that a user from Brazil location with English lang will get 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 iPhone in English will get the application in English but some texts will be in Spanish.

Something like

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

I thought that sending that to the table parameter would work but it didn't as it looks for the name of the file and not for the language.

htafoya
  • 18,261
  • 11
  • 80
  • 104

3 Answers3

8

You can use the different bundle to choosing 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)
kelin
  • 11,323
  • 6
  • 67
  • 104
SoVa_
  • 379
  • 4
  • 12
  • 1
    This is brilliant – I don't know why it doesn't have more upvotes. Much easier than all the other suggested methods. I'm using it to get the version of a string in the development language, so I use `[[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];` – deltacrux Apr 07 '17 at 07:54
  • This is the top answer, I tested it and it works! Added Swift 3.0 version. – kelin Jul 19 '17 at 20:45
3

Perhaps you want to use NSBundle's localizedStringForKey:value:table: rather than NSLocalizedString(). This method would give you the opportunity to specify a different table.

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

BTW, do not forget your @ in front of objective-C strings ;-).

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Yes I meant that, but the Table parameter is a .strings file name, so I need to have two additional files pt.strings and es.strings, rather than using the wanted Localizable.strings file. Not a problem to implement but does not answer my question though. – htafoya Jun 01 '12 at 15:18
1

Did you mean NSLocalizedStringFromTable?

documented here: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#jumpTo_112

Dad
  • 6,388
  • 2
  • 28
  • 34
  • 1
    Yes I meant that, but the Table parameter is a .strings file name, so I need to have two additional files pt.strings and es.strings, rather than using the wanted Localizable.strings file. – htafoya Jun 01 '12 at 15:17
  • Your question wasn't so clear then because it seemed to ask for how to get it from a different file. "Is it possible to get a localized string from a specific file, rather than from the predefined localized.strings file." – Dad Jun 03 '12 at 00:34