Question

I am trying the multi language features in an application. I have created the resource files GlobalTexts.en-EN.resx GlobalTexts.fr-FR.resx and a class that sets the culture and returns the texts like (I will not go in all the details, just show the structure):

public class Multilanguage
{
    ...
    _res_man_global = new ResourceManager("GlobalResources.Resources.GlobalTexts", System.Reflection.Assembly.GetExecutingAssembly());
    ...
    public virtual string GetText(string _key)
    {
        return = _res_man_global.GetString(_key, _culture);
    }
}
...
Multilanguage _translations = new Multilanguage();
...
someText = _translations.GetText(_someKey);
...

This works just fine. Now, I would like to use this application in another solution that basically extends it (more windows etc.) which also has resource files ExtendedTexts.en-En.resx ExtendedTexts.fr-FR.resx and a new class like:

public class ExtendedMultilanguage : Multilanguage
{
    ...
    _res_man_local = new ResourceManager("ExtendedResources.Resources.ExtendedTexts", System.Reflection.Assembly.GetExecutingAssembly());
    ...
    public override string GetText(string _key)
    {
        string _result;
        try 
        {
            _result = _res_man_local.GetString(_key, _culture);
        }
        catch (Exception ex)
        {
            _result = base.GetText(_key);
        }
}
...
ExtendedMultilanguage _translations = new Multilanguage();
...
someText = _translations.GetText(_someKey);
...

the idea being that if the key is not found in ExtendedTexts the method will call the base class which is looking into GlobalTexts. I did this in order to use the call GetText(wantedKey) everywhere in the code without having to care about the location of the resource (I do not want to include the translations from the extensions in the GlobalTexts files); it is juts the used class that is different from project to project.

The problem I am facing is that the try/catch is very slow when exceptions raise- I wait seconds for one window to populate. I tested with direct call and works much faster, but then I need to care all the time where the resource is located...

The question is: is there an alternative way of doing this (having resources spread in various files and have only one method that gives the desired resource without throwing an error)?

No correct solution

OTHER TIPS

In the end I took a workaround solution and loaded all the content of the resource files in dictionaries. This way I can use ContainsKey and see if the key exists or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top