An example of how to switch local language in IOS application



When our application is only for domestic users, it generally supports only one language – Chinese. When facing foreign users, it needs to be internationalized, not only the change of language, but also the change of design style, page layout and interaction effect, such as wechat, microblog, QQ and other applications have the function of switching languages.

Recently, due to work reasons, I have encountered a need to set the language in the app. The default language is the one set by the system. If the user sets another language in the app, the interface will be displayed according to the new language.

IOS determines the language of the application

1. IOS first searches the user’s language preferences (settings – General – language and region)

2. Check whether your application supports the user’s language. First, use the first language of preferences to check whether the application contains the folder corresponding to the language (suffix is. Lproj, file name part, English is en, Chinese Simplified is zh Hans, Japanese is Ja). If it exists, it is the language. Otherwise, use the second language of preferences to match. Repeat the process.

3. Once the system determines the language for the application, the corresponding. Lproj folder will be used as localization resources.

So, I wrote a new wheel solocalization (local download). I hope you like it. In fact, the code is very simple. If you are interested, you can read the source code directly, and if you have a supplement, you can submit a pull request to me directly. Let’s take a look at the detailed introduction.

The method is as follows

1. Create localization file.

Create a localized string file, the file name can be the default infoPlist.strings , or something else. Set the language and content to support for this file.

2. Import solocalization.

3. Configure solocalization.

Set the supported language and default language. When the language set by the system does not belong to the supported language, the default language will be used.

[SOLocalization configSupportRegions:@[SOLocalizationEnglish, SOLocalizationSimplifiedChinese] fallbackRegion:SOLocalizationEnglish];

4. Set the UIKit object.

Setting appropriate properties for UIKit objects is as simple as the following. Moreover, when the language is switched, the text content of these UIKit objects will automatically change to the newly set language without any additional processing.

UIBarButtonItem *change = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(changeLanguage:)];
//Here @ "setting" is the corresponding key in the strings file, the same below
change.sol_title = @"Setting"; 
self.navigationItem.rightBarButtonItem = change;
self.label.sol_text = @"label";
[self.button sol_setTitle:@"button" forState:UIControlStateNormal];
self.textField.sol_placeholder = @"textField placeholder";
self.navigationItem.sol_title = @"title";

For the UIKit object whose solocalization does not support automatic modification of text content after switching languages, you can obtain the required localized string as follows:

//Where "title" is the corresponding key in the strings file and "infoplist" is the localized string file name
NSString *localizedString = SOLocalizedStringFromTable(@"title", @"infoPlist");

5. Modify the language used in the application

// to use English
[SOLocalization sharedLocalization].region = SOLocalizationEnglish;
//To use simplified Chinese
[SOLocalization sharedLocalization].region = SOLocalizationSimplifiedChinese;

6. Custom localized string file name

In the API provided by solocalization, the default localized string file name is infoPlist.strings If you use another file name (such as this file name: local.strings ), as follows:

//For supported UIKit objects
label.sol_table = @"local";
//When using solocalizedstringfromtable
NSString *localizedString = SOLocalizedStringFromTable(@"title", @"local");


The above is the whole content of this article, I hope the content of this article has a certain reference value for your study or work, if you have any questions, you can leave a message to exchange, thank you for your support for developer.