Complete guide to iOS Internationalization
After reading this article you can solve most of the problems of internationalization, and I rewrite the previous script; Automatic internationalization of code, XIB, storyboard. Automatically add, delete text, all content in the project, no need to manually maintain, simple internationalization of new projects, easy internationalization of old projects. The project address
I tried to implement a MAC application using XCodeEditor to make our internationalization completely out of the project. A single language file was fine, but the file references of multiple internationalization languages were never set correctly, so it didn’t work. If you are a MAC developer or have an understanding of the core file structure of project. Pbxproj; You can contact me to realize this project together
If you have any questions, please feel free to contact me.
Basic posture of internationalization
To internationalize a project, first add the required language to the project’s Build Settings:
Xib and StroyBoard internationalization
If it’s a new project, I strongly recommend energize to use xiB and StroyBoard to build your project, because if you come across languages that support Arabic, their custom is right to left, the RTL (Rignt to Left). The entire interface required mirror flipping, and at that time the only methods provided by the system supported xiB and StroyBoard. If you die in pure code, it’s a lot of trouble.
Xib and SB internationalization is actually very simple; You only need to select the corresponding file.
The following items should appear:
Note that characters already used in xiB or SB will appear in the automatically generated files. But it will only generate characters that already exist at the time you clicked Localization if you keep developing them. Controls are added and removed, so they are not automatically generated. Only by uninternationalizing the part and then checking the language will a new file be generated and will overwrite the internationalization file you have already written. This script solves this problem and no longer requires manual maintenance. The solution principle is:
ibtool --generate-stringsfile MyNib.strings MyNib.nib
Copy the code
Ibtool is a small tool that comes with the system. Xib or SB can automatically generate a. Stings file.
Code internationalization
The only way to internationalize code parts is to use the system’s internationalization macros:
NSLocalizedString("Here are the characters that need to be internationalized.",comment: "Here's a comment.")
#define NSLocalizedString(key, comment) \
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
...
/* Method for retrieving localized strings. */
- (NSString *)localizedStringForKey:(NSString *)key value:(nullable NSString *)value table:(nullable NSString *)tableName NS_FORMAT_ARGUMENT(1);
#define NSLocalizedStringFromTable(key, tbl, comment) \
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
[bundle localizedStringForKey:(key) value:@"" table:(tbl)]
Copy the code
NSLocalizedStringFromTable (key, TBL, comment) is less in this way, because the TBL is TBL. The name of the strings, after the part USES the macro. You also need to generate the resource file strings:
You generate this file, enter the file name if it is Localizable you use macros first, if you enter any other name, so you should use NSLocalizedStringFromTable (key, TBL, comment). Where TBL is the name of the file you typed. Tip: You can write this in every.m file that needs to be internationalized:
button.titleLabel.text = NSLocalizedString(@"Probability test", "XXXXX. M");Copy the code
Later in the generated internationalized language, you will find that the internationalized annotations are generated. When there are too many words to translate, it is convenient to find the characters on the page.
Here’s a little trick, whether you’re new or old; You can use regular matching to find all non-internationalized characters in your project:
@"[^"]*[\u4E00-\u9FA5]+[^"\n]*?"
NSLocalizedString($0, nil)
Copy the code
This allows you to quickly find items that use the @ “Chinese” format and replace them with system macros. Replace all with one click, and then use my script to automatically manage the internationalized characters in the code section.
My Python scripts only support macros in the first way, as long as you implement the macro. So my script supports automatic import, automatic add, automatic delete. It is recommended to include the file name in the comment section, if you have thousands of words that need to be translated, then the file is very large. It helps you locate specific characters. My implementation principle is based on:
genstrings -o .m en.lproj
Copy the code
Genstrings system has its own, can use a file macro character, generate the corresponding file to the en.lproj directory.
My script has taken care of the details and no longer requires manual maintenance. Internationalization is just a matter of filling in the blanks.
Pictures, film and television frequency internationalization
This part of internationalization is simple:
- In the code part, as in the text part of the code, you just take the key and write the corresponding name in the different language.
- The XIB and SB sections create a file directory in the project specifically for images. And then it’s the same as XIb and SB. Click Localization in the file resource monitor on the right and modify it in the actual project directory.
There’s no difference between pictures, video, and audio.
System permission internationalization –InfoPlist
The system permissions section generates multilingual files in the same way as the other sections, you only need to generate files in:
The description of the NSLocationAlwaysAndWhenInUseUsageDescription = "use positioning"Copy the code
To find the corresponding permission name, just select showRaw and it will appear:
Internationalization of System resources
Into the photo gallery page to be obtained, for example, the address book page, and copy and paste the system prompt, etc., this part is to follow CFBundleDevelopmentRegion the permissions, system development language. This is the system default language. When there is nothing, it will be English. If you change it. The system resources page will take effect based on your changes. There are two ways to choose: one is to choose a country, and the other is to choose a specific language range. These are two concepts!! If you want to change the default development language, select International. If you want to modify the system resources page, you need to select the language range. For example: zh_CN
Server-side internationalization
There is no special good method for this part, but it is achieved through negotiation. Generally, there are two kinds:
1. The back end fully realizes the corresponding language prompt and returns it to the front end. 2. Regardless of language, the backend returns a set of its own set of prompt language numbers. The front end prompts the corresponding content according to this number.
In-app language selection
In fact, internationalization in iOS is to switch different language resources and display different file information. The system then uses a Localizable system macro to maintain the key/value relationship. Each language in the system corresponds to a bundle resource file. When you switch languages, look for resources in the corresponding language pack. So we can use the Runtime to dynamically load different bundles to implement language switching. Examples are provided in the demo.
There are generally only two ways to implement this part:
1. Wechat: Switch bundle.
2. The way of micro-blog: send language switch notification.
The articles discussed in this section are already numerous and mature; I won’t go into any more detail.
Problem solving
1. Arab region to support RTL after practice, XIB, SB system provides a good method. Just use Masony to change the layout of the code section, and you’re done.
2. TextView system does not support textView to write a classification, not difficult.
3. The AttributedString rich-text internationalization part is a bit of a customization, but I still have to write my own classification.
Script use question answer:
-
If it doesn’t work?
- Please check if you use the system internationalization macros, the code part, will only generate the corresponding characters using internationalization macros. Such as:
button.titleLabel.text = NSLocalizedString(@"Probability test", nil); Copy the code
- Please check if you have PYTHon3. Python2.7 comes with MAC. I’m using python3.7.
- Instead of using the zip package, clone the project and take the ZPPScript file from the project. Too late, update slowly.