Multi-language solution for ASP.net Web site based on GetText

Source: Internet
Author: User
Tags gettext interface resource wrapper root directory
Asp.net| Solution

Long ago wanted to write this blog, the system describes how I used GetText to blogwind many language interface ...

I don't like. NET built in multi-language solutions ... Because, it uses XML ... Editing XML is a painful thing ... I don't want to get a bunch of resouce files for every page ... Looking at these resource files, I am dizzy ...

I know from Django that there is a gettext solution for programs that are widely used in open source software ...

Favorite place is it by default directly use the English text as the key value of the string ... Instead of adding a number to all strings, like. NET Defaults ... When translation does not exist,. NET throws an exception, and GetText returns the original ... In this way, I can make a language version of the website in part ... Without the need for one-step ...

--------------------------------------------

GetText's translation file is. po file ... is actually a plain text file ... The content is similar:

#: D:/web/blogwind/default.aspx:87
MsgId "Loading ..."
Msgstr "Read ..."

The first line represents the position of the string I need to translate in the program ... MsgId is the character that needs to be translated ... msgstr is the result of translation ... Very simple......

When I do a website, I use a function to cover all the strings that need to be translated ... Say:
PH.G ("Write blog")

Then, I wrote a small Python program to search all the PH.G ("XXXXX") in my web program and generate/update the existing PO file ...

Poedit can be used to edit the Po file ... Very convenient ... Because the annotation can be placed to the translation of the string in the source program position, I can always refer to the translation of the source program, to see where it is used in this phrase ... To ensure that when you encounter a word multiple translation can choose the right one translation ...

After you edit the Po file, you will need to use the GetText package msgfmt this applet to compile the Po file directly into the resource DLL used by. Net. Commands are similar:

Msgfmt--csharp-r blogwind-l zh-chs-d. Blogwind.po

--csharp is a DLL that specifies that output is. NET ... Uh...... You need to specify this because it can also be exported as JAVA/TCL/QT with several. NET resource file Formats ... Support for. NET is later added ... If you have an error, make sure you are using the 0.13.1 version or the updated GetText library ...

-R blogwind refers to the DLL's resource name Blogwind

-L Zh-chs is the language family.

The last one is the PO file name to be compiled ...

----------------------------------------

GetText has C # 's "wrapper" ... The source code inside GNUWIN32 ... It's the wrapper......wrap of the. NET System.Resources class ... Make it conform to the practice that GetText can not find translation and return to the original. By the way, I'll take care of the resource cache ... There are also hundreds of lines of code ...

However, it appears that this version is designed for the desktop ... To use it in a Web page, you need to make some minor changes ... When it reads those msgfmt-generated DLLs, it uses the following: Assembly.LoadFrom (...)

This means that it reads only one DLL at a time ... For a single user's desktop program, it is normal to use one language at a time ... However, for the site, I need to provide the same language interface for different users ... So...... Need to be changed into assembly.loadassembly (...)

------------------------------------------

Well, in the end, it's called ... Notice that the code that appears below is written by me ... There is absolutely no need to do it in my own 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

Call in the application start of Global.asax:
Lh.initcate ("Blogwind", Server.MapPath ("Langs"))

Well, this way, it will go to the site root directory langs directory to find the various language DLLs it needs ... Of course, blogwind this category name and Langs This directory can be changed to anything else ...

The PH.G (..) that I wrote first. The 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 ... Uh...... I know, my class name is very standard ... In short, I also saved the current viewer's language information in the pagehelper inside the langs/pagehelper ... Pass:
Dim cu as New System.Globalization.CultureInfo (langs.getlangstring (Me.lang))
To obtain the corresponding CultureInfo, and then to the gettext ...

For example, the language family is Zh-chs, in the search for translation word is "write blog", GetText will go to Webroot/langs/zh-chs directory to find Blogwind.resources.dll this document ... Found, then load it, and then look for "write blog" and so on ...

Because these translated DLLs are read dynamically ...

In fact, so that the program in the App_start automatically go through the langs in the subdirectory, to see what language families ... Then automatically generate a menu in the Web page for users to choose ... Add the language later, in fact, simply to the directory to create a directory to throw the DLL ... The program itself, no need to make any changes ...

-----------------

Of course, the above is just the simplest string translation ... There are pictures on the Web page ... Language-related CSS and so on details ... And, in fact, I wrote a small Python program (dozens of lines) to generate/maintain PO files when I used GetText ... If the PO file can not automatically generate, maintenance updates, management is very troublesome ...

The instructions above are also very brief ... It just means to provide a multilingual interface to ASP.net website ... In addition to using the official advocacy of resource files, there are other options ... And for GetText This choice, I feel more elegant ... www.blogwind.com interface, is the use of the above way to achieve ...

As for where to download GetText and msgfmt and other tools, wrapper in the line of changes in the specific where and so on, please do not ask me ...



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.