Set the culture of the current thread
Public static void SetCurrentThreadCultureInfo (string name)
{
Properties. Resource. Culture = new System. Globalization. CultureInfo (name );
Var newName = name;
Switch (Properties. Resource. Culture. Name)
{
Case "zh-CHS ":
NewName = "zh-CN ";
Break;
Case "zh-CHT ":
NewName = "zh-TW ";
Break;
Default:
Break;
}
System. Threading. Thread. CurrentThread. CurrentUICulture = System. Globalization. CultureInfo. CreateSpecificCulture (newName );
}
Note:
The following code:
System. Threading. Thread. CurrentThread. CurrentCulture = new CultureInfo ("zh-CHS ");
System. Threading. Thread. CurrentThread. CurrentCulture = new CultureInfo ("en ");
The preceding two statements report exceptions during execution:
If it is modified as follows:
System. Threading. Thread. CurrentThread. CurrentCulture = CultureInfo. CreateSpecificCulture ("en ");
System. Threading. Thread. CurrentThread. CurrentCulture = CultureInfo. CreateSpecificCulture ("zh-CHS ");
Code 1 can be executed normally, and Code 2 has the following error:
Why does the above Code appear?
Here we will talk about: regional, fixed regional, non-specific regional, specific regional
The regional name follows the RFC 1766 standard in the format of <symbol agecode2>-<country/regioncode2> ", <languagecode2> is a code derived from ISO 639-1 consisting of two lower-case letters. <country/regioncode2> is a code derived from ISO 3166 consisting of two upper-case letters. For example, the American English is "en-US ". When the dual-letter code is unavailable, it uses a tri-letter code derived from ISO 639-2. For example, the tri-letter code "div" is used for the region where the Dhivehi language is used. Some regional names are suffixed with the specified writing symbol; for example, "-Cyrl" specifies the Spanish writing symbol, and "-Latn" specifies the Latin Writing symbol.
Fixed regions do not distinguish between regions. You can use an empty string ("") to specify a fixed culture by name or by the Regional identifier 0x007F. A fixed culture is represented by the InvariantCulture attribute of the CultureInfo class. A fixed region is only associated with the English language and is not associated with any country or region. It can be used in almost all the methods in the "regionalized" namespace that requires a culture. If your program performs string comparison or case-insensitive modification, you should use InvariantCulture to ensure that regardless of the system settings, the behavior will be completed according to the fixed culture of the English language represented by InvariantCulture.
A non-specific region is a region associated with a language but not a country/region. A specific region is a region associated with a language and a country/region. For example, "en" is not a specific region, and "en-US" is a specific region. Note that "zh-CHS" (Simplified Chinese) and "zh-CHT" (Traditional Chinese) are non-specific regions.
A region has a hierarchical structure, that is, the parent level of a specific region is non-specific, and the parent level of a non-specific region is InvariantCulture. The Parent attribute of the CultureInfo class returns a non-specific culture associated with a specific culture. If resources in a specific region do not exist in the system or are unavailable for other reasons, use resources in a non-specific region. If resources in a non-specific region are not available, use the resources embedded in the main program.
What are the similarities and differences between the following two statements:
Var cultureSpecific = CultureInfo. CreateSpecificCulture (name );
Var culture = new CultureInfo (name );
The first method can only be used to create a CultureInfo instance in a fixed or specific region. If the name is a Null String, an instance of the fixed culture is created. If the name is not a specific culture, the default CultureInfo instance of the specific culture associated with the name is created. The second method is to create a CultureInfo instance for the region specified by name. It can be fixed, non-specific, or specific.
Although "en" and "zh-CHS" are both non-fixed areas, "en" is associated with a specific area by default, neither "zh-CHS" nor "zh-CHT" are associated with a specific region by default. Therefore, the code in the fourth section above reports an error.