Application open-source project stringresourcetool2 to implement. Net multi-language solutions

Source: Internet
Author: User

We develop applications in the application. NET TechnologyProgramThe use of string resources is often involved. For example, users in different countries can provide resources in different languages to make our applications more friendly. You can use. the resx file provided by net solves this problem. However, stringresourcetool2 can help you solve this problem more elegantly and make full use of intelligent sensing technology to greatly reduce the chance of errors.

What is stringresourcetool2?

Stringresourcetool2 is a project on codeplex.com. It is designed to solve the resource problems related to language areas in. NET development. Its address is http://stringresourcetool.codeplex.com.

This project introduces a string file. The format of the string file was introduced by Microsoft's Enterprise Library at an early stage to facilitate your use of resources in applications.

From the project name, we can see that it is mainly used to solve the problem of string resource related to language. Generate a. resx File Based on the string file, and generate a category class to facilitate the use of classes to access resources.

It also supports typed parameters, for example, inserts {0}, {1}, which makes our solution perfect.

We know that the. NET standard resource file format is resx. The default generation tool is resxfilecodegenerator..
If your project needs to use string resources related to a specific language, you can try this tool to replace resxfilecodegenerator in Visual Studio 2005. In addition, it makes full use of the. NET smart sensing function, greatly reducing the chance of errors when searching strings.

However, you may have noticed that it also has limitations. If you need to provide different image resources for users in different countries, stringresourcetool2 cannot meet your needs. From its name, we can see that it is committed to solving string resources.

Confusions encountered during development

In our daily development, string resources are often used. For example, when a system error occurs, we need to prompt the user for an error. If the current user is a country of English language, our program needs to provide a prompt in English. If the user is used to a simplified Chinese language, we need to provide the corresponding error message in simplified Chinese, this is a user-friendly application.

In addition, you can benefit from this project even if your applications do not need users in multiple languages. Put your string-related resources in string format files for centralized management to facilitate maintenance. For example, in the past, I often wrote strings directly to various classes, or simply wrote them to the client when a prompt message pops up to the user. In this way, as the project enters the later stage, the string is everywhere in the application: the string resource with an exception prompt is thrown at the data access layer, when an error message is reported at the user interface layer, even the title of each form increases your maintenance costs. If you have been dealing with this problem during project development, from the very beginning of the project, it is necessary for you to continue to look at the following content in the workshop, it will give you a perfect solution to this problem.

How to Apply in development

First, add a text file named Sr. strings to your project.

Figure 1 Add a string File

Then, directly write the followingCode

[Strings]

Raw = raw string

Stringarg (string name) = with name argument {0}

As you can see in the code above, stringresourcetool2 supports typed parameters ).

This format is also frequently used in development.

Right-click the file, set the customtool attribute to stringresourcetool2 or srt2, and right-click Run custom tool

Figure 2 run the generation tool
The tool will generate a resx file and a sequence class Sr.

We can use the following code to access the string resource file we just added

String S1 = Sr. Raw;

String S2 = Sr. stringarg ("Microsoft ");

This is all you need to learn: Add a text file (string file), write the string you want to use in the project, generate a resource file, then you can apply it in your project.

If you want to know how it works, the following content will help you understand its principles.

How it works

Let's review how to use resource files in resx format in. net.

Add the following resource files to your project:


Figure 3 add a resource file

Use the following code to access the resource:

ResourceManager Rm = new ResourceManager ("sampleapplication. resourcesample. Sr", assembly. getexecutingassembly ());

String raw = RM. getstring ("Raw ");

String stringarg = string. Format ("{0} world", Rm. getstring ("stringarg "));

This is how we often use resource files.

Return to our stringresourcetool2 project. As you can see, everything is very simple. You have to add a string file in text format, and then run the tool to generate the corresponding file,

We can see that the string format file we added has the following features:

Figure 4 Structure of the generated file

Under each string file, an SR class file and a resx resource file are automatically generated.

The content of the SR. srt. resx file is as follows:

Figure 5 automatically generated resource files
It is the same as what we just added.

The SR. CS file looks as follows

Public partial class SR

{

Public static string raw

{

Get

{

Return keys_sr.getstring (keys_sr.raw );

}

}

Public static string stringarg (string name)

{

Return keys_sr.getstring (keys_sr.stringarg, new object [] {

Name });

}

Internal class keys_sr

{

Public static system. Resources. ResourceManager = new system. Resources. ResourceManager ("sampleapplication. multistrings. sr. srt", typeof (sampleapplication. multistrings. Sr). Assembly );

Public const string raw = "Raw ";

Public const string stringarg = "stringarg ";

Public static string getstring (string key, object [] ARGs)

{

String MSG = ResourceManager. getstring (Key, resources. cultureinfo );

MSG = string. Format (MSG, argS );

Return MSG;

}

}

Sr generates a field for each of our string resources, so that you can easily apply the. Net Smart sensing feature and greatly reduce the probability of errors.
All the implementation methods in the field call the getstring method of the keys_sr class.
The keys_sr embedded class inside the SR class defines a ResourceManager field. Its construction method is to pass in the path sampleapplication of the current resource. multistrings. sr. the name of the Assembly where the SRT and Sr classes are located. The getstring method of keys_sr extracts the resource from the resource file, and the getstring method of ResourceManager is directly converted to the getstring method. The resource name is the name of the resource we type, here, it is a constant field of the class.

All the secrets are here. We finally understood its principles.

Apply stringresourcetool2 in your project

Download and install the software package from the website, add a string file to your project, and set customtool to stringresourcetool2 or srt2.

If you need to specify resource files in a specific language, you can set

Sr. Strings

Sr. Strings-ZH-CN

Sr. Strings-ZH-TW

Do not forget to set customtool = srt2. to all string resource files, the tool will generate the category class from the string file. For string files in a specific language, the tool will generate only the resource file and not the category class.
If you are not familiar with the names of languages in different countries, open ie properties, tools à Internet Options, click the ages button on the General tab, and click Add.

Figure 6 Language and region options
The square brackets show the abbreviation of the languages of all countries.

String file skills

In the example we downloaded, we often see the following content:

# This file is used to generate Sr. CS and Sr. resx files. The copyright notice

# For those files appears here, in this Sr. Strings file.

# Options are specified as lines starting "#! "

# Comments are lines starting with ";" or "#"

# To define the SR class public instead of internal (default ):

##! Accessor_class_accessibility = Public

#! Culture_info = resources. cultureinfo

From the above content, we can know that the custom option starting with ";" or "#" starts #! .
For example: # comments this indicates Annotation
#! Accessor_class_accessibility = Public change the default access modifier internal of the SR class to public.

The benefits of this feature are obvious. We can compile resources separately into a set of programs without mixing them with applications, such as creating a uiresx project, set the compilation output to library. use this option to set the access modifier of the SR class to public. this facilitates the use of resources in other applications.

#! Culture_info = resources. cultureinfo indicates that the Set region is read from the current resources's cultureinfo.
If we do not want to specify the language region during runtime, this option allows us to specify the current language region during compilation ..

// The following two lines switch the current language to simplified Chinese

Thread. currentthread. currentuiculture = new system. Globalization. cultureinfo ("ZH-CN ");

Thread. currentthread. currentculture = new system. Globalization. cultureinfo ("ZH-CN ");

In addition, you can put all language-related resources in a string file, or distribute resources related to different languages in different files.

The following code demonstrates placing string resources of all languages in one file.

[Strings]

Raw = raw string

Stringarg (string name) = with name argument {0}

[Strings. ZH-cn]

Raw = original string

Stringarg (string name) = simplified Chinese character {0}

You can also put string resources of different languages into different files as follows.

Figure 7 place strings in different files

Put the current default language in the SR. Strings file, and put the Simplified Chinese Resources in the SR. Strings-ZH-CN file.

Summary

In string-related projects, you can make full use of stringresourcetool2 and smart sensing technology of. Net to build your applications conveniently and accurately. You can view the code to learn more about its usage.
It is recommended that you create a project that only contains string resources for your project and set the output to library for convenient maintenance and reuse.

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.