NSLocalizedString not defaulting to Base language

后端 未结 4 2151
情话喂你
情话喂你 2021-02-20 09:20

I have the following problem with a small iOS 7 project on which I\'m testing the localisation capabilities.

  • I have a default project, with one VC, in which I have
相关标签:
4条回答
  • 2021-02-20 09:41

    Using Dirk's answer here is the Swift equivalent implemented as a computed property in a String extension:

    extension String {
    
    var localized: String {
    
        var localizedString = NSLocalizedString(self, comment: "")
    
        // If a localized string was found then return it.
        // This check is based on the fact that if no localized string
        // exists then NSLocalized() returns the key itself.
        if self != localizedString {
            return localizedString
        }
    
        // No localized string exists.  Retrieve the display string
        // from the base strings file.
        var bundleForString: Bundle
        if let path = Bundle.main.path(forResource: "Base", ofType: "lproj"),
            let bundle = Bundle(path: path) {
            bundleForString = bundle
        } else {
            bundleForString = Bundle.main
        }
    
        localizedString = bundleForString.localizedString(forKey: self, value: self, table: nil)
    
        return localizedString
    }
    
    } 
    
    0 讨论(0)
  • 2021-02-20 09:52

    the order of default languages is a user setting on OSX and not editable (AFAIK) on iOS
    BUT still adhered to!

    the app is passed the array AppleLanguages (or so..) that specifies the languages to try. The NSLocalizedString macro will try load each language in the array in the order they appear UNTIL it finds a working one and then it uses that

    compare: How to force NSLocalizedString to use a specific language

    0 讨论(0)
  • 2021-02-20 09:53

    I have created the following class, which supports a fallback to a customizable language. In my case I use Base.lproj as file for my default language contents.

    StringUtilities.h

    @interface StringUtils : NSObject
    
    #define GetLocalizedString(key) [StringUtils getLocalizedString:key comment:nil]
    //#define GetLocalizedString(key,comment) [StringUtils getLocalizedString:key comment:comment]
    
    
    + (NSString*) getLocalizedString:(NSString*)key comment:(NSString*)comment;
    
    @end
    

    StringUtilities.m

    #import "StringUtilities.h"
    
    @implementation StringUtils
    
    
    //Returns a localized string, with fallback to version of Base
    + (NSString*) getLocalizedString:(NSString*)key comment:(NSString*)comment {
        NSString* localizedString = NSLocalizedString(key, nil);
    
        //use base language if current language setting on device does not find a proper value
        if([localizedString isEqualToString:key]) {
    
            NSString * path = [[NSBundle mainBundle] pathForResource:@"Base" ofType:@"lproj"];
            NSBundle * bundle = nil;
            if(path == nil){
                bundle = [NSBundle mainBundle];
            }else{
                bundle = [NSBundle bundleWithPath:path];
            }
            localizedString = [bundle localizedStringForKey:key value:comment table:nil];
        }
        return localizedString;
    }
    
    @end
    

    How to use

    Import the header file and use the GetLocalizedString macro instead of NSLocalizedString macro.

    #import "StringUtilities.h"
    
    NSString* str = GetLocalizedString(@"your.text.key");
    
    0 讨论(0)
  • 2021-02-20 10:05

    I have an equivalent in Swift:

    public func LS(_ key: String) -> String {
        let value = NSLocalizedString(key, comment: "")
        if value != key || NSLocale.preferredLanguages.first == "en" {
            return value
        }
    
        // Fall back to en
        guard
            let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
            let bundle = Bundle(path: path)
            else { return value }
        return NSLocalizedString(key, bundle: bundle, comment: "")
    }
    
    0 讨论(0)
提交回复
热议问题