Search code examples
c#localizationresourcesgodotresourcemanager

Resource manager gets default language and only one culture specific translation, every next culture returns english


I am using resx files in a c# godot project. After some issues I can now finally access translations with resourcemanager.

The problem I ran into is that I can only get one culture specific translations and when I call for the same string in another language/culture it always goes back to default english.

I have an string with ID: test_string

english value: test1

spanish value: test2

french value : test3

var translationAssembly = Assembly.GetExecutingAssembly();
var translationAssemblyResource = "GodotFrontend.MultilingualResources.strings";
var resourceManager = new ResourceManager(translationAssemblyResource, translationAssembly);

System.Globalization.CultureInfo ci1 = new System.Globalization.CultureInfo("en"); 
string temp = resourceManager.GetString("test_string", ci1); // returns test1

System.Globalization.CultureInfo ci2 = new System.Globalization.CultureInfo("es"); 
string temp2 = resourceManager.GetString("test_string", ci2); // returns test2   

System.Globalization.CultureInfo ci3 = new System.Globalization.CultureInfo("fr"); 
string temp3 = resourceManager.GetString("test_string", ci3); // returns test1 (WRONG)

So the third language does not return the expected "test 3" string but rather the default english one. But what I find even stranger is if I switch the order of calling the strings (switching es and fr) I get the french string but then the spanish string reverts to english

System.Globalization.CultureInfo ci1 = new System.Globalization.CultureInfo("en");
string temp = resourceManager.GetString("test_string", ci1); // returns test1

System.Globalization.CultureInfo ci2 = new System.Globalization.CultureInfo("fr");
string temp2 = resourceManager.GetString("test_string", ci2); // returns test3

System.Globalization.CultureInfo ci3 = new System.Globalization.CultureInfo("es");
string temp3 = resourceManager.GetString("test_string", ci3); // returns test1 (WRONG)

Basically it works for first "translation" which is not english, next one stops working.

Any idea why this is happening?

UPDATE:

Found a issue where correct language assembly cannot be loaded after first one because the .dll-s for each language have same name.

If I switch the app to "console app" it works, but when I switch it back to "class library app" it cannot load the same-named assembly so the assembly stays the same, even when I call it with different specified culture.

How to bypass this??


Solution

  • Workaround:

    public static void LoadAllTranslationAssemblies()
    {
        string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        foreach (string dll in Directory.GetFiles(path, "*.resources.dll", SearchOption.AllDirectories))
        {
            try
            {
                Assembly loadedAssembly = Assembly.LoadFile(dll);
            }
            catch (FileLoadException loadEx)
            {
                Console.WriteLine(loadEx);
            }
            catch (BadImageFormatException imgEx)
            {
                Console.WriteLine(imgEx);
            } 
        } 
    }