asp.net-mvc ASP.NET MVC 应用程序中的多种语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4034/
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:43:39  来源:igfitidea点击:

Multiple languages in an ASP.NET MVC application?

asp.net-mvcinternationalizationmultilingual

提问by Lance Fisher

What is the best way to support multiple languages for the interface in an ASP.NET MVC application? I've seen people use resource files for other applications. Is this still the best way?

为 ASP.NET MVC 应用程序中的界面支持多种语言的最佳方法是什么?我见过人们将资源文件用于其他应用程序。这仍然是最好的方法吗?

采纳答案by Haacked

If you're using the default view engines, then local resources work in the views. However, if you need to grab resource strings within a controller action, you can't get local resources, and have to use global resources.

如果您使用默认视图引擎,则本地资源在视图中工作。但是,如果您需要在控制器操作中获取资源字符串,则无法获取本地资源,而必须使用全局资源。

This makes sense when you think about it because local resources are local to an aspx page and in the controller, you haven't even selected your view.

当您考虑它时,这是有道理的,因为本地资源对于 aspx 页面是本地的,并且在控制器中,您甚至还没有选择您的视图。

回答by user10479

I found this resourceto be very helpful

我发现这个资源非常有帮助

Its a wrapper round the HttpContext.Current.GetGlobalResourceStringand HttpContext.Current.GetLocalResourceStringthat allows you to call the resources like this...

它是HttpContext.Current.GetGlobalResourceStringHttpContext.Current.GetLocalResourceString的包装器,允许您像这样调用资源...

// default global resource
Html.Resource("GlobalResource, ResourceName")

// global resource with optional arguments for formatting
Html.Resource("GlobalResource, ResourceName", "foo", "bar")

// default local resource
Html.Resource("ResourceName")

// local resource with optional arguments for formatting
Html.Resource("ResourceName", "foo", "bar")

The only problem I found is that controllers don't have access to local resouce strings.

我发现的唯一问题是控制器无权访问本地资源字符串。

回答by Nick Berardi

Yes resources are still the best way to support multiple languages in the .NET environment. Because they are easy to reference and even easier to add new languages.

是的,资源仍然是在 .NET 环境中支持多种语言的最佳方式。因为它们易于参考,甚至更容易添加新语言。

Site.resx
Site.en.resx
Site.en-US.resx
Site.fr.resx
etc...

So you are right still use the resource files.

所以你仍然使用资源文件是对的。

回答by Brady Moritz

The Orchard project uses a shortcut method called "T" to do all in-page string translations. So you'll see tags with a @T("A String to Translate").

Orchard 项目使用称为“T”的快捷方法来执行所有页内字符串翻译。所以你会看到带有@T("A String to Translate") 的标签。

I intend to look at how this is implemented behind the scenes and potentially use it in future projects. The short name keeps the code cleaner since it will be used a lot.

我打算看看这是如何在幕后实现的,并可能在未来的项目中使用它。短名称使代码更简洁,因为它将被大量使用

What I like about this approach is the original string (english, in this case) is still easily visible in the code, and doesnt require a lookup in a resource tool or some other location to decode what the actual string should be here.

我喜欢这种方法的是原始字符串(在这种情况下是英语)在代码中仍然很容易看到,并且不需要在资源工具或其他位置中查找来解码这里的实际字符串。

See http://orchardproject.netfor more info.

有关更多信息,请参阅http://orchardproject.net

回答by ADB

Some of the other solutions mentioned as answer do not work for the released version of MVC (they worked with previous versions of alpha/beta).

作为答案提到的其他一些解决方案不适用于已发布的 MVC 版本(它们适用于先前版本的 alpha/beta)。

Here is a good article describing a way to implement localization that will be strongly-typed and will not break the unit testing of controllers and views: localization guide for MVC v1

这是一篇很好的文章,描述了一种实现本地化的方法,该方法将是强类型的并且不会破坏控制器和视图的单元测试:MVC v1 的本地化指南

回答by datajohnson

This is another option, and you'll have access to the CurrentUICulture in the controller:

这是另一种选择,您可以访问控制器中的 CurrentUICulture:

Check MVC3-multi-language

检查MVC3-多语言