Let's get your. NET programs support multiple languages

Source: Internet
Author: User

Painstakingly made out of the software, we certainly hope to allow more people to use, support multi-language is necessary. I'll take the ASP. NET Web form as an example to show how to support multiple languages. Other programs such as Windows programs, the process is very similar.

There are several steps in the following sections:
    1. Generate a default resource file and translate the resource file.
    2. Write a little code to implement language switching
    3. Some high-level topics. The example is always simple, what problems will the real project encounter?

Here are the detailed steps:

Generate a default resource file and translate the resource file.
      1. Create an ASP. NET Web Form project and add a webform with some elements, such as Label,button, to set their text.
      2. Select Menu Tool->generate Local Resource, you will see that vs automatically created a folder App_LocalResources, and there is a file WebForm1.aspx.resx inside. As a technician, open your own and look at it, it's an XML file.
        Note: for ASP. NET MVC, we cannot automatically generate resource files in such a way that we need to create them ourselves.
      3. 3. At this point, open WebForm1.aspx, you will see some things changed, there are some similar to the meta:resourcekey= "Label1resource1" code. This is the most basic thing to understand how to support multiple languages in ASP. ASP. NET will use this resorucekey to find the text in the resource file that corresponds to the label's display in different languages.

      4. At this point, you try to change the text in the resource file and then restart debugging, and you will see that the content on the page changes. Well, the most basic thing that has been done to support multiple languages is now.
      5. Copy this resource file WebForm1.aspx.resx, name the new file Webform1.aspx.zh-cn.resx, and change the text into Chinese. If you start writing in Chinese, you can name it en-US and then change the content to English.
Write a little code to implement language switching

6. How do I make the page display the translated content? Very simple, in order to test you can first do this: in the WebForm1 background code to add the following method:

protected override void InitializeCulture () {Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture ("ZH-CN"); Thread.CurrentThread.CurrentUICulture = new CultureInfo ("ZH-CN"); Base. InitializeCulture (); }

 

      1. After adding the code as above, start debugging and you will see the Chinese page. Try changing the ZH-CN to a different language code your system will appear in other languages.
        A little reminder: if the resource file for the English language does not exist, or if some items do not exist, the system will be automatically looked up from the default resource file, so there will be no blank on the interface even if no translation is completed.
      2. In the actual project you will likely want to change the language of the website Mobile station, it is not complicated, just need to add a few links or buttons on the interface, sometimes a few small pictures to show the national flag of different countries, when the user click on the user's choice to save and then dynamically replace InitializeCulture () The language code in the function.
      3. Programmers who do not have multi-lingual experience will hard code some of the content to be displayed on the interface when they are programmed, so the hard-coded text needs to be extracted into the resource file before it can be supported. Otherwise it is impossible to support multiple languages. The VS Code Checker can help you find the code that supports multiple language problems. In addition to the VS auto-generated resource files, you can also create your own resource files.
Some high-level topics.

Well, the actual project is certainly not so simple, this small article is certainly not possible to the actual project may encounter the problem of all clear. I will write about some of the more typical problems and solutions that I have ever met.

        1. translation issues for resources.
          The actual project is sure to find a professional translator to translate, how do they open our resource file? It is common practice to export resource files to Excel, or to other translation industry-specific formats, and then import them. There are tools that support this feature. Some projects will have hundreds of resource files, and the management of these files is really not simple.
        2. translated version management.
          When we have completed the first version of the translation, we have to develop a second version, of course, to translate the second version, because there will be a lot of new additions and changes in the interface. Perhaps you have thought about the problems you will face, how do you find these changes and then send them to translators? Can not be manually compared and then one by one to pick out! Here we recommend a tool that we develop ourselves to help you find these changed resources, and translators can simply log in to the website to complete the translation. The URL is a layout issue for the rtp.cleancodesoft.com
        3. interface.
          When the actual translation of the project is complete, you will find something out of the interface. Chinese is a two-character content English may take a long string. For the Web program when writing CSS to consider these issues, for Windows programs, you need to reopen the designer, from the Properties window to select the language, the designer will automatically load the selected language resources, then you can re-adjust the layout. There is no shortcut, you need to re-check all the interface, it is better to have a careful tester to help you do it.
        4. can I use a. resx resource file?
          Of course you can. The principle is the same, the need to translate the content extracted, and then dynamically read. How to save doesn't matter, convenient is good. There are many open source projects and projects under other platforms that have their own variety of approaches. But as far as I know, using. resx should be the simplest.  

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.