全球化在ASP.Net MVC 3 [英] Globalization in ASP.Net MVC 3

查看:149
本文介绍了全球化在ASP.Net MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现全球化/本地化在我的MVC 3应用程序。我不想为每种语言不同的看法。请建议我该怎么继续。任何支持链接/网址将有很大的帮助。

I am trying to achieve globalization/localization in my MVC 3 application. I don't want different Views for each language. Please suggest how I can proceed. Any supported links/URLs will be of great help.

推荐答案

您以同样的方式定位其为这样任何其他应用程序:

You localize it in the same way as any other application like this:


  1. 创建一个文件夹,例如叫它资源

  2. 右键单击该文件夹并添加类......选择资源文件。说它是任何你喜欢的如Strings.resx

  3. 在文件的属性,修改自定义工具为 PublicResXFile codeGenerator

  4. 填充用翻译键和值对资源文件(这将是默认的转换)

  5. {name}的.de.resx例如:
  6. 与他们以这种形式的文化的名义创建的其他资源Strings.de.resx

  7. (这是剃刀)破解Views文件夹打开web.config,这增加/configuration/system.web.webPages.razor/pages/namespaces:其中,添加命名空间=资源/> (假设资源中创建的资源文件夹的名称,并在种源文件本身没有更改默认的命名空间)。
    这一步意味着你不必你想引用一个翻译每次完全限定在视图中的资源类。

  8. 使用翻译到位文本在你的看法就像下面code:

  1. Create a folder, call it e.g. Resources
  2. Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
  3. Under the properties of file, change Custom Tool to be PublicResXFileCodeGenerator
  4. Populate the Resource file with Translation key and value pairs (this will be the default translation)
  5. Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
  6. (This is for Razor) crack open the web.config in the Views folder and add this to /configuration/system.web.webPages.razor/pages/namespaces: <add namespace="Resources" /> (assuming resources is the name of the folder you created the resources in and you haven't changed the default namespace on the resouce files themselves). This step means you don't have to fully qualify the resource classes in your views each time you want to reference a translation.
  7. Use the translations in place of text in your views like with the following code:

@Strings.MyString


字符串会被自动翻译,具体取决于CultureInfo.CurrentCulture的观点,但是这不是自动为您设置

Strings will be automatically translated in the view depending on CultureInfo.CurrentCulture but this is not set automatically for you

您将需要更改的CurrentCulture (可能在的Application_BeginRequest )。你怎么做,这是你的,它可能是一个路径值设置它,也可以读取用户的浏览器语言

You will need to change the CurrentCulture (potentially in Application_BeginRequest). How you do this is up to you, it could be a route value which sets it or you can read the user's browser language

您可以在 HttpContext.Current.Request.UserLanguages​​ 用户的prefered语言(按顺序)列表。

You can find a list of the user's prefered languages (in order) in HttpContext.Current.Request.UserLanguages.

这篇关于全球化在ASP.Net MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆