问题描述
我需要向我的服务器发送特定字符串的本地化列表。
I need to send my server a list of localizations for a particular string.
意思是,如果我的应用程序有一个字符串Foo,它本地化为@Foo在英语和@Фу俄语中,我想向服务器发送如下列表:
Meaning, if my app has a string Foo which is localized as @"Foo" in English and @"Фу" in Russian, I'd like to send the server a list such as this:
- String Foo:
- 英语:Foo
- 俄语:Фу
我认为我需要做的是:
- 枚举我的应用已本地化的每种语言的本地化字符串
- 为每种语言获取Foo的本地化版本
我如何做(1)以及如何做(2)?
How do I do (1) and how do I do (2)?
推荐答案
你可以通过将English.lproj / Localizable.strings作为字典读取并获取其键来检索所有字符串键:
You can retrieve all of the string keys by reading in English.lproj/Localizable.strings as a dictionary and fetching its keys:
NSString *stringsPath = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings"]; NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:stringsPath];
要获得每种语言的翻译,您可以迭代每个英语密钥的语言并使用
NSLocalizedStringFromTableInBundle
:To get each language's translation, you can iterate over the languages for each English key and use
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)); }
这篇关于iPhone / iOS:如何在我的应用程序本地化的所有语言中获取本地化字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!