How does Internationalization in ASP.NET work?

2019-05-29 09:23发布

问题:

I wonder if it is possible to make your application multilingual by simply creating resource files for every required language
Like

Resource.resx for English      //string abc(name)=xyz(value)
Resource.zh.resx for Chinese   //string abc(name)=zh(value)

And simply placing a string in your view(single view only that support multilingual) string like

@appName.Resource.abc

and

<globalization culture="en-GB" uiCulture="auto:en-GB" />

in Web.Config

Now my question is

Is this enough to get started with multilingual sites i.e if I change the preferred language in my browser to Chinese the content of page is changing? But how does this work?

What I know is

  • Browser returns preferred culture list

Need to know - How mapping to particular resource file take place. I mean both resource files (Resource.resx and Resource.zh.resx) in my example have an 'abc' property with different value. How does asp.net figure out which value to render? Is there any naming convention?

回答1:

At run time, ASP.NET uses the resource file that is the best match for the setting of the CurrentUICulture property. The UI culture for the thread is set according to the UI culture of the page. For example, if the current UI culture is Spanish, ASP.NET uses the compiled version of the WebResources.es.resx file. If there is no match for the current UI culture, ASP.NET uses resource fallback. It starts by searching for resources for a specific culture. If those are not available, it searches for the resources for a neutral culture. If these are not found, ASP.NET loads the default resource file. In this example, the default resource file is WebResource.resx.

Ref: ASP.NET Web Page Resources Overview

Microsoft .NET Internationalization