Question

I've never tried to use resx files in a MVC project before so I got interested in this now because I got a requirement of 3 languages for my website, a global English, and two local Danish and Spanish. so I was thinking this is easy to do I'll just do what I used to do I'll make my own xml files and just ask the browser to switch between them at the users request, and always load the global language first.

then I read about resx and how ASP.Net MVC could choose the language of Danish or Spanish depending on the users language as default and always go back to English if the user didn't use any of these languages. this got me started on reading on how to Globalize internationalize and localize my website.

however ,and now comes my problems, I feel that I'm missing something. I know how to create the resx files and locate them in either folders of my own choosing or the app_GlobalResources or one of the others, I just can't seem to get it to work everywhere I read it feels like something important like how to link the resx files together and use them in the razor view is missing. I find tons of places that just say write:

@Resources.myString

and somehow it would know which of the resources to take, the problem however is that the intellisense doesn't recognize my resx files as Resources.mystring nor does it accept it anyway as a compilable build. in short I can't find or make the link from the resx files to the Razor2 view pages, or any other places in my code for that matter, so please someone explain or link a tutorial for dummies cause I Clearly does not understand why it have to be so difficult to use something that should be simple.

Thanks for reading all my wall of text, I'm not good at formulate myself in short sentences in English so I'm sorry if I'm making anyone confusing about my actual problem.

Was it helpful?

Solution

As far as I know, there are multiple ways your can work with resx files in MVC. We decided to give them all a namespace and copy them into a Resources directory and then use the same path as the MVC Views. This is how this looks like and make sure you set the resx files property according to my picture:

enter image description here

Then you can access the resources like this:

<h1>@ViewResources.Creation.Index.Headline</h1>

You can change the default language like this: Just make sure you store hte languageCode somewhere or get it from the url. Personally I suggest adding the language code to the url. example: www.supertext.ch/en. If you go to the bottom of the page, there are the language links.

protected void Application_BeginRequest(object sender, EventArgs e)
{                    
    //Dateformats, etc. (not the language)
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(languageCode);
    //Language
    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(languageCodeUI);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top