Preface
When our application is only aimed at the domestic user base, it generally only supports one language - Chinese. When targeting foreign users, internationalization is needed. It is not only a language change, but also a change in design style, page layout, and interactive effects. Applications such as WeChat, Weibo, and QQ all have the function of switching languages.
Recently, due to work reasons, I encountered a need to set a language inside the application. By default, the language set by the system is used. If the user sets another language in the App, the interface will be displayed according to the newly set language in the future.
iOS determines the language of the application
First, search for the user's language preference settings (settings-General-Language and Region)
2. Check whether your application supports the user's language. First use the first language of preference to check whether the application contains the corresponding folders of the language (the suffix is .lproj, the file name part, English is en, Chinese simplified Chinese is zh-Hans, Japanese is ja). If it exists, it is the language, otherwise use the second language to match. Repeat the process.
3. Once the system determines the language for the application, the corresponding .lproj folder will be used as a localization resource.
So, I wrote a new wheelSOLocalization (Local download), I hope everyone likes it. In fact, the code is very simple. If you are interested, you can directly read the source code. If you have any supplements, you can directly submit a pull request to me. I won’t say much below, let’s take a look at the detailed introduction together.
The method is as follows:
1. Create a localized file.
Create a localized string file. The file name can be the default or other. 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 UIKit object.
Setting appropriate properties for UIKit objects is as simple as the following. After setting this way, when the language is switched, the text content of these UIKit objects will automatically become the newly set language without any additional processing.
UIBarButtonItem *change = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(changeLanguage:)]; // The @"Setting" here is the corresponding key in the strings file, the same belowchange.sol_title = @"Setting"; = change; .sol_text = @"label"; [ sol_setTitle:@"button" forState:UIControlStateNormal]; .sol_placeholder = @"textField placeholder"; .sol_title = @"title";
For SOLocalization, UIKit objects that automatically modify text content after switching languages, you can get the required localized strings like this:
// where "title" is the corresponding key in the strings file, and "infoPlist" is the localized string file nameNSString *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 localized string file name used by default is file. If you use other file names (such as using this file name:), it is as follows:
// For supported UIKit objectslabel.sol_table = @"local"; // When using SOLocalizedStringFromTableNSString *localizedString = SOLocalizedStringFromTable(@"title", @"local");
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.