Write in front
? IOS Multilingual version of the development (b) we realized how to let the user to switch the function of the system language, we also wrote a demo for the auxiliary learning; However, the following two articles are based on the project has just started or started shortly, the project exists in the Chinese string is not many, manually changed up also can accept ; So the question is, if the project has been completed or several versions have been iterated, how can that be done? Manual change is not too realistic, time-consuming and laborious manual, here will not be considered. Not to consider, because I have a simpler and faster and more efficient way, let me slowly with you to the road;
How do I tell the Chinese string in my project?
If you want to replace the Chinese strings used in the project, then the premise must first find these strings, and then replace these strings, we define the macros , in order to replace the convenience, you can use these strings themselves key , here do not understand it does not matter, I will talk about below. Since it is the identification of the Chinese string, that is, in the project file matching find, when it comes to matching, it needs regular expression, with regular expression matching Xcode in the use of the Chinese string (@"[^"]*[\u4E00-\u9FA5]+[^"\n]*?")\s* , use can, open your Xcode try, remember find The following options are selected Regular Expression , which is selected by Text default,
How do I remove a recognized Chinese string?
? We already know how to identify a string in a project, but how do we get it out and use it? This requires traversing all the specified suffixes in the project (e.g:. h,. m, etc.) and then using regular expressions to match all the Chinese strings used and write to the file.
For the sake of convenience, I wrote this process a Mac client small application, can directly select all the Chinese strings in the project, export to the specified path;
The application can also choose to repeat the Chinese character string processing, as well as the Chinese string can also be exported to traditional; The small application is more noteworthy is its export files, can be directly used as a multi-language file in the key=value use, very simple, like can go to download see, contains the source of the OH The small application run graph is as follows:
How do I use the extracted Chinese string?
have been taken out of the Chinese string file, this file can be used directly for use as a multilingual file, small application exported files, can only be used in Chinese and traditional multi-language files, and for other languages, you need to take the exported files, to find your company's translators, to translate; Take the traditional version to cite an example , the exported traditional files are as follows (take my app to export files):
This traditional file can be used directly as a traditional multi-language, how to want to translate into English, you can copy this file, to the translator, let it translate. When translating, you must give the translator good communication, let it only translate, that is, the contents of the value = double quotation marks in the back of the picture, format and so do not let him change, otherwise prone to problems, leading to key and value not on the number. After all communication, and so on after the translation of the document to you, directly the contents of the file, copied to the multilingual file in English file can be;
How to replace a Chinese string in a project
The multilingual file has been configured, and here, so the problem is, we just take out the Chinese string used in the project, and then encapsulate the string into a multilingual file, but the Chinese string used in the project is still a Chinese string, and we haven't done any substitution processing. Of course, this step we are not able to forget, because is to replace all the items in the use of Chinese strings, we are cautious as well, first of all your project backup, backup, backup , very important things to say three, remember must be backed up. After the backup, we can start to replace the work, no backup do not look down, and quickly back up;
? Assuming you've backed up, you can start replacing your work, as we said before to replace the Chinese string itself as a key key, because it can greatly reduce the difficulty of substitution, but also facilitates the readability of the code, basically maintaining the original code Now that you want to replace the use of Chinese strings in the project, you will need to use the string substitution function of Xcode, first of all (@"[^"]*[\u4E00-\u9FA5]+[^"\n]*?")\s* , you'll want to match the Chinese string used in the project with the regular expression, and then replace it with.
Because we are in the development of iOS Multi-language version (ii), the corresponding language content of the key, the macro is encapsulated, here we can match the use of Chinese characters directly to the use of our defined macros
#define ASLOCALIZEDSTRING (Key)
The above is the definition of the macro, we need to replace the string macro use, such as:
? This is before the replacement._lbl.text = @"我是多语言";
? After replacement_lbl.text = ASLocalizedString(@"我是多语言")
Substitution rules
This time it was chosen Replace , not Find . As shown in the figure, after the selection is finished, click ReplaceAll , will come out a box, do not care it directly to continue, and then you look at the project in the use of the Chinese string has been replaced, to here basically done;
Legacy issues
1, regular expression, do not know can first according to the article to write on the line, and then to learn, here is a basic article
2, Aslocalizedstring (key), the macro area is wide, you can define it as a common header file, and then use the reference header file, you can also encapsulate the file into the PCH file, so that you do not need to use the header file interface
3, after the replacement, there may be some local error, command + b compile, to see where the error, the corresponding changes can be.
Cause of Error:
A. may be a constant string that you define;
B. A macro that may be defined;
C. There may be no line break after replacement;
The basic report error is obvious, can be modified;
4, in the iteration version, the use of Chinese strings again, you need to use ASLocalizedString(key) , to replace, and then in a multi-language file, to key=value configure;
ios-Multi-language version development (iii) (reproduced)