当前位置:网站首页>ASP.NET Mvc5 + ef6 + easyUI background management system (89) - internationalization, localization, multi language application

ASP.NET Mvc5 + ef6 + easyUI background management system (89) - internationalization, localization, multi language application

2021-01-24 11:14:55 ymnets

The opening

I wrote a multi language application in my early years :    localization ( Multilingual )    Describes how to create a multilingual resource file , And use the resource file to get the language properties of the page and request

This supplement to this article , The reason for this is that in actual projects , There are many situations that require multilingualism

such as : The log should be displayed according to the current language

And some scenarios are more complex , We could key It's extracted from the data , Or some error prompts and display of model interface fields

Now let's review and add some practical applications

1. Multilingual use

Basic use of multi language , Please jump to the connection I wrote in the early years of the beginning   localization ( Multilingual ) 

The multilingual namespace is using System.Threading;

So on the back end .cs Page or front view , You have to quote  System.Threading

2. Get the name of the current multilingual

string info = System.Threading.Thread.CurrentThread.CurrentCulture.Name;

Get the language name of the resource file en-US  /  zh-CN  / zh-TW

3. Change the language of the current request

For example, when the user requests, the language is zh-CN, That is Chinese , Through the following 3 Line code can be set to change the current request language

CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

Sample code

public ActionResult Create(string name)
{
    int a=1;

    CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
    Thread.CurrentThread.CurrentCulture = culture;
    Thread.CurrentThread.CurrentUICulture = culture;

     return View(Resource. Create success );
}

When not in use , return : Create success   When used, returns :Insert Succeed

3. Usage mode

Because the back-end code of the resource file is also a class ,Key Attribute , It's very easy to use

string value1 = Resource.Key00001;
string value2 = Resource.Key00002;

4. Use scenario 1

Take a chestnut :

For example, I want to write a diary : among full name And the phone is a variable

   Zhang San successfully created a customer information , Customer name : Li Si , Customer phone :13800138000

Then you can use formatting to fill in the display , In the log , Just store the value

string.Format(Resource.Key0001, " Li Si ", "138XXXXXXXX");

5. adopt Key The reflected value

If you don't like the scene above , Then you can use the following method , take key With the value to the database

for instance : Table data -   Key00002: Zhang San

Then it can be extracted by asset management manager Key To reflect

ResourceManager resMan = new ResourceManager(typeof(Resource));
string val = resMan.GetString("Key000002");

6.MVC Comments on model properties in

Annotations are mainly used for 2 A place to

  • 1. Errors in model validation
  • 2.DisplayFor Interface display

Let's take an example :

4 individual key Middle front 2 One is an error message

So that's what our model does

[Display(Name = "Key00003", ResourceType = typeof(Resource))]
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "Key00001")]
 public string Name { get; set; }

Then you will get a non empty through translation prompt and a front-end display translation processing , This MVC It's the simplest and the most perfect

@Html.LabelFor(model => model.Name)
 Get the field 
@Html.ValidationMessageFor(model => model.Name)
 Error message 

 

Last

If your system is used in many countries , Or a system with many people in different areas using

So the knowledge of this article is very practical , Because by dealing with

It can make some fixed general parts of the data originally in the database also get perfect translation

 

版权声明
本文为[ymnets]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210124111336523q.html

随机推荐