Question

I need a way to use a third "layer" of a culture specific resource file. This can possibly be done in a way it should be, and possibly hacked somehow. And as long the hack isn't too big, it's okay! Another solution is also possible (if I'm approaching the problem wrong, please tell the better solution).

Normally you have a Default resource file, which works as a fallback. On top of that you can have a culture specific resource for a language. Now what I want is a 'theme' specific resource.

The thing is that we have several sites all with different themes (horses, cars, rc-units, etc.), and when we use a string from the localized resource file if the resource isn't translated in a specific language yet, it'll fallback to english which is the default language. Cool!

A problem example: We have the phrase "Add your horse" and "Add your car" and it might seem the same (when it comes to grammar) in english, yet that's not necessarily the case in Polish or Romanian!

So I'm thinking that we should, if possible, having an english file with "Add your {0}", then a Polish file with the equivalent sentence in Polish, on top of that a "Car" specific Polish translation where the grammar is suited for the word "Car" and a "Horse" specific polish translation where the grammar is suited for the word "Horse".

The problem might not the so bad with two or three themes in two or three languages. It would be easy to think that we could just make a switch in the code handling those few cases. Thing is, we have 15 themes, in 6 different languages and are expanding.

Hoping for some neat hacks or knowledge of some rarely used exotic features!

Thanks in advance

Was it helpful?

Solution

I'm not pointing out a solution for your problem, but I have, however, a concern about your string-formatting:

One problem with your example is the use of String.Format with translations.

The "Add your {0}" part will, in some languages, requre different wordings depending on, what will be inserted into the {0} part.

An example:

"Add you horse" will, translated into Danish, become "Tilføj din hest" whereas "Add your donkey" will translate into "Tilføj dit æsel". So as you can see there is a difference: din/dit which will not be translateable correctly from "Add your {0}". The same goes for other languags such as german.


Edit: A pseudo-answer could be something like: Add more strings to each resx-file (with identifiers like AddYourItemHorse, AddYourItemRacecar and AddYourItemDonkey) for each of the themes. Or create resource file like PageName.Racecar.da-DK.resx with the resources for the racecar-theme in it and so on. Then, based on the area of the code, theme and the current culture, you do the following:

Dictionary<string, string> resourceDictionary = new Dictionary<string, string>();
private void fillDictionary(string path)
{
    using (ResXResourceReader rxrr = new ResXResourceReader(path))
    {
        foreach (DictionaryEntry entry in rxrr)
        {
            //Fill the fillDictionary
        }
    }
}

And then access the string in the dictionary by concatenating what you want with a string representing the theme:

string theme = "Racecar";
Label testLabel = new Label();
testLabel.Text = resourceDictionary[AddYourItem + theme];

Alternatively you can have a resource-file pr. area, language and theme like: MainForm.Racecar.da-DK.resx and then build the path passed to the fillDictionary like this:

string languageCode = Thread.CurrentThread.CurrentCulture.Name;
string path = "MainForm." + theme + "." + llanguageCode + ".resx";

And pass the path to the fillDictionary-method.

Note that the above code is not tested. I have just written it here from the top of my mind. But it should point you in the direction of a possible (not necessarily optimal) solution.

You can read more about the ResXResourceReader here: Working with .resx Files Programmatically

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