Reputation: 2103
I have a scenario where I need to share localization between multiple project types (web front end, WCF, windows service, etc) so my idea was to create a localization assembly where I could embed all my resx files and then anytime I add/modify these I simply need to redeploy a single dll to update all my apps.
So to test I created a Class Library project and named it TestLocalization. I added a class to it called LocalizationManager that is just a stub for now, (I plan to put some utility methods in it later). Then I added two resx files to it with a single test entry in each. I named one file Resource.resx and the other Resource.fr.resx. I verified that the resx files' Build Action was set to 'Embedded Resource' then compiled it and tried to call it with the following code:
Assembly assembly = typeof(TestLocalization.LocalizationManager).Assembly;
ResourceManager rm = new ResourceManager("Resource", assembly);
string str = rm.GetString("AKey");
It gives me the following error:
"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Resource.resources" was correctly embedded or linked into assembly "TestLocalization" at compile time, or that all the satellite assemblies required are loadable and fully signed."
What am I doing wrong here? thanks!
Upvotes: 2
Views: 2986
Reputation: 2103
I'm not sure what I had wrong initially but I could have sworn I tested it with all the possible permutations of assembly + namespace + resourcename and it didn't work but maybe I forgot to save or something because it really was as simple as:
ResourceManager rm = new ResourceManager("TestLocalization.Resource", assembly);
That's where my project name is TestLocalization and my resx files are all in the root. If you have them in a subdirectory under the root, you need to include that as well.
And then if you want to switch between locales just do this:
CultureInfo french = new CultureInfo("fr-FR");
string translatedVal = rm.GetString("keyname", french);
Upvotes: 1