Overview of International Program Implementation Steps

  

> Internationalization (I18N) refers to software that supports multi-language extensions in design structure and mechanism. Its function and code design are not specific to a specific language and region. . >> One: Basic Implementation Steps > 1 Edit Source Program> Edit the source program with the vim editor. Note: To successfully implement program internationalization, the source program should contain the corresponding standard header file. The main function should call related functions, such as library libintl, function setlocale(), bindtextdomain(), textdomain(), and so on. > The following program is a simple example: >>#include<stdio.h>>#include<locale.h>>#include<libintl.h>>#include<stdlib.h>>> ;#define _(String) gettext (String)>int main(int argc,char**argv)>{> setlocale(LC_ALL,"");> bindtextdomain("gettext"," .");> textdomain("gettext");> printf("%s\ ",_("one world,one dream!"));> return 0;> } >>>2 Compile the source program> Compile the source program with the gcc command to generate an executable file. After execution, the characters of the input function in the source program can be displayed. Command example: >gcc -o gettext gettext.c>>3 Create translation file template pot> Use the xggetxt command to extract translation information from the source program and create a translation file template pot (Portable Object Template) file. Command example: >xgettext --keyword=_ gettext.c -o gettext.pot>>4 Generate portable object po files from pot> Each language has its own portable object file, ie po (Portable Object ) file. The file is composed of a pair of msgid and msgstr, where msgid represents the information extracted from the program and msgstr represents the translation. Command example: >cp gettext.pot gettext.po>>5 Edit the translated characters in the po file, modify the encoding of the characters > Edit the po file with the gedit or vim editor, translate the relevant information, and change the charset to UTF -8, otherwise it cannot be encoded. When you are done, save the file. Note: If you want to implement multiple languages, you should copy the multi-po files and translate them separately. Command example: >gedit gettext.po or vim gettext.po>>6 Generate a binary mo file from the po file> Use the msgfmt command to convert the po file to a binary mo (MachineObject) file for computer read execution. Command example: >msgfmt gettext.po -o gettext.mo>>7 Putting the mo file in the correct directory structure >mo file storage location is the key to the internationalization of the program. In general, it should be placed in the corresponding subdirectory under the locale of the corresponding region. Usually the default directory structure is /usr/locale/share/local, so the mo generated in this example should be placed in the corresponding subdirectory under this directory. However, since the setlocale() function in the program is set to search for translation information under the current directory structure, it is only necessary to create a corresponding local directory in the current directory (ie, the directory where the source program is located). In this example, you can create the following directory structure: > zh_CN/LC_MESSAGES/and en_US/LC_MESSAGES/> After the directory is created, put two different mo files in these two directories respectively>>8 Setting the environment Variables> This sample program reads the LAN information from the current environment setting LANG, so you need to set LANG as follows. Note: Only one region can be set at a time. >LANG=zh_CN.UTF-8>LANG=en_US.UTF-8>>9 Execute the program and get the result> Run ./gettext on the terminal interface to get the result to be displayed. >>2: Upgrading the internationalization program> In the process of debugging the program, it is inevitable to modify the source program accordingly. How to correctly display the newly modified translation after the source program is modified, and effectively use the original translation information to improve efficiency, that is, how to "upgrade" the internationalization program? > Use the following steps to resolve this issue. >>1 Modify (add, delete) source program>>2 Run xgettext to generate new pot file> Command example: >xgettext --keyword=_ gettext.c -o gettext.pot>>3 Rename the original program's po file to distinguish the po file that will be generated by the new pot file. > Command example: >mv gettext.po old_gettext.po>>4 Run msgmerge to compare and merge the new and old pot files. Upgrade version of the po file> command example: >msgmerge old_gettext.po gettext.pot --output-file gettext.po>>5 To record the difference between the old and new pots, you can use the diff command to record the two pot files. Similarities and differences. > Command example: >diff -u old_gettext.po gettext.po > old_gettext.diff>>6 After generating a new po file in step 4, you can delete the original po file to avoid interference. >Command example: > rm -f old_gettext.po>>7 Generate a new file from the newly generated po file> Command example: >msgfmt gettext.po -o gettext.mo>>8 will be newly generated The mo file is placed under the correct directory structure >>9Set environment variable>>10 Execute the program to get the new result>>Three: Attention to some problems>1 The setlocale() function in the source program The directory structure of the mo file should be consistent, otherwise the correct result will not be obtained. >>2 When editing a po file, be careful to adjust the characters to a portable encoding UTF-8 to get the correct result. Otherwise there will be a warning: >(gettext.po: warning: character set <quo;CHARSET” is not a portable encoding name. Converting a message to a user character set may not work.)>>3 Setting multiple environment variables (Region), copy multiple copies of po and edit them separately, and put the generated mo files in the correct directory. >>4 After modifying the source program to upgrade it internationally, the executable file of the original program (before modification) should be deleted, otherwise the result of the modified program cannot be obtained>>4: Some of the source program Explain >>#include<stdio.h>>#include<locale.h> //Define the localization function. >#include<libintl.h>//Includes the gettext library, which is a set of tools and libraries specifically developed by GUN to solve the problem of program internationalization.

Copyright © Windows knowledge All Rights Reserved