asp.net-mvc 如何本地化 ASP.NET MVC 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/192465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 22:47:13  来源:igfitidea点击:

How to localize ASP.NET MVC application?

asp.net-mvclocalizationglobalization

提问by pirho

What would be best practice to localize your ASP.NET MVC application?

本地化 ASP.NET MVC 应用程序的最佳实践是什么?

I would like to cover two situations:

我想涵盖两种情况:

  • one application deployment in IIS which would handle multiple languages
  • one language/application deployment.
  • IIS 中的一个应用程序部署,可以处理多种语言
  • 一种语言/应用程序部署。

In first situation should you go with some kind of view based thing like, ~/View/EN, ~/View/FI, ~/View/SWE or something different?

在第一种情况下,你应该使用某种基于视图的东西,比如 ~/View/EN、~/View/FI、~/View/SWE 还是其他东西?

What about second case, just application based config via Web.config and point these different languages to different URLs?

第二种情况如何,只是通过 Web.config 进行基于应用程序的配置并将这些不同的语言指向不同的 URL?

采纳答案by fyasar

You can also take a look here ASP.NET MVC 2 Localization complete guideand ASP.NET MVC 2 Model Validation With Localizationthese entires will help you if you working with ASP.NET MVC 2.

您还可以在此处查看ASP.NET MVC 2 本地化完整指南ASP.NET MVC 2 模型验证与本地化,如果您使用 ASP.NET MVC 2,这些内容将对您有所帮助。

回答by Elijah Manor

You would localize your ASP.NET MVC application very much in the same way you would a classic ASP.NET Web Form application.

您将本地化 ASP.NET MVC 应用程序的方式与处理经典 ASP.NET Web 窗体应用程序的方式非常相似。

You wouldn't use different pages/views for each language, but each page would support multiple languages using satellite assemblies.

您不会为每种语言使用不同的页面/视图,但每个页面都将使用附属程序集支持多种语言。

You can look at Matt Hawley's blog entryfor more explanation and examples.

您可以查看Matt Hawley 的博客条目以获取更多解释和示例。

回答by kevintechie

Unfortunately, Matt Hawley's original code doesn't work in the release version of ASP.NET MVC. Check out an updated post: http://blog.eworldui.net/post/2008/10/ASPNET-MVC-Localization-via-View-Engines.aspx

不幸的是,Matt Hawley 的原始代码在 ASP.NET MVC 的发布版本中不起作用。查看更新的帖子:http: //blog.eworldui.net/post/2008/10/ASPNET-MVC-Localization-via-View-Engines.aspx

In general, the localization process isn't as smooth in the VS 2008 / ASP.NET MVC world as it is with traditional web forms. http://www.guysmithferrier.com/post/2009/05/Localizing-ASPNET-MVC.aspx

通常,本地化过程在 VS 2008 / ASP.NET MVC 世界中并不像在传统 Web 表单中那样顺利。http://www.guysmithferrier.com/post/2009/05/Localizing-ASPNET-MVC.aspx

回答by Joel Cunningham

Have a look at Rob Connery's MvcStore project. He does a screencast showing one way to solve the globalization problem.

看看 Rob Connery 的 MvcStore 项目。他做了一个截屏视频,展示了解决全球化问题的一种方法。

http://wekeroad.com/2008/04/24/mvcstore-part-5

http://wekeroad.com/2008/04/24/mvcstore-part-5

回答by Ian T

I've never been convinced of handling localization within a form as Elijah suggests - the different lengths and directions can lead to very complex or vary naff looking forms.

我从来没有像 Elijah 建议的那样在表单中处理本地化 - 不同的长度和方向可能导致非常复杂或不同的 naff 外观形式。

I'm only just starting with MVC but taking the decoupling method you would wan to use the same controller regardless of the language (treating language just as a view) - this would then give you /Controller/Action/language/form

我只是从 MVC 开始,但采用解耦方法,无论语言如何,您都希望使用相同的控制器(将语言视为视图)-这将为您提供 /Controller/Action/language/form

回答by user405803

There is good tutorial with recent update on How to localize asp.net mvc application covering all aspects including DisplayName localization, Validation, using Routing (storing culture name in URL), issues with output cache and so on... Alex Adamyan Blog - While my keyboard gently weeps

关于如何本地化 asp.net mvc 应用程序的最新更新有很好的教程,涵盖所有方面,包括 DisplayName 本地化、验证、使用路由(在 URL 中存储区域性名称)、输出缓存问题等...... Alex Adamyan 博客 - 虽然我的键盘轻轻地哭泣

回答by Michael Sander

We actually went a complete different with overriding the DataAnnotationsMetadaDataProvider. In there you can make sure that the DisplayNameAttribute values are resolved into the correct language. Actually you could even get rid of that attribute and resolve by field names only if that would be any help.

我们实际上完全不同,覆盖了 DataAnnotationsMetadaDataProvider。在那里,您可以确保将 DisplayNameAttribute 值解析为正确的语言。实际上,只有在有帮助的情况下,您甚至可以摆脱该属性并按字段名称解析。

回答by Vlad

I wrote this article a while ago. It uses a custom view engine. http://blog.oimae.com/2011/02/20/cultured-view-engine-for-mvc/

不久前我写了这篇文章。它使用自定义视图引擎。 http://blog.oimae.com/2011/02/20/cultured-view-engine-for-mvc/

回答by Nikola

And another tutorial for different solution

以及针对不同解决方案的另一个教程

Tutorial: Create ASP.NET MVC localization with language detection

教程:使用语言检测创建 ASP.NET MVC 本地化