简体   繁体   English

如何创建多语言网站

[英]How to create a multi-lingual site

I am about to start on a project which must support a number of European languages. 我即将开始一个必须支持多种欧洲语言的项目。 All static content on this site must be translatable between these languages. 此站点上的所有静态内容必须可以在这些语言之间进行翻译。 I have heard of satellite assemblies and have heard that they are used for multi-language sites in .NET but this was a long time ago. 我听说过卫星装配,并听说它们用于.NET中的多语言站点,但这是很久以前的事了。 Is this still current practice in .NET? 这仍然是.NET中的当前做法吗? I'm using ASP.NET MVC. 我正在使用ASP.NET MVC。

If you're using ASP.NET MVC, one option would be to use a different resource for each view. 如果您使用的是ASP.NET MVC,则可以选择为每个视图使用不同的资源。 I wrote an article about it, maybe it can be of help: 我写了一篇关于它的文章,也许它可能有所帮助:

ASP.NET MVC Localization: Generate resource files and localized views using custom templates ASP.NET MVC本地化:使用自定义模板生成资源文件和本地化视图

Without the satellite part, you can add a App_GlobalResources folder to the project and add *.resx files for each language. 如果没有卫星部件,您可以将App_GlobalResources文件夹添加到项目中,并为每种语言添加*.resx文件。 You can have one resource file for the whole project, or one per ASP.NET MVC Area or however you see fit, but you don't need to have more then 1. 您可以为整个项目创建一个资源文件,或者每个ASP.NET MVC区域有一个资源文件,或者您认为合适,但是您不需要有超过1个资源文件。

App_GlobalResources App_GlobalResources文件

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

In MyResources.resx 在MyResources.resx中

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

In Global.asax.cs ( Application_PreRequestHandlerExecute ) 在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";

Then in the Views and PartialViews (eg MyView.cshtml). 然后在Views和PartialViews中(例如MyView.cshtml)。

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

Optional: 可选的:

The Resources can be added as a link, and use custom namespaces. 可以将Resources添加为链接,并使用自定义命名空间。

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

mToolNamespace: MyNamespaceHere mToolNamespace:MyNamespaceHere

Then they would be accessible via. 然后他们可以访问。

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

This is a general way to use (normal / linked) resources in ASP.NET MVC. 这是在ASP.NET MVC中使用(普通/链接)资源的一般方法。 If you use "Add as link" you can have one physical copy in a separate project. 如果使用“添加为链接”,则可以在单独的项目中拥有一个物理副本。

Satellite: 卫星:

Some info on satellite assemblies: 卫星装配的一些信息:

MSDN: Creating Satellite Assemblies MSDN:创建卫星装配

A MSDN Blog: Introduction to Satellite Assemblies MSDN博客:卫星装配简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM