iOS development: NSLocalizedString language internationalization (localization) + language switching within the APP

This article explains two functional points:
① Let the APP display different texts according to the system language of the phone.
② Switching the language within the APP has nothing to do with the system language of the phone.

Language localization NSLocalizedString

First of all, we need to make our APP support multiple languages. I will explain the process in detail in the form of pictures and texts.

  1. Create Localizable.stringsfiles, shortcut keyscommand + N

  2. Select the languages ​​you want to support (click Localizable.stringsFile, in the right panel of Xcode)

  3. Configure multilingual text

  4. Support more languages

  5. Use localized language and replace all text where text is needed with local language writing.
    Previous writing method

    NSString *str = @"你好";
    

    Configure the localized writing method

    NSString *str = NSLocalizedString(@"你好", nil)
    

Switch language within APP

I am in Beijing, China, and the language on my mobile phone is Simplified Chinese. So how does the APP display the English configuration?

  1. Create NSBundlea category
    #import <Foundation/Foundation.h>
    	
    @interface NSBundle (Language)
    + (void)setLanguage:(NSString *)language;
    
    @end
    
    
    #import "NSBundle+Language.h"
    #import <objc/runtime.h>
    static const char _bundle = 0;
    
    @interface BundleEx : NSBundle
    
    @end
    
    @implementation BundleEx
    
    - (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
          
          
    
        NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);
        return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
    }
    
    @end
    
    @implementation NSBundle (Language)
    + (void)setLanguage:(NSString *)language {
          
          
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
          
          
            object_setClass([NSBundle mainBundle], [BundleEx class]);
        });
    
        objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    @end
    
    
  2. Force the APP to display the specified language (called when your business needs to switch languages)
    //简体中文
    [NSBundle setLanguage:@"zh-Hans"];
    
  3. Get the language of the system (this has nothing to do with the user's choice. You need to save the memory yourself for the user's choice)
    (this function may not be used depending on your business logic, it is for reference only)
    NSArray *appleLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
    if (appleLanguages.count > 0) {
          
          
    	NSString *currentLanguage = appleLanguages[0];
    	// 在这里判断 currentLanguage 是哪国的语言,来处理你的逻辑
    	}
    

List several language identifiers that may be used in iOS development

regional language logo
Arabic ar
Simplified Chinese zh-Hans
traditional Chinese zh-Hant
Traditional Chinese (Hong Kong) zh-Hant-HK
Traditional Chinese (Taiwan) zh-Hant-TW
English(UK) in
English (Australia) in the
English (India) and in
French fr
Indonesia id
Italy it
Japan and
South Korea is
Malay ms
Portuguese(Portugal) pt-pt
Russia ru
spanish es
Sweden sv
Thailand th
Ukraine uk
Vietnam vi

Follow, like, and share more development stories...

Guess you like

Origin blog.csdn.net/wujakf/article/details/127461704