1

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?

Adam
  • 6,041
  • 36
  • 120
  • 208

3 Answers3

4

You can add in parameter your targe culture

GetLocalResourceObject("title","YourCulture");

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

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • Thank you...could you provide a sample on how to use it? I don't see any examples on the page you supplied. Thanks again! – Adam Mar 26 '13 at 17:36
  • And I tried your suggestion: GetLocalResourceObject("title", "en"), but that doesn't work. I'm using ASP.NET 4.5 btw – Adam Mar 26 '13 at 17:52
  • try with GetLocalResourceObject("title", "en-EN") – Aghilas Yakoub Mar 27 '13 at 09:47
  • The problem lies in the fact that I'm only passing 2 parameters...I get the error: Overload resolution failed because no accessible 'GetLocalResourceObject' accepts this number of arguments. But I don't know how else to call it and then access the returned value. – Adam Mar 27 '13 at 10:06
  • Ah, apparently I didn't. I now changed it to: lblStatus.Text = HttpContext.GetLocalResourceObject("~/freemediadetails.aspx.nl.resx", "backtosearchresults").ToString But then I get this error: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "freemediadetails.aspx.nl.resx.resources" was correctly embedded or linked into assembly "App_LocalResources.root._j2bta_i" at compile time, or that all the satellite assemblies required are loadable and fully signed. But this file indeed doesn't exist: freemediadetails.aspx.nl.resx.resources – Adam Mar 27 '13 at 10:36
  • Ah, this works: HttpContext.GetLocalResourceObject("~/freemediadetails.aspx", "backtosearchresults", New CultureInfo("nl")).ToString – Adam Mar 27 '13 at 10:48
3

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.

Lucian Sturião
  • 155
  • 1
  • 4
1

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;
    }
}
James Crosswell
  • 648
  • 4
  • 13