Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

preface

Using localization capabilities, you can easily translate your application into multiple languages and even multiple dialects of the same language.

To add localization capabilities, you need to create a subdirectory for each supported language called the localization folder, usually using.lproj as the extension name

When a localized application needs to load a resource, such as an image, a property list, or a NIB file, the application checks the user’s language and locale and looks for a matching localized folder. If the appropriate folder is found, the resources in that folder are loaded

I, nib& Info.plist localization

Preparation before localization: create a localization folder (zh-hans.lproj) to enable the application to support the corresponding locale, and select the current resources to be localized

Open viewController.xib (Chinese) file and modify the text information inside

The info. The internationalization of plist: blog.csdn.net/z929118967/…

II. Image localization

Application scenario: Different language login pictures are displayed

  • The prepared picture can be replaced with the picture in the corresponding language

For example, replace home.png in the zh-hans. lproj folder

  • Just use images in your code as usual[UIImage imageNamed:@"icon_hyxq_youhuiq"];

III. String localization

Application scenario: Internationalization switch (text in dialog box) and application name localization in iOS APP

3.1 Localization switching within the APP

[iOS APP internationalization switch] 1, string localization, 2, custom parse localization string tool class LanguageManager, 3, example: login interface switch English.

From CSDN download demo internationalization switch within the iOS APP 】 【 source: download.csdn.net/download/u0…

Article: kunnan.blog.csdn.net/article/det… Principle: 1, custom parse localized string tool class LanguageManager

2, the implementation of in-application switching language effective technology: destroy the root controller, re-enter once

Localizer string specifies the order of arguments

To highlight

1. Localization of strings

2, custom parse localized string tool class LanguageManager,

3, example: login interface switch English (including demo)

Technical implementation of in-application switching language implementation:

1, destroy the root controller, re-enter (adopt)

2. Update text to each controller by sending notification (not adopted)

  • The effect

3.2 A language for multiple string resource files scheme

Application Scenario: Solve the problem that some English internationalization does not take effect

String file length is limited, need to modify the logical partition file storage.Blog.csdn.net/z929118967/…

If your string resource file name is not Localizable. Strings, such as kn.strings, So you have to use the NSLocalizedStringFromTable (), NSLocalizedStringFromTableInBundle to read the localized strings:

//NSLocalizedStringFromTable(key, tbl, comment)
NSLocalizedStringFromTable(@"Tip".@"KN".nil);

Copy the code
  • The complete read code is as follows:

// Program localization, referencing the internationalized file
#define QCTLocal(x, ...) HZLocalizedString(x, nil)

#define HZLocalizedString(key, comment)               HZLocalizedStringFromTable(key, @"Localizable", nil)
#define HZLocalizedStringFromTable(key, tbl, comment) [[HZLanguageManager defaultManager] stringWithKey:key table:tbl]



- (NSString *)stringWithKey:(NSString *)key table:(NSString *)table
{
    // If the following system is used
    if (self.languageType==HZLanguageTypeSystem) {
        return  NSLocalizedString(key, nil);
    }
    
    // Return the corresponding internationalized text
    if (_bundle) {// If you can't find it, you can find it in file 2.
        //NSLocalizedStringFromTableInBundleWithKey
        return [self NSLocalizedStringFromTableInBundleWithKey:key table:table];
        
        
        
    }
    
    return NSLocalizedStringFromTable(key, table, nil);
}




//{// change the file to another file, first from the file, if you can't find the file, then from the file.
- (NSString*)NSLocalizedStringFromTableInBundleWithKey: (NSString *)key table:(NSString *)table{
    NSString* tmp = key;
    if(_bundle){
        
        tmp =NSLocalizedStringFromTableInBundle(key, table, _bundle, nil);

        if([tmp isEqualToString:key]){
            // Continue the search from the backup table
            
            NSLog(Start from Localizable1 for key:%@, tmp);
            tmp =NSLocalizedStringFromTableInBundle(key, @"Localizable1", _bundle, nil);
// NSLog([NSString stringWithFormat:@" end from Localizable1 key:%@", TMP]);
            NSLog(End search for key from Localizable1 :%@, tmp); }}return tmp;

    
    
    
}



Copy the code

3.3 iOS localization string specifies the parameter order (application: Multilanguage switching within the app)

From CSDN download demo internationalization switch within the iOS APP 】 【 source: download.csdn.net/download/u0…

Article: kunnan.blog.csdn.net/article/det…

Principle:

1. Customize the tool class LanguageManager to parse localized strings

2, the implementation of in-application switching language effective technology: destroy the root controller, re-enter once

Localizer string specifies the order of arguments

— — — — — — — —

Copyright notice: This article is an original article BY CSDN blogger “# public account: iOS Reverse”, in accordance with CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.

The original link: blog.csdn.net/z929118967/…

IV. Use of genstrings localization tool

➜ MainViewControllerDemo git:(Master) Qualify Genstrings Usage: Genstrings [OPTION] file1.{m,c, CPP,swift}... filen.{m,c,cpp,swift} Options -h showsthis message and exits.
 -encoding encoding       assume input files use specified encoding.
 -SwiftUI                 enables SwiftUI Text() support.
 -a                       append output to the old strings files.
 -s substring             substitute 'substring' for NSLocalizedString.
 -skipTable tablename     skip over the file for 'tablename'.
 -noPositionalParameters  turns off positional parameter support.
 -u                       allow unicode characters.
 -macRoman                read files as MacRoman not UTF- 8.
 -d                       attempt to detect encoding if read fails.
 -q                       turns off multiple key/value pairs warning.
 -bigEndian               output generated with big endian byte order.
 -littleEndian            output generated with little endian byte order.
 -o dir                   place output files in 'dir'.

Please see the genstrings(1) man page for full documentation

Copy the code
  • Another way to generate string resource files (via the terminal command genstrings)

A tool from apple for automatically extracting and generating internationalized strings from code’s NSLocalizedString;

If use NSLocalizedStringFromTable (key, TBL, comment) to obtain a string, resource files to TBL parameters as the file name

Use steps:

  • Start by adding code to get strings, such as in viewController.m
    NSString *tip = NSLocalizedString(@"Tip".@"dialog title");
    NSString *ok = NSLocalizedString(@"Ok".@"dialog button");
    NSString *tip1 = NSLocalizedStringFromTable(@"Tip".@ "ios advanced".@"dialog title");
    NSString *ok1 = NSLocalizedStringFromTable(@"Ok".@ "ios advanced".@"dialog button");




Copy the code
  • Open the terminal, navigate to the viewController.m folder, and typegenstrings ViewController.mCommand to generate a string resource file
-rw-r--r-- @1 MAC staff 714 9 17 11:52 viewcontroller. m ➜ MainViewControllerDemo git:(master) qualify genstrings Viewcontroller. m ➜ MainViewControllerDemo git:(master) qualify lrt-rw-r --r-- 1 MAC staff 140 9 17 11:54 ios advanced strings -rw-r--r-- 1 mac staff 140 9 17 11:54 Localizable.stringsCopy the code

  • Just import the resource file into the project, and then open the resource file to see that keys and comments have been generated
➜ MainViewControllerDemo git:(master) qualify cat Localizable. Strings??/* dialog button */
"Ok" = "Ok";

/* dialog title */
"Tip" = "Tip";

➜  MainViewControllerDemo git:(master) ✗ cat ios进阶.strings
??/* dialog button */
"Ok" = "Ok";

/* dialog title */
"Tip" = "Tip";

Copy the code

see also

For more information and services, please check out # Applets: iOS Reverse, only for you to present valuable information, focusing on mobile technology research field.