Asp.net MVC internationalization (2) Problem Solving

Source: Internet
Author: User

Solve the remaining problems raised in the previous article

 

Frontend internationalization. solved through JS Language Pack.

Prepare the JS Language Pack. Use the following on the template page:Code

 
<Script SRC= 'HTTP: // www.cnblogs.com/scripts/grid.locale-<% = system. Threading. thread. currentthread. currentculture. name %>. js' Type= "Text/JavaScript"> </Script>

Note the Language Pack name
The prompt language output in the background is internationalized. (For example, "this user name does not exist ")

Simple Method: Write the statement directly to the global resource file and use the resource class for access.

Complexity: Summarize statements into XML files and encapsulate them for use.

1. Add a global resource file

2. Add the following XML file to the global resource Folder:

<?XML Version= "1.0" Encoding= "UTF-8"?>
 
<Root>
 
<Text Key= "Usernameexist" Zh-CN= "The user name already exists" En-US= "Username is already exist! "> </Text>
 
</Root>

3. Provides the category class

 
// Enumeration class
 
Public Static ClassResponsekey {
Public Static Const StringUsernameexist ="Usernameexist";
 
}
 
 
 
Private StaticXelement root = xelement. parse (responseintel. responseintelxml );
 
Private Static StringCachekey ="Responseintelxml";
 
Public Static StringLang (StringKey ){
If(Httpcontext. Current. cache. Get (cachekey + key) =Null)
 
{
 
VaR nodes = (from ElInRoot. Elements ("Text")
 
WhereEl. Attribute ("Key"). Value = Key
 
Select El). First ();
 
StringRet = nodes. Attribute (system. Threading. thread. currentthread. currentculture. Name). value;
 
// System. Web. caching. cachedependency dependency = new system. Web. caching. cachedependency (
// Httpcontext. Current. server. mappath ("~ /App_globalresources/responseintelxml. xml "));
 
Httpcontext. Current. cache [cachekey + key] = ret;
 
ReturnRET;
 
}
 
Else
 
Return(String) Httpcontext. Current. cache. Get (cachekey + key );
 
}

4. Access

 
Common. respalertintel _. Lang (common. respalertintel _. responsekey. usernameexist)

ASP. net mvc custom verification prompt message Internationalization

1. Create a global resource file. Add a key/value to loginmodel_username: 'user name cannot be blank'

2. The characteristics of this model are similar to the following:

 
[Required (errormessageresourcetype =Typeof(Resource class name), errormessageresourcename ="Loginmodel_username")]
[Displayname ("User Name")]
 
Public StringUsername {Get; set ;}

Status enumeration internationalization (for example, "normal" or "STOPPED)

Attribute feature used

// Feature classes and enumeration
 
[Attributeusage (attributetargets. All, allowmultiple =True)]
 
Public ClassLanguageattribute: attribute {
 
PublicLanguageattribute (StringLanguagekey,StringLanguagevalue)
 
{
 
Languagekey = languagekey; languagevalue = languagevalue;
 
}
 
Public StringLanguagekey {Get; set ;}
 
Public StringLanguagevalue {Get; set ;}
 
}
Public EnumState {
 
[Language ("ZH-CN","Error")]
 
[Language ("En-us","Error !!! ")]
 
Error = 0,
 
[Language ("ZH-CN","Success")]
 
[Language ("En-us","Yes !!! ")]
 
OK = 1
 
}

 
// Core processing method
 
Public StringGetstr (State state ){
VaR ATTR = state. GetType (). getfield (State. tostring (). getcustomattributes (False);
 
StringRet ="";
 
Foreach(VAR itemInATTR)
 
{
 
VaR lang = (languageattribute) item );
 
If(System. Threading. thread. currentthread. currentculture. Name. tolower () = Lang. languagekey. tolower ()){
 
Ret = Lang. languagevalue;
 
Break;
 
}
 
}
ReturnRET;
 
}

Test.

 
Response. Write ("Multiple Languages <br> Chinese now <br>");
Response. Write (getstr (State. Error); response. Write (getstr (State. OK ));
 
Response. Write ("Multiple Languages <br> now Ying <br>");
 
System. Threading. thread. currentthread. currentculture = system. Globalization. cultureinfo. createspecificculture ("En-us");
 
Response. Write (getstr (State. Error); response. Write (getstr (State. OK ));

 

International Database Text

Create a resource table

 
Select[CRID]
 
, [Cultureresourcecode]
 
, [Culturename]
 
, [Resourcename]
, [Status]
 
From[Boss]. [DBO]. [cultureresource]
 
Go

CRID, auto-increment field, primary key cultureresourcecode: Resource Key culturename: Asp.net Program The language region value of the current thread resourcename: Resource Value status: whether the status is enabled (0, 1). Add a foreign key for each field of the required text.

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.