Iphone sdk - 从特定的localized.strings文件中获取本地化文本

时间:2021-09-21 10:44:59

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.

是否有可能从特定的localized.strings文件中获取本地化字符串,而不是从系统选择的localized.strings文件中获取,只有一次。我不需要更改所有本地化文本,只需更改其中一些。

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.

但是来自阿根廷的用户以及使用英语的iPhone将获得英语应用程序,但有些文本将使用西班牙语。

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.

我认为将其发送到table参数会起作用,但它没有找到文件的名称而不是语言。

3 个解决方案

#1


6  

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)

#2


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的localizedStringForKey:value:table:而不是NSLocalizedString()。此方法将为您提供指定其他表的机会。

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

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

顺便说一句,不要忘记你的@在Objective-C字符串前面;-)。

#3


1  

Did you mean NSLocalizedStringFromTable?

你的意思是NSLocalizedStringFromTable吗?

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

记录在这里:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#jumpTo_112

#1


6  

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)

#2


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的localizedStringForKey:value:table:而不是NSLocalizedString()。此方法将为您提供指定其他表的机会。

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

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

顺便说一句,不要忘记你的@在Objective-C字符串前面;-)。

#3


1  

Did you mean NSLocalizedStringFromTable?

你的意思是NSLocalizedStringFromTable吗?

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

记录在这里:http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#jumpTo_112