1

I'm trying to add localization to my .NET MVC project. As far as I've seen (here, here, and here), I should simply be able to create a total of three files (if I have two languages).

Resources.resx
Resources.en-us.resx
Resources.da-dk.resx

When I open the .resx files, I can add entries to them. Once I've done that (and set "Access Modifier" to either Internal or Public), it generates a Resources.*.Designer.cs file (as it should). However, for en-us and da-dk they are empty. No errors or anything.

As far as I could read (here, here, and here), I cannot have a dot between the file name and the .resx extension. And to my surprise, it's true. If I rename any of those en-us/da-dk files to Whatever.resx the Whatever.Designer.cs file will be created.

I've read a lot of answers, tried my way with T4 templates, and a bunch of other things, but I simply cannot get it to create a working Designer.cs file.

Am I doing it wrong? I feel like I've tried everything now. I just want to be able to do Resources.TestText and have my application do the translation depending on the culture.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • 1
    I use a VS Extension called Resx Manager, which handles everything for you, you just install it and select the language you need and it creates all the files. It also has a UI to add the strings. – Nekeniehl Nov 13 '18 at 09:26
  • @Nekeniehl Doesn't really help me when the code doesn't work as expected. I created a new project and it works perfectly. – MortenMoulder Nov 13 '18 at 09:31
  • It was just a suggestion for next time, I always have struggle with localizations – Nekeniehl Nov 13 '18 at 10:01

1 Answers1

1

It is by design.

The Resources.EN-US.resx file types, doesn't have a designer because the actual designer is in it's "parent" file, Resources.EN-US.resx. The en-us file only holds the key/value XML.

If you are calling your Resource, you probably use it like:

var someVar = Resources.SomeLocalizedString;

You don't have to differentiate between the EN-US types.

If you look at the designer's code, you can see whats happening (hold on, I'll fetch an example)

So, you don't need those designers, and it should work out of the box if you set the culture info of the UI thread.

Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • That makes sense, if that was the case. But my locale is set to `en-US` (and I did try to uppercase the US and DK part), but it's not being affected in my code. I did add the tag inside my Web.config file – MortenMoulder Nov 13 '18 at 08:37
  • Ok, I'll spin up a visual studio: what kind of project are you working on? Console? Asp? Wpf? – Stefan Nov 13 '18 at 08:40
  • It's an ASP.NET Web Project. I did try setting the culture using a Thread in the Startup.cs, but it still won't use my translation (it just defaults to the Resource.resx value) – MortenMoulder Nov 13 '18 at 08:44
  • Ah, yes, I think I know whats happening: if you do a call to the asp.net web project, a new thread is spinned-up; this new thread contains the default UI culture info. – Stefan Nov 13 '18 at 08:46
  • If I output the `Thread.CurrentThread.CurrentUICulture` on the page, it shows `en-US`. So something is definitely not working haha – MortenMoulder Nov 13 '18 at 08:47
  • Hmm, that makes it a bit more annoying... I need to go, for now I'll leave my answer for reference. – Stefan Nov 13 '18 at 08:49
  • Just to be sure: can you put a breakpoint at the location where you retrieve the string and see if the thread's culture info matches? – Stefan Nov 13 '18 at 08:55
  • Yeah it does match – MortenMoulder Nov 13 '18 at 09:05
  • I created a new project and made the exact same changes I made in my other project. Now it works fine and the translations are replaced. Debugging this is most likely not going to happen... – MortenMoulder Nov 13 '18 at 09:32
  • Works now.. goddamn casing was wrong. What a stupid system that allows me to have values in my different locales that aren't in my main .resx file. – MortenMoulder Nov 13 '18 at 09:43
  • I created an unit test that runs every time we deploy. It's now impossible to deploy if a locale file (en-US etc.) includes a value that isn't in the main .resx file :-D – MortenMoulder Nov 13 '18 at 12:01
  • It's really simple. Just grab the files using Directory.GetFiles(), loop through each file, and add the values to a List. Then simply create your test that loops through each translation and see if the key from the translation (en-US) is in the main/neutral file list. If it's not, Assert.IsTrue will be false and therefore crash the deployment (we use Bamboo). – MortenMoulder Nov 15 '18 at 12:02