全球化在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:
- 创建一个文件夹,例如叫它资源
- 右键单击该文件夹并添加类......选择资源文件。说它是任何你喜欢的如Strings.resx
- 在文件的属性,修改自定义工具为
PublicResXFile codeGenerator
- 填充用翻译键和值对资源文件(这将是默认的转换)
- 与他们以这种形式的文化的名义创建的其他资源Strings.de.resx
- (这是剃刀)破解Views文件夹打开web.config,这增加/configuration/system.web.webPages.razor/pages/namespaces:其中,添加命名空间=资源/> (假设资源中创建的资源文件夹的名称,并在种源文件本身没有更改默认的命名空间)。
这一步意味着你不必你想引用一个翻译每次完全限定在视图中的资源类。 -
使用翻译到位文本在你的看法就像下面code:
{name}的.de.resx例如:
- Create a folder, call it e.g. Resources
- Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
- Under the properties of file, change Custom Tool to be
PublicResXFileCodeGenerator
- Populate the Resource file with Translation key and value pairs (this will be the default translation)
- Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
- (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.
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
.