如何创建多语言网站

时间:2012-11-22 16:25:48

标签: c# .net asp.net-mvc localization satellite-assembly

我即将开始一个必须支持多种欧洲语言的项目。此站点上的所有静态内容必须可以在这些语言之间进行翻译。我听说过卫星装配,并听说它们用于.NET中的多语言站点,但这是很久以前的事了。这仍然是.NET中的当前做法吗?我正在使用ASP.NET MVC。

2 个答案:

答案 0 :(得分:4)

如果您使用的是ASP.NET MVC,一种选择是为每个视图使用不同的资源。我写了一篇关于它的文章,也许它可以提供帮助:

ASP.NET MVC Localization: Generate resource files and localized views using custom templates

答案 1 :(得分:4)

如果没有卫星部分,您可以向项目添加App_GlobalResources文件夹,并为每种语言添加*.resx个文件。您可以为整个项目创建一个资源文件,或者每个ASP.NET MVC区域有一个资源文件,或者您认为合适,但是您不需要有超过1个。

App_GlobalResources文件

MyResources.resx        (Neutral / Default language translated texts)
MyResources.en-GB.resx
MyResources.de-DE.resx

在MyResources.resx

Name    Value
TextID  Some text which will be localized to the end user.

在Global.asax.cs(Application_PreRequestHandlerExecute

// Set it according to cookies/Session etc.
System.Threading.Thread.CurrentThread.CurrentUICulture = "de-DE"; 
System.Threading.Thread.CurrentThread.CurrentCulture = "de-DE";

然后在Views和PartialViews中(例如MyView.cshtml)。

<span>@Resources.MyResources.TextID</span>

可选

可以将资源添加为链接,并使用自定义命名空间。

BuildAction:           Embedded Resource
CopyToOutputDirectory: Copy always
CustomTool:            PublicResXFileCodeGenerator
CustomToolNamespace:   MyNamespaceHere

mToolNamespace:MyNamespaceHere

然后可以通过。

访问它们
<span>@MyNamespaceHere.MyResources.TextID</span>

这是在ASP.NET MVC中使用(普通/链接)资源的一般方法。如果使用“添加为链接”,则可以在单独的项目中拥有一个物理副本。

<强>卫星

有关附属装配的一些信息:

MSDN: Creating Satellite Assemblies

A MSDN Blog: Introduction to Satellite Assemblies