How to localize text based on criterion other than language

前端 未结 1 478
再見小時候
再見小時候 2021-01-29 04:14

I have an application which will be marketed in different European countries. We\'ve gone through the process of localizing the application so that its strings are maintained i

相关标签:
1条回答
  • 2021-01-29 04:49

    Define two bundles on a singleton, fallback and preferred...

    #import <Foundation/Foundation.h>
    
    @interface Localization : NSObject
    
    @property (nonatomic, retain) NSString* fallbackCountry;
    @property (nonatomic, retain) NSString* preferredCountry;
    
    @property (nonatomic, retain) NSDictionary* fallbackCountryBundle;
    @property (nonatomic, retain) NSDictionary* preferredCountryBundle;
    
    +(Localization *)sharedInstance;
    - (NSString*) countryStringForKey:(NSString*)key;
    
    @end
    
    
    #import "Localization.h"
    
    @implementation Localization
    
    @synthesize fallbackCountryBundle, preferredCountryBundle;
    @synthesize fallbackCountry, preferredCountry;
    
    +(Localization *)sharedInstance 
    {
        static dispatch_once_t pred;
        static Localization *shared = nil;
        dispatch_once(&pred, ^{
            shared = [[Localization alloc] init];
    
            [shared setFallbackCountry:@"country-ES"];
    
            NSLocale *locale = [NSLocale currentLocale];
            NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
            [shared setPreferredCountry:[NSString stringWithFormat:@"country-%@",countryCode]];
        });
        return shared;
    }
    
    
    -(void) setFallbackCountry:(NSString*)country
    {
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:country ofType:@"strings"];
        self.fallbackCountryBundle = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
        trace(@"Fallback: %@ %@",[bundlePath lastPathComponent], self.fallbackCountryBundle);
    }
    
    
    -(void) setPreferredCountry:(NSString*)country 
    {
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:country ofType:@"strings"];
        self.preferredCountryBundle = [NSDictionary dictionaryWithContentsOfFile:bundlePath];
    
        BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:bundlePath isDirectory:nil];
        if (!exists) warn(@"%@.strings %@", country, exists ? @"FOUND" : @"NOT FOUND");
    
        trace(@"Preferred: %@ %@",[bundlePath lastPathComponent], self.preferredCountryBundle);
    }
    
    
    - (NSString*) countryStringForKey:(NSString*)key 
    {
        NSString* result = nil;
        if (preferredCountryBundle!=nil) result = [preferredCountryBundle objectForKey:key];
        if (result == nil) result = [fallbackCountryBundle objectForKey:key];
        if (result == nil) result = key;
    
        return result;
    }
    
    
    @end
    

    Then call it from a macro function

    #define countryString(key) [[Localization sharedInstance]countryStringForKey:key];
    

    Write a default file for ES, and one file per supported language. eg:

    /* 
      country-ES.strings
    */
    
    "hello" = "hello";
    

    And just get the value for the key:

    countryString(@"hello");
    
    0 讨论(0)
提交回复
热议问题