Asp.net 从特定于语言的资源文件中获取值,而不更改站点语言

Asp.net 从特定于语言的资源文件中获取值,而不更改站点语言,asp.net,resources,cultureinfo,currentculture,Asp.net,Resources,Cultureinfo,Currentculture,我有几个资源文件,例如 default.aspx.resx,default.aspx.nl.resx,default.aspx.en.resx 现在,当我在荷兰域名上时,会加载default.aspx.nl.resx。 但是现在我想从default.aspx.en.resx访问该值,并获取属于name“title”的英文值 现在,我可以通过更改当前区域性服务器端,访问该值,然后再将其更改回来来实现这一点,如下所示: Dim culture As CultureInfo = New Culture

我有几个资源文件,例如

default.aspx.resx,default.aspx.nl.resx,default.aspx.en.resx

现在,当我在荷兰域名上时,会加载default.aspx.nl.resx。 但是现在我想从default.aspx.en.resx访问该值,并获取属于name“title”的英文值

现在,我可以通过更改当前区域性服务器端,访问该值,然后再将其更改回来来实现这一点,如下所示:

Dim culture As CultureInfo = New CultureInfo("en")
Threading.Thread.CurrentThread.CurrentCulture = culture
Threading.Thread.CurrentThread.CurrentUICulture = culture
Dim title as String = GetLocalResourceObject("title")
culture = New CultureInfo("nl")
Threading.Thread.CurrentThread.CurrentCulture = culture
Threading.Thread.CurrentThread.CurrentUICulture = culture

但是有更好/更快的方法吗?最好不必更改当前线程的区域性,这样我就可以定义要访问的资源文件和语言?

您可以在参数中添加目标区域性

GetLocalResourceObject("title","YourCulture");

链接:

您可以将参数添加到您的目标文化中

GetLocalResourceObject("title","YourCulture");
链接:

编辑:(很抱歉,我不知道您需要另一种与此不同的方法,但这是我唯一能做到的方法:)

我通过以下方式做到了这一点:

var userLanguage = "en-US";

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage);

HttpContext.GetGlobalResourceObject("MyAppResource", "KeyThatIWantToGet");
其中MyAppResource是您的.resx文件的命名方式以及我想要设置的键。

编辑:(很抱歉,我不知道您想要另一种不同于此的方法,但这是我唯一能做到的方法:)

我通过以下方式做到了这一点:

var userLanguage = "en-US";

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(userLanguage);

HttpContext.GetGlobalResourceObject("MyAppResource", "KeyThatIWantToGet");

其中MyAppResource是您的.resx文件的命名方式和密钥,我将对其进行解释。

如果不使用HttpContext(通用.NET应用程序),我将使用以下帮助程序:

/// <summary>
/// Disposable class that lets us assume a specific culture while executing 
/// a certain block of code. You'd typically use it like this:
/// 
/// using (new CultureContext("de"))
/// {
///     // Will return the German translation of "Please click here"
///     string text = SharedResource.Please_click_here;
/// }
/// </summary>
public class CultureContext : IDisposable
{
    private readonly CultureInfo _previousCulture;
    private readonly CultureInfo _previousUiCulture;

    public CultureContext(CultureInfo culture)
    {
        // Save off the previous culture (we'll restore this on disposal)
        _previousCulture = Thread.CurrentThread.CurrentCulture;
        _previousUiCulture = Thread.CurrentThread.CurrentUICulture;
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
    }

    public CultureContext(string cultureCode)
        : this(new CultureInfo(cultureCode))
    {

    }

    /// <summary>
    /// Syntactic sugar so that we can switch to a culture context as follows:
    /// 
    /// using (CultureContext.For("de"))
    /// {
    ///     // Will return the German translation of "Please click here"
    ///     string text = SharedResource.Please_click_here;
    /// }
    /// </summary>
    public static CultureContext For(string cultureCode)
    {
        return new CultureContext(cultureCode);
    }

    public void Dispose()
    {
        // Restore the culture settings that were in place before switching
        // to this context
        Thread.CurrentThread.CurrentCulture = _previousCulture;
        Thread.CurrentThread.CurrentUICulture = _previousUiCulture;
    }
}
//
///一次性类,允许我们在执行时采用特定的区域性
///某个代码块。您通常会这样使用它:
/// 
///使用(新的CultureContext(“de”))
/// {
/////将返回“请单击此处”的德语翻译
///string text=SharedResource。请单击此处;
/// }
/// 
公共类CultureContext:IDisposable
{
私有只读文化信息(previousCulture);
私有只读文化信息(previousUiCulture);
公共文化文本(文化信息文化)
{
//保存以前的区域性(我们将在处置时恢复此区域性)
_previousCulture=Thread.CurrentThread.CurrentCulture;
_previousUiCulture=Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentCulture=区域性;
Thread.CurrentThread.CurrentUICulture=区域性;
}
公共CultureContext(字符串cultureCode)
:此(新文化信息(文化代码))
{
}
/// 
///语法糖,以便我们可以切换到文化背景,如下所示:
/// 
///使用(CultureContext.For(“de”))
/// {
/////将返回“请单击此处”的德语翻译
///string text=SharedResource。请单击此处;
/// }
/// 
用于的公共静态CultureContext(字符串cultureCode)
{
返回新的CultureContext(cultureCode);
}
公共空间处置()
{
//恢复切换前的区域性设置
//在这种情况下
Thread.CurrentThread.CurrentCulture=\u previousCulture;
Thread.CurrentThread.CurrentUICulture=\u previousUiCulture;
}
}

当不使用HttpContext(通用.NET应用程序)时,我使用以下帮助程序:

/// <summary>
/// Disposable class that lets us assume a specific culture while executing 
/// a certain block of code. You'd typically use it like this:
/// 
/// using (new CultureContext("de"))
/// {
///     // Will return the German translation of "Please click here"
///     string text = SharedResource.Please_click_here;
/// }
/// </summary>
public class CultureContext : IDisposable
{
    private readonly CultureInfo _previousCulture;
    private readonly CultureInfo _previousUiCulture;

    public CultureContext(CultureInfo culture)
    {
        // Save off the previous culture (we'll restore this on disposal)
        _previousCulture = Thread.CurrentThread.CurrentCulture;
        _previousUiCulture = Thread.CurrentThread.CurrentUICulture;
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
    }

    public CultureContext(string cultureCode)
        : this(new CultureInfo(cultureCode))
    {

    }

    /// <summary>
    /// Syntactic sugar so that we can switch to a culture context as follows:
    /// 
    /// using (CultureContext.For("de"))
    /// {
    ///     // Will return the German translation of "Please click here"
    ///     string text = SharedResource.Please_click_here;
    /// }
    /// </summary>
    public static CultureContext For(string cultureCode)
    {
        return new CultureContext(cultureCode);
    }

    public void Dispose()
    {
        // Restore the culture settings that were in place before switching
        // to this context
        Thread.CurrentThread.CurrentCulture = _previousCulture;
        Thread.CurrentThread.CurrentUICulture = _previousUiCulture;
    }
}
//
///一次性类,允许我们在执行时采用特定的区域性
///某个代码块。您通常会这样使用它:
/// 
///使用(新的CultureContext(“de”))
/// {
/////将返回“请单击此处”的德语翻译
///string text=SharedResource。请单击此处;
/// }
/// 
公共类CultureContext:IDisposable
{
私有只读文化信息(previousCulture);
私有只读文化信息(previousUiCulture);
公共文化文本(文化信息文化)
{
//保存以前的区域性(我们将在处置时恢复此区域性)
_previousCulture=Thread.CurrentThread.CurrentCulture;
_previousUiCulture=Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentCulture=区域性;
Thread.CurrentThread.CurrentUICulture=区域性;
}
公共CultureContext(字符串cultureCode)
:此(新文化信息(文化代码))
{
}
/// 
///语法糖,以便我们可以切换到文化背景,如下所示:
/// 
///使用(CultureContext.For(“de”))
/// {
/////将返回“请单击此处”的德语翻译
///string text=SharedResource。请单击此处;
/// }
/// 
用于的公共静态CultureContext(字符串cultureCode)
{
返回新的CultureContext(cultureCode);
}
公共空间处置()
{
//恢复切换前的区域性设置
//在这种情况下
Thread.CurrentThread.CurrentCulture=\u previousCulture;
Thread.CurrentThread.CurrentUICulture=\u previousUiCulture;
}
}

谢谢……您能提供一个如何使用它的示例吗?我在你提供的页面上没有看到任何例子。再次感谢!我尝试了你的建议:GetLocalResourceObject(“title”,“en”),但这不起作用。我将ASP.NET 4.5 btwtry与GetLocalResourceObject(“title”、“en-en”)一起使用。问题在于,我只传递了2个参数……我得到了错误:重载解析失败,因为没有可访问的“GetLocalResourceObject”接受此数量的参数。但是我不知道如何调用它,然后访问返回的值。啊,显然我没有。我现在将其更改为:lblStatus.Text=HttpContext.GetLocalResourceObject(“~/freemediadetails.aspx.nl.resx”,“backtosearchresults”)。ToString但随后出现以下错误:找不到任何适合指定区域性或非特定区域性的资源。确保“freemediadetails.aspx.nl.resx.resources”在编译时已正确嵌入或链接到程序集“App_LocalResources.root._j2bta_i”,或者确保所需的所有附属程序集都是可加载和完全签名的。但这个文件确实不存在:freemediadetails