1

我有几个资源文件,例如

default.aspx.resx、default.aspx.nl.resx、default.aspx.en.resx

现在,当我在荷兰域上时,会加载 default.aspx.nl.resx。但现在我想从 default.aspx.en.resx 访问值并获取属于名称“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

但是有更好/更快的方法吗?最好不必更改当前线程的文化,所以我可以定义我想访问哪个资源文件以及使用哪种语言?

4

3 回答 3

4

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

GetLocalResourceObject("title","YourCulture");

链接:http: //msdn.microsoft.com/fr-fr/library/ms149953.aspx

于 2013-03-26T16:49:38.857 回答
3

编辑:(对不起,我不知道您想要另一种与此不同的方法,但这是我设法做到的唯一方法:)

我设法做到了这一点:

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 文件的命名方式,而 KeyThatIWantToGet 会自行解释。

于 2014-04-30T03:05:27.860 回答
1

当不使用 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;
    }
}
于 2015-05-08T08:06:01.990 回答