Question

I have several resource files, e.g.

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

Now when I'm on the Dutch domain the default.aspx.nl.resx is loaded. But now I want to access the value from default.aspx.en.resx and get the English value belonging to name "title".

I can now achieve this by changing the current culture server-side, access the value and then change it back, like so:

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

But is there a better/faster way? Preferably without have to change the culture for the current thread, so I can just define which resource file I want to access and in which language?

Was it helpful?

Solution

You can add in parameter your targe culture

GetLocalResourceObject("title","YourCulture");

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

OTHER TIPS

Edit: (Sorry I didn't know that you wanted another method different from this, but this was the only way that I managed to do:)

I managed to do this by doing:

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");

Where MyAppResource is how your .resx file is named and KeyThatIWantToGet explains itself.

When not using the HttpContext (general .NET applications) I use the following helper:

/// <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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top