Use gettext technology to provide international support for ASP. NET websites

Source: Internet
Author: User
Tags glob

I don't know how many people are interested in this topic, because I am playing with a website recently, and I am a bit idle to add international support to the website. AlthoughASP. NETThe ResourceManager class already exists and tags are supported for internationalization. However, the problem is that ResourceManager requires a Key for each sentence to be translated ):

1. Create a. resx file first. In Visual Studio, there is a tool to edit this. resx file.

2. Add a key-value pair for each sentence to be translated.

3. Then, use the ResourceManager or <% # label in the Code and use the defined key to tell ASP. NET to find the correct translated text during running.

It's too troublesome. I don't know if you have any other good methods. The method I use is borrowed from the unix gettext.

Concept

The concept of Gettext is very simple. Is it Text Translation? To put it bluntly, it means translating a sentence into another sentence. The sentence to be translated can be used as a keyword for retrieval, why should we create another keyword? The gettext method is simple:

1. In the source code, you can compile a special function to execute translation. This function only accepts one parameter, that is, the text to be translated.

2. Use an auxiliary program xgettext to scan the source code text, find all the text to be translated, and save it to a file. Generally, this file is called a po file.

3. Because ASP. NET does not support po files, you can use msgfmt to convert the po files to. resources files supported by ASP. NET. This method has the following advantages:

1. When writing a program, you do not need to define a new keyword for the sentence to be translated. This keyword is generally hard to understand and cannot be named. It is troublesome to maintain code-because you need to switch between the. resx editor and the cs file constantly.

2. If you do not know how to do this, it is difficult to find a tool that can edit the. resx file. The po file generated by gettext is a common text file, and the format is very simple. In this way, it is very convenient to translate.

Practice

For example, I wrote an ASP. NET MVC program. Of course, the program concept of Form Web Form is the same,

1. Write a base class for the Controller and view page. There is a function T for executing Translation:

 
 
  1. public class G18nController : Controller  
  2. {  
  3. public CultureInfo Culture { get; set; }  
  4. public string T(string message)  
  5. {  
  6. var obj = HttpContext.GetGlobalResourceObject("website", message, Culture);  
  7. var translated = obj == null ? null : obj.ToString();  
  8. if (string.IsNullOrEmpty(translated))  
  9. return message;  
  10. else 
  11. return translated;  
  12. }  
  13. }  
  14. public abstract class G18nWebViewPage<U> : WebViewPage<U>  
  15. {  
  16. public CultureInfo Culture { get; set; }  
  17. public string T(string message)  
  18. {  
  19. var obj = HttpContext.GetGlobalResourceObject("website", message, Culture);  
  20. var translated = obj == null ? null : obj.ToString();  
  21. if (string.IsNullOrEmpty(translated))  
  22. return message;  
  23. else 
  24. return translated;  
  25. }  
  26. }  
  27.  

The preceding Culture attribute can be obtained from the Request. Headers ["Accept-Language"] attribute.

2. In the code, call the T function directly for each sentence to be translated:

 
 
  1. Throw new ArgumentException (string. Format (T ("the project with ID {0} cannot be found! "), Id ));

3. After the program is written, start translation. Call the gettext program to find all the sentences to be translated and save them to the specified po file. You can download gettextfrom http://gnuwin32.sourceforge.net/packages/gettext.htm.

However, the tragedy is that gettext seems to require the main language to be English and does not support Chinese strings very well. So I wrote a gettext file using C #. You can download it from the attachment in this article. The command format is:

Zgettext-k T-I source code path name-o output po file name

Zgettext-k T-f Source code path list file-o output po file name

For example:

Zgettext-k T-I AccountController. cs-o test. po

4. The generated po file format is actually very easy to understand:

 
 
  1. #: C: \ workspace \ Views \ Role \ Edit. cshtml: 9
  2. Msgid "manage user groups"
  3. Msgstr ""
  4. #: C: \ workspace \ Views \ Role \ Edit. cshtml: 23
  5. Msgid "User Group [{0}] Permissions"
  6. Msgstr ""

Msgid is the sentence to be translated, and msgstr is the translated sentence.

5. After the translation is completed, use a Helper Program msgfmt to convert the translated po file to a format supported by ASP. NET. Because msgfmt.exe in the original gettextprogram package does not seem to generate the. resources file recognized by ASP. NET, I also wrote an msgfmt program to complete this job. The file can be downloaded from the attachment in this article. The command format is:

Msgfmt-o output resource file path-I input po file path

For example:

Msgfmt-o web site. en-US.resources-I website. po

Note: The output resource file name must be the same as the first parameter of the HttpContext. GetGlobalResourceObject function in step 1.

6. I wrote a small batch and combined Steps 3, 4, and 5 for execution:

 
 
  1. pushd src  
  2. del /F source.lst  
  3. dir /s /b src\*.cs >> source.lst  
  4. dir /s /b src\*.cshtml >> source.lst  
  5. tools\zgettext\zgettext\bin\Debug\zgettext.exe -k T -f source.lst -o glob\website.po  
  6. tools\zgettext\msgfmt\bin\Debug\msgfmt.exe -o src\App_GlobalResources\website.resources -i glob\ website.po  
  7. popd  

I hope it will be helpful to you.

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.