Using enumeration type skillfully to implement multi-language switch of project

Source: Internet
Author: User

There are many ways to implement multiple languages in a project program, and the multi-lingual processing of enumeration types is a headache. For example, there is the following enumeration type:

 Public enum Mysex    {        0,        1    }

If you want to output this enumeration on the interface, the direct procedure is as follows:

Console.WriteLine ("sex:{0},{1}", Mysex.women,mysex.man);

Program output:

Sex:women,man

What if we want to output the Chinese name of an enumeration?

The usual practice is this:

 Public enum Mysex    {[Description (" female ")]         0 , [Description (" male ")]         1     }

Then reflect the enumeration items, get the contents of the declaration of the feature, probably using the look like this:

Console.WriteLine ("sex:{0},{1}",getenumdescription<mysex>(mysex.women),                                                 getenumdescription<MySex> (Mysex.man));

The Getenumdescription function is a feature that uses reflection to obtain an enumeration item description, specifically implemented slightly.

Has it not been found that the use of this method has violated the initial form of the enumeration we used? Add a method call.

Is there a better way?

Yes, it is the enumeration type that defines a Chinese language:

   Public enum Mysex    {        0,        1    }

However, the 2 gender enumeration types are defined in the project at the same time, and must be compiled. What do we do?

This is. NET's "conditional compilation" artifact appeared.

To manage the code conveniently, we add 2 files to the project:

    • Enum_lan_en.cs
    • Enum_lan_zh.cs

En's file represents an enumeration definition file for English, zh represents the enumeration definition file for Chinese, and in these 2 files, the enumeration is defined separately:

Enum_lan_en.cs:

namespace myenum{#if(lan_en)     Public enum Mysex    {        0,        1    }#endif}

Enum_lan_zh.cs:

namespace myenum{#if(Lan_zh)     Public enum Mysex    {        0,        1    }#endif}

At this point, you need to specify a conditional compilation on the assembly that matches the

Project Properties-"Generate-" general-"conditional compilation symbols: input

Lan_en

In this way, we use enumerations as follows in the main program to compile and use them properly:

Console.WriteLine ("sex:{0},{1}", Mysex.women,mysex.man);

Of course, if you want to use the Chinese enumeration in the main program, it doesn't matter,

Project Properties-"Generate-" general-"conditional compilation symbols: input

Lan_zh

Can.

Console.WriteLine ("sex:{0},{1}", Mysex Female, mysex. male);

Program output:

Sex: Female, male

Finally, when we need to deploy Chinese or because of the program, only need to modify this conditional compilation conformance, recompile the assembly containing the enumeration.

Enumeration method of multi-language problem, is not very simple?

If it's a classmate who thinks, I might ask, I modified the conditional compilation symbol for the assembly containing the enumeration type definition, why does the main program work correctly without error?

This involves the compiler's handling of enumeration types, which, when compiled, are directly replaced with the values of the enumerated items, so when using an enumeration type, you cannot arbitrarily change the order and value of the enumeration items, or you may have an error if you do not arbitrarily reduce the enumeration items.

Using enumeration type skillfully to implement multi-language switch of project

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.