Gettext-based multi-language solution for Asp.net websites

Source: Internet
Author: User

Http://www.blogwind.com/Wuvist/54115.shtml

I wanted to write this blog a long time ago. The system will introduce how to use gettext to give blogwind a multi-language interface ......

I really don't like. Net's built-in multi-language solutions ...... Because it uses XML ...... Editing XML is a very painful task ...... I also don't want to get a bunch of resouce files for every page ...... I am dizzy when I look at these resource files ......

I know from Django that gettext is a multilingual program solution widely used in open-source software ......

What I like most about it is that it uses the original English text as the key value of the string by default ...... Instead of adding a number to all strings as. Net defaults ...... When the translation does not exist,. Net throws an exception, while gettext returns the original text ...... In this way, I can partially create a language version for the website ...... You don't need to do it in one step ......

The translation file of gettext is A. Po file ...... It's actually a plain text file ...... The content is similar:

#: D:/web/blogwind/default. aspx: 87
Msgid "loading ..."
Msgstr "reading ..."

The first line represents the position of the string to be translated in the program ...... Msgid is the character to be translated ...... Msgstr is the translation result ...... Very simple ......

When I was doing a website, I used a function to cover all the strings to be translated ...... For example:
Ph. G ("write online logs ")

Then, I wrote a small Python program to search all the Ph. G ("XXXXX") in the even web page program, and generated/updated the existing Po file ......

You can use poedit to edit the Po file ...... Very convenient ...... Because the position of the string to be translated in the source program can be put in the comment, even when translating it, you can check the source program at any time to see where the word is used ...... In order to ensure that the correct translation can be selected in the case of one-word multi-translation ......

After editing the Po file, you need to use the msgfmt Applet in the gettext package to directly compile the Po file as the resource DLL used by. net ...... The command is similar:

Msgfmt -- CSHARP-r blogwind-l ZH-CHS-D. blogwind. Po

-- CSHARP is the DLL that specifies the output as. net ...... Er ...... You need to specify this because it can also output several. net resource files in the format of Java, TCL, and QT ...... The support for. Net was later added ...... If an error is reported, make sure you are using version 0.13.1 or the updated gettext library ......

-R blogwind indicates that the DLL Resource Name Is blogwind.

-L ZH-CHS is a language family.

The last one is the name of the Po file to be compiled ......

Gettext has C # "wrapper "...... The source code is included in gnuwin32 ...... It's a wrapper ...... WRAP is the. NET system. Resources class ...... Make it conform to the practice of returning the original text without the translation of gettext ...... By the way, we can fix the resource cache and so on ...... Hundreds of lines of code ......

However, this version seems to be designed for desktop programs ...... To use it on a webpage, you also need to make some minor changes ...... When it reads the DLL generated by msgfmt, it uses: Assembly. loadfrom (.....)

This means that it only reads a DLL ...... For a single user's desktop program, it is normal to use one language at a time ...... However, for websites, you may need to provide different language interfaces for different users at the same time ...... So ...... Need to change to assembly. loadassembly (.....)

Well, at the end, it's called ...... Note that the following code is written by myself ...... There is no need to do this in an even way ......

Imports GNU. gettext

Public class LH
'Lh = language helper
Public shared catalog as gettextresourcemanager

Public shared sub Init (byval name as string, byval dictpath as string)
Catalog = new gettextresourcemanager (name, dictpath)
End sub
End Class

In application start of Global. asax, call:
LH. initcate ("blogwind", server. mappath ("Langs "))

Well, in this way, it will go to the Langs directory under the root directory of the website to find the required language DLL ...... Of course, the Class Name of blogwind and the Langs directory can be changed to anything else ......

The first ph. G (...) function is actually:
Public Function g (byval word as string) as string
Dim Cu as new system. Globalization. cultureinfo (Langs. getlangstring (Me. Lang ))
Return LH. Catalog. getstring (word, Cu)
End Function

PH is pagehelper ...... Er ...... I know, my class name is very standard ...... In short, I saved the language information of the current viewer in pagehelper and put it in Langs/pagehelper ...... Pass:
Dim Cu as new system. Globalization. cultureinfo (Langs. getlangstring (Me. Lang ))
To obtain the corresponding cultureinfo, and then pass it to gettext ......

For example, if the language is ZH-CHS and the word to be translated is "write blog", gettext searches for blogwind In the webroot/Langs/ZH-CHS directory. resources. DLL file ...... Find it, load it, and search for "write blog" and so on ......

Because these translation DLL files are dynamically read ......

In fact, the program can automatically traverse the sub-Directories In Langs in app_start to see which languages are there ...... Then, a menu is automatically generated for the user to choose ...... To add a language later, it is easy to set up a directory in this directory and throw the DLL ...... The program itself does not need to be modified at all ......

 

Of course, the above is just the simplest string translation ...... There are images on the webpage ...... Language-related CSS and other details ...... In fact, when using gettext, I also wrote a small Python Program (dozens of lines) to generate/maintain Po files ...... If the Po file cannot be automatically generated or updated, It is very troublesome to manage it ......

The above description is also very simple ...... I just want to provide a multilingual interface for the Asp.net website ...... In addition to the official resource file method, you can also have other options ...... The choice of gettext is more elegant ...... The multilingual interface of www.blogwind.com is implemented using the above method ......

As for where to download tools such as gettext and msgfmt, do not ask me where to modify the lines in wrapper ......

Here, we may modify the version after re-Compilation:
Http://files.cnblogs.com/wuvist/GetText.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.