1

I have localized resource strings like

  • localizedEnStrings.resx
  • localizedDeStrings.resx

each has inside strings like

  • Home
  • Page
  • ...

How can I create extension method which will return string Page if I know language from parameter, I dont want to hassle with threads.

public static string LocalizeString(this string text, string lang)
{
    if(lang == "EN"){
        return localizedEnStrings.Page; // how to return string?
    }
}
user1765862
  • 13,635
  • 28
  • 115
  • 220

2 Answers2

2

It looks to me like you are not employing the full potential of .NET localization capabilities.

How resources work in .NET

First, I'd recommend you change your resources from this:

  • localizedEnStrings.resx
  • localizedDeStrings.resx

to this:

  • localizedStrings.en.resx
  • localizedStrings.de.resx

The above naming conforms to the pattern {resource_name}.{locale_name}. The resource_name part is how you want to name the resource. The locale_name part is the standardized name of a locale or region culture (valid would be "en", "en-GB", "de", "de-DE" and so on).

This will instruct the framework to treat the localizedStrings files as one and the same resource, that could vary by culture (see System.Globalization.CutlureInfo.CurrentCulture or CurrentUICulture). I will expose more about the cultures below in this post.

The easiest approach to get the resource would be to use the generated class for the respective resource file. In your case it would be called localizedStrings. Then access the respective resource key as a property. For example localizedStrings.HelloString will be valid assuming you defined a "HelloString" entry. You may as well use other standard ways to get a resource, that are exposed by the .NET framework.

Depending on the value of the current culture, you will get the English or German text.

It is a good practice to have a resource file without locale in this name - localizedString.resx that would have fallback values in case any of the concrete files do not have an entry defined for the given key. This works because the .NET resource management operates in a certain way to lookup resources. For example, if you have this file texts.en-US.resx, your culture is set to en-US and you request the resource with the name "HelloString", the framework will do this:

  1. Lookup a resource file for the current culture - that would be texts.en-US.resx - because this has the most detailed locale name (en-US). If the file is not found, or the resource was not defined in the file, will proceed with next step.
  2. Lookup the string from the texts.en.resx. Here, en is considered some form of a super-culture to all English based resources, and acts as fallback to all en-XX cultures. Again we will proceed if the file or resource entry was not found.
  3. Lookup the string from the texts.resx file if again not found so far. This is the ultimate fallback file in case no resource file exists for the specified culture, or there is no entry to match the requested key.

The above happens transparently for you out of the box, you only need to adjust the value of the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture.

To sum this up nicely, your extension method should look like this:

public static string LocalizeString(this string text, string lang)
{
    return localizedStrings.Page;
}

And it will work depending on the value of the current culture.

What are Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture ?

I have answered that in a related question: What is the difference between CurrentCulture and CurrentUICulture properties of CultureInfo in .NET?. Checkout the post and the other answers to get better understanding of both properties.

Community
  • 1
  • 1
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
1

solution 1:

If you know the language you can override InitializeCulture() method and set

string culture = "EN";
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

solution 2:

If you want to load every resource:

// Page.GetLocalResourceObject()
//
string text = GetLocalResourceObject("text_id").ToString();

Here MSDN sample.

McNets
  • 10,352
  • 3
  • 32
  • 61