ResourceManager and ResXFileCodeGenerator provide great functionality for localization: it is enough to create the same .resx file with a language prefix (e.g. MyStrings.ru.resx) and as a result we can smoothly work with different languages by setting corresponding MyStrings.Culture property in generated Designer file and invoking necessary string-related property:
MyStrings.Culture = new CultureInfo("ru");
Console.Write(MyStrings.MyTranslatedString); // Russian output
MyStrings.Culture = CultureInfo.InvariantCulture;
Console.Write(MyStrings.MyTranslatedString); // English output
I like this approach very well. But unfortunately it will fail in multithreading mode, because mentioned .Culture property is static.
I want to keep the same functionality (easy resource files edit; automatically generated properties with Inlellisense support, etc.), but with ability to work in multithreading mode.
Of course I can use ResourceManager directly, like that:
ResourceManager.GetString("Commands description", resourceCulture);
But in this case if I change a name (key) in a .resx file, I will have to change it manually in .cs files, which is not convenient enough.
In your example, the property
MyStrings.MyTranslatedStringis defined in generated code. To achieve what you are asking for, you'd need to generate your own implementation of that file.You could achieve this using a source generator, although there are quite a few steps involved in doing that.
https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview
Alternatively you could hand-code a class to do this. Perhaps you could add a unit test to ensure that all keys in your
.resxfile are also present in your hand-coded class, to catch cases where it gets out of sync. That'd be less work than a source generator, but require more ongoing maintenance. If your resources don't change that frequently, it might be the better option.