iPhone / iOS:如何获取我的应用程序本地化的所有语言的本地化string列表?
我需要向我的服务器发送特定string的本地化列表。
意思是说,如果我的应用程序有一个stringFoo,它被本地化为@“Foo”英文和@“Фу”俄文,我想发送一个列表如下:
- stringFoo:
- 英语:“Foo”
- 俄语:“Фу”
我认为我需要做的是:
- 枚举我的应用程序本地化的每种语言的本地化string
- 获取每种语言的Foo的本地化版本
我该怎么做(1),我该怎么做(2)?
您可以通过以英文.lproj / Localizable.strings作为字典读取并获取其键来检索所有string键:
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)); }