我即将开始一个项目,该项目必须支持多种欧洲语言。该网站上所有静态内容都必须在这些语言之间能够翻译。我听说过卫星程序集,听说它们被用于 .NET 中的多语言网站,但这是很久以前的事了。在 .NET 中,这是否仍然是当前做法?我正在使用 ASP.NET MVC。
我即将开始一个项目,该项目必须支持多种欧洲语言。该网站上所有静态内容都必须在这些语言之间能够翻译。我听说过卫星程序集,听说它们被用于 .NET 中的多语言网站,但这是很久以前的事了。在 .NET 中,这是否仍然是当前做法?我正在使用 ASP.NET MVC。
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";
然后在视图和局部视图中(例如MyView.cshtml)。
<span>@Resources.MyResources.TextID</span>
可选项:
资源可以作为链接添加,并使用自定义命名空间。
BuildAction: Embedded Resource
CopyToOutputDirectory: Copy always
CustomTool: PublicResXFileCodeGenerator
CustomToolNamespace: MyNamespaceHere
mToolNamespace: MyNamespaceHere
然后它们可以通过以下方式访问。
<span>@MyNamespaceHere.MyResources.TextID</span>