如何在ASP.NET上使用各种特定于文化的资源文件

4

我有一个网站,目前使用的是EN-US语言,并且我还有法语资源文件。我需要编写什么代码才能让应用程序使用法语版本的资源文件。

该网站是一个使用.NET的商务服务器和共享点网站。

如果有任何代码示例,将会非常好。

3个回答

3
您有几个选项。第一种选择是在应用程序级别上操作。您可以在web.config的system.web部分中完成此操作:
<globalization culture="fr-FR" uiCulture="fr-FR" />

另一个选项在页面上:

<%@Page Culture="fr-FR" Language="C#" %>

或者通过线程:

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

请参阅http://support.microsoft.com/kb/306162了解更多信息。


1

我认为主要的资源文件特性是能够根据URL中指定的区域设置检索本地化字符串,通过代码实现,例如:

localresourcestring = (String)GetLocalResourceObject("username")  
//will retrieve the localized string for username from your resx (according to the resx's name)!  

这里是一个很好的起点,包括演示和其他内容。


1
我会在 Global.asax 文件中的 Application_BeginRequest() 全局事件中设置语言:
protected void Application_BeginRequest()
{
    // Get the CultureInfo object that contains the French language specification
    CultureInfo frenchCulture = CultureInfo.CreateSpecificCulture("fr-FR");

    // Set the language the current thread uses (per-user)
    Thread.CurrentThread.CurrentCulture = frenchCulture;
    Thread.CurrentThread.CurrentUICulture = frenchCulture;
}

希望能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接