iPhone/iOS: How can I get a list of localized strings in all the languages my app is localized in?

こ雲淡風輕ζ 提交于 2019-12-17 18:37:57

问题


I need to send my server a list of localizations for a particular string.

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:
    • English: "Foo"
    • Russian: "Фу"

What I think I need to be able to do is:

  1. Enumerate localized strings for each language my app is localized for
  2. Get the localized version of Foo for each language

How do I do (1) and how do I do (2)?


回答1:


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];

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));
}


来源:https://stackoverflow.com/questions/6310487/iphone-ios-how-can-i-get-a-list-of-localized-strings-in-all-the-languages-my-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!