【问题标题】:iPhone/iOS: How can I get a list of localized strings in all the languages my app is localized in?iPhone/iOS:如何获取我的应用本地化的所有语言的本地化字符串列表?
【发布时间】:2011-06-10 18:18:59
【问题描述】:

我需要向我的服务器发送特定字符串的本地化列表。

意思是,如果我的应用程序有一个字符串 Foo,它在英语中本地化为 @"Foo",在俄语中本地化为 @"Фу",我想向服务器发送如下列表:

  • 字符串 Foo:
    • 英文:“Foo”
    • 俄语:“Фу”

我认为我需要能够做的是:

  1. 为我的应用本地化的每种语言枚举本地化字符串
  2. 获取每种语言的 Foo 本地化版本

我该怎么做 (1) 以及我该怎么做 (2)?

【问题讨论】:

标签: iphone ios localization nslocalizedstring


【解决方案1】:

您可以通过将 English.lproj/Localizable.strings 作为字典读取并获取其键来检索所有字符串键:

NSString *stringsPath = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:stringsPath];

要获得每种语言的翻译,您可以遍历每个英文键的语言并使用NSLocalizedStringFromTableInBundle

for (NSString *language in [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]) {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]];
    NSLog(@"%@: %@", language, NSLocalizedStringFromTableInBundle(@"Testing", @"Localizable", bundle, nil));
}

【讨论】:

  • 这听起来很合理;我看到的其他选项需要重写我们检索字符串的方式,但这是侵入性较小的方式,看起来它可以满足我的需要。我今天试试看。
  • 请注意,在上面的代码示例中,第一次调用 pathForResource 将返回 current 本地化的路径,不一定是英文的(例如,如果您的模拟器/设备设置为德语,您将获得de.lproj/Localizable.strings的路径。
猜你喜欢
  • 2011-11-19
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多