In the previous article, I introduced the Chinese problem of mono. In any case, we can finally display Chinese characters in the program compiled by mono. If you want to develop international programs or components, you should use the Mono internationalization method to process related international texts.
In this article, internationalization refers to the strings designed in the program and will not discuss internationalization issues such as dates and currencies.
Now, Mono uses gettext to translate strings in the program. You can also use gettext # for internationalization, but it is not recommended by Mono.
In the Mono. Posix accessory's Mono. Unix Command space, there is a Catalog class, which encapsulatesLibintlProvides international services.
This article uses a console program to demonstrate how to perform international processing.
First, let's look at a console program that is not going international:
Using System; Class MainClass { Public Static void Main (String [] args) { Console. WriteLine ("Hello Everyone! My name is smallnest "); Int I = 32; Console. WriteLine ("I'm {0} years old. ", I ); } } |
Two strings are involved:
"Hello everyone! My name is
Smallnest"
"I'm {0}YearsOld ."
The second string involves the problem of single and multiple numbers. As a friendly string, if the age is 1, the string should be "I'm
{0}YearOld. ", where year should be in the singular form.
Next we will handle internationalization:
First introduce Mono. Posix. dll in monodevelop (if manually compiled, add-t: Mono. Posix ),
Then add the following content to the Main. cs file:
Using
Mono. Unix;
Initialize the Catalog class in the Main method:
Catalog.Init("i18n","./locale");
The program will be in. locale/zh_CN/
Find the i18n.mo file in the LC_MESSAGES/directory. If the file exists and contains international text, it will be replaced with international text. Otherwise, it will not be replaced.
Finally, the Main. cs file is as follows:
The Catalog. GetString method replaces text in a common way.
Catalog. GetPluralString selects the singular or plural text based on the conditions. If the third parameter is 1, the singular text is used; otherwise, the plural text is used.
To the directory of the main.csfile, run the main.exe file:
Mcs-codepage: utf8 Main. cs |
Use xgettext to export the po files to be internationalized:
Xgettext -- from-code = UTF-8 Main. cs-o Zh_CN.po |
Translate the zh_CN.po file:
# A sample for Internationalization # Smallnest <smallnest@gmail.com> # Http://www.kuaff.com # Msgid "" Msgstr "" "Project-Id-Version: PACKAGE VERSION \ n" "Report-Msgid-Bugs-To: \ n" "POT-Creation-Date: 2005-12-15 + 0800 \ n" "PO-Revision-Date: YEAR-MO-DA HO: MI + ZONE \ n" "Last-Translator: FULL NAME <EMAIL @ ADDRESS> \ n" "Language-Team: LANGUAGE <LL@li.org> \ n" & Quot; MIME-Version: 1.0 \ n & quot" "Content-Type: text/plain; Charset = gb2312 \ n" "Content-Transfer-Encoding: 8bit \ n" "Plural-Forms: nplurals = INTEGER; Plural = EXPRESSION; \ n" #: Main. cs: 10 Msgid "Hello everyone! My name is Smallnest" Msgstr "Hello everyone, my name is Yue pan ." #: Main. cs: 12 #, Csharp-format Msgid "I'm {0} year old ." Msgid_plural "I'm {0} years Old ." Msgstr [0] "I'm {0} years old" Msgstr [1] "Old Man, Brother: {0} years old ." |
Note that Content-Type:
Text/plain; charset =CharsetChange
Content-Type: text/plain; charset =Gb2312
Convert zh_CN.po to the zh_CN.mo file:
Mkdir-p locale \ zh_CN \ LC_MESSAGES Msgfmt zh_CN.po-o locale/zh_CN/LC_MESSAGES/i18n.mo |
Because zh_cn.gb2312is set for our locale, the running main.exe program will display Chinese output, as shown in: