Localizing strings in iOS: default (fallback) language?
Asked Answered
M

9

42

Is there a way to set a default language to be used when the device UI language is not supported by an app?

Example: My app is localized into English and German:

// en.lproj:
"POWER_TO_THE_PEOPLE_BTN" = "Power";
"POWER_PLUG_BTN" = "Power";

// de.lproj:
"POWER_TO_THE_PEOPLE_BTN"  = "Macht";
"POWER_PLUG_BTN" = "Spannung";

Now, if I run the app on a device with UI language set to Italian the app will use the key strings POWER_TO_THE_PEOPLE_BTN and POWER_PLUG_BTN.

There must be a way to specify a default (fallback) language to be used by the application in such a case.

From the above example it should be clear that using the English string as a key will not work.

The only option I see right now is to use NSLocalizedStringWithDefaultValue instead of NSLocalizedString.

Moraceous answered 16/7, 2010 at 10:22 Comment(0)
C
19

Perhaps this should help? -- iPhone: localization / internationalization default strings file

It should fallback to English by default. I've just switched my phone to a language into which my app is not localized, and the text was all in English, as expected.

Important: as @hyperspasm commented : To expand on/rephrase this, the fallback language is the language which was most recently chosen by the user in the device Settings, that is also represented in the app's bundle.

Cleat answered 25/7, 2010 at 10:20 Comment(3)
The fallback language is the first language in [NSLocale preferredLanguages] that is supported by the app. It is not always english. If you change the language in the settings it will move to the top of the list.Jennings
To expand on/rephrase this, the fallback language is the language which was most recently chosen by the user in Settings, that is also represented in the app's bundle.Consubstantiate
Can we set fallback language as English regardless of language which was chosen most recently?Caddie
F
21

To avoid all those lengthy syntax and more having more descriptive var name for translators, I derived my own helper method L() for translation and falling back to English

NSString * L(NSString * translation_key) {
    NSString * s = NSLocalizedString(translation_key, nil);
    if (![[[NSLocale preferredLanguages] objectAtIndex:0] isEqualToString:@"en"] && [s isEqualToString:translation_key]) {
    NSString * path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    NSBundle * languageBundle = [NSBundle bundleWithPath:path];
    s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil];
    }
    return s;
}

My Localizable.strings would look like this

"SOME_ACTION_BUTTON" = "Do action";

So in my code, i would use L(@"SOME_ACTION_BUTTON") to get the correct string

Though sometime the key is longer than the translation itself HELP_BUTTON_IN_NAV_BAR = 'Help' but it saves me a lot of time explaining what it is to whoever is helping me doing the translation

Futile answered 9/1, 2012 at 5:58 Comment(1)
I plus oned it, but I regret it now. I had set up my phone to choose a language in this order : Swedish, French, English. I had no localization for Swedish so I wanted it to fallback to English, but instead it fell back on French. See my solution.Frankie
C
19

Perhaps this should help? -- iPhone: localization / internationalization default strings file

It should fallback to English by default. I've just switched my phone to a language into which my app is not localized, and the text was all in English, as expected.

Important: as @hyperspasm commented : To expand on/rephrase this, the fallback language is the language which was most recently chosen by the user in the device Settings, that is also represented in the app's bundle.

Cleat answered 25/7, 2010 at 10:20 Comment(3)
The fallback language is the first language in [NSLocale preferredLanguages] that is supported by the app. It is not always english. If you change the language in the settings it will move to the top of the list.Jennings
To expand on/rephrase this, the fallback language is the language which was most recently chosen by the user in Settings, that is also represented in the app's bundle.Consubstantiate
Can we set fallback language as English regardless of language which was chosen most recently?Caddie
P
19

You need to make sure that the value of CFBundleDevelopmentRegion in your Info.plist is the language region that you would like to fallback to. (e.g. "en")

Phila answered 20/10, 2014 at 10:24 Comment(3)
This is (currently) the only correct solution apart from the crutches (could be suitable at the moment) suggested by the others. For details check this article: maniak-dobrii.com/understanding-ios-internationalizationButyrin
This does not seem to work anymore, tested a device with Finnish language available only in the settings. The app has Finnish and English translations, en is defined in the Info.plist for the key CFBundleDevelopmentRegion still the keys that does not exist in the Finnish .string files don't fallback to English version.Disburden
Doesn't work on iOS 11, sorry. I'm getting keys instead of translations (from Base).Roi
R
7

@Bogus answer in Swift 4, works like a charm on iOS 11.1:

public func NSLocalizedString(_ key: String, tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "", comment: String) -> String {
    let fallbackLanguage = "en"
    guard let fallbackBundlePath = Bundle.main.path(forResource: fallbackLanguage, ofType: "lproj") else { return key }
    guard let fallbackBundle = Bundle(path: fallbackBundlePath) else { return key }
    let fallbackString = fallbackBundle.localizedString(forKey: key, value: comment, table: nil)
    return Bundle.main.localizedString(forKey: key, value: fallbackString, table: nil)
}
Roi answered 23/11, 2017 at 14:20 Comment(1)
You forgot to add tableName to the last two lines for the table parameter. Other than that, works great!Festination
B
4

A fast way to do this without replacing any methods is "overriding" the NSLocalizedString define and using the methods that Apple uses for this define to replace it and add the additional fallback logic in the "overridden" method.

#undef NSLocalizedString
#define NSLocalizedString(key, comment) [self localizedStringForKey:(key) replaceValue:(comment)]

+ (NSString *)localizedStringForKey:(NSString *)key replaceValue:(NSString *)comment {
    NSString *fallbackLanguage = @"en";
    NSString *fallbackBundlePath = [[NSBundle mainBundle] pathForResource:fallbackLanguage ofType:@"lproj"];    
    NSBundle *fallbackBundle = [NSBundle bundleWithPath:fallbackBundlePath];
    NSString *fallbackString = [fallbackBundle localizedStringForKey:key value:comment table:nil];    
    NSString *localizedString = [[NSBundle mainBundle] localizedStringForKey:key value:fallbackString table:nil];

    return localizedString;
}
Bleachers answered 23/8, 2014 at 14:5 Comment(1)
Thanks for sharing. I do like your provided solution. It's a smart way of using iOS crappy fallback system where it returns the key if it doesn't find the string.... :P :)Subjunctive
F
2

My solution thanks to https://mcmap.net/q/391788/-is-there-the-official-way-to-set-fallback-localized-language-in-ios-app

Global.h

NSString * LString(NSString * translation_key);

Global.m

NSString *LString(NSString *translation_key) {
  NSString *lString = nil;
  NSString *languageCode = nil;

  if ([UIDevice currentDevice].systemVersion.floatValue >= 9) {
    NSString *localeIdentifier = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSDictionary *localeDic = [NSLocale componentsFromLocaleIdentifier:localeIdentifier];
    languageCode = [localeDic objectForKey:@"kCFLocaleLanguageCodeKey"];
  } else {
    languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
  }

  NSString *path = [[NSBundle mainBundle] pathForResource:languageCode ofType:@"lproj"];
  if (path != nil) {
    lString = NSLocalizedStringFromTableInBundle(translation_key, @"Localizable",
                                             [NSBundle bundleWithPath:path], @"");
  }

   path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];
   lString = NSLocalizedStringFromTableInBundle(translation_key, @"Localizable",
                                             [NSBundle bundleWithPath:path], @"");
  }
  return lString;
}

Usage:

#import "Global.h"
printf(LString(@"MyKey").UTF8String);

This solution doesn't take the users preference order into consideration. Instead, it will always fallback to what you have under Base if the users first language is not localized. Also if a specific key is not localized for the current language, but it exists in the base localication, you will get the base localization.

Update:

Since iOS 9, region is included in the language locales. I updated the code to handle that.

Frankie answered 27/7, 2016 at 10:0 Comment(0)
H
1

I've created category NSBundle+FallbackLanguage to support fallback language, you can check it out on the github folder. You only need to specify the array of supported languages in the implementation.

NSBundle+FallbackLanguage.h

#import <Foundation/Foundation.h>

#undef NSLocalizedString
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] localizedStringForKey:(key) replaceValue:(comment)]

@interface NSBundle (FallbackLanguage)

- (NSString *)localizedStringForKey:(NSString *)key replaceValue:(NSString *)comment;

@end

NSBundle+FallbackLanguage.m

#import "NSBundle+FallbackLanguage.h"

@implementation NSBundle (FallbackLanguage)

- (NSString *)localizedStringForKey:(NSString *)key replaceValue:(NSString *)comment {        
    NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSString *localizedString;

    if ([@[@"en", @"de", @"fr"] containsObject:language]){
        localizedString = [[NSBundle mainBundle] localizedStringForKey:key value:@"" table:nil];
    }
    else{
        NSString *fallbackLanguage = @"en";
        NSString *falbackBundlePath = [[NSBundle mainBundle] pathForResource:fallbackLanguage ofType:@"lproj"];
        NSBundle *fallbackBundle = [NSBundle bundleWithPath:falbackBundlePath];
        NSString *fallbackString = [fallbackBundle localizedStringForKey:key value:comment table:nil];
        localizedString = fallbackString;
    }

    return localizedString;
}

@end
Hush answered 13/10, 2014 at 19:32 Comment(2)
I did a riff on this so it falls back per-string and not per-language. It's possible that I missed the point entirely, of course, but it appears to work in practice. github.com/drosenstark/categoriesDuffey
This is not getting called. I have created the category on NSBundle and passed the supported language array.Dugong
K
0

Based on Bodus solution (thx btw.) I created this category because you need the "fallbackString" too. So I have to check the current selected language of the device and compare it with my languages I want to support. Just import the header and you can use apples default macro

NSString *myString = NSLocalizedString(@"My Ub0rstring", nil);

Works fine on iOS 9.x and 11.1.

NSString+Helper.h

#import <Foundation/Foundation.h>

#undef NSLocalizedString
#define NSLocalizedString(key, comment) [NSString localizedStringForKey:(key) replaceValue:(comment)]

@interface NSString (Helper)

+ (NSString *)localizedStringForKey:(NSString *)key replaceValue:(NSString *)comment;

@end


NSString+Helper.m

#import "NSString+Helper.h"

@implementation NSString (Helper)

+ (NSString *)localizedStringForKey:(NSString *)key replaceValue:(NSString *)comment
{
    NSString *fallbackLanguage      = @"en";
    NSString *fallbackBundlePath    = [[NSBundle mainBundle] pathForResource:fallbackLanguage ofType:@"lproj"];
    NSBundle *fallbackBundle        = [NSBundle bundleWithPath:fallbackBundlePath];
    NSString *fallbackString        = [fallbackBundle localizedStringForKey:key value:comment table:nil];
    NSString *localizedString       = [[NSBundle mainBundle] localizedStringForKey:key value:fallbackString table:nil];

    NSString *language              = [[NSLocale preferredLanguages] firstObject];
    NSDictionary *languageDic       = [NSLocale componentsFromLocaleIdentifier:language];
    NSString *languageCode          = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];

    if ([languageCode isEqualToString:@"de"] || [languageCode isEqualToString:@"en"]) {
        return localizedString;
    }
    else {
        return fallbackString;
    }
}

@end
Krimmer answered 15/11, 2017 at 0:55 Comment(0)
S
0

Old issue, but still an evergreen.

Here we are with a swift 4.2 quick solution to force the app on an WHATEVER_THE_FALLBACK_LANGUAGE_WE_WANT_IT_TO_BE fallback.

The example forces to "en"

extension String {

  var localized: String {

    var preferred = "-"
    if let pl = NSLocale.preferredLanguages.first, let pref = pl.split(separator: "-").first { preferred = String(pref) } //<- selected device language or "-"

    guard let _ = Bundle.main.path(forResource: preferred, ofType: "lproj") else {
        //PREFERRED ISN'T LISTED. FALLING BACK TO EN
        guard let en_path = Bundle.main.path(forResource: "en", ofType: "lproj"), let languageBundle = Bundle(path: en_path) else {
            //EN ISN'T LISTED. RETURNING UNINTERNATIONALIZED STRING
            return self
        }
        //EN EXISTS
        return languageBundle.localizedString(forKey: self, value: self, table: nil)
    }
    //PREFERRED IS LISTED. STRAIGHT I18N IS OKAY
    return NSLocalizedString(self, comment: "")
  }

}
Scott answered 4/12, 2019 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.