简体   繁体   English

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

[英]Iphone sdk - Get Localized text from specific localized.strings file

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. 是否有可能从特定的localized.strings文件中获取本地化字符串,而不是从系统选择的localized.strings文件中获取,只有一次。 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. 但是来自阿根廷的用户以及使用英语的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参数会起作用,但它没有找到文件的名称而不是语言。

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: 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)

Perhaps you want to use NSBundle's localizedStringForKey:value:table: rather than NSLocalizedString() . 也许你想使用NSBundle的localizedStringForKey:value:table:而不是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 ;-). 顺便说一句,不要忘记你的@在Objective-C字符串前面;-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM