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

后端 未结 1 1237
-上瘾入骨i
-上瘾入骨i 2020-12-09 10:46

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 @\"Фу\" i

相关标签:
1条回答
  • 2020-12-09 11:15

    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));
    }
    
    0 讨论(0)
提交回复
热议问题