views:

401

answers:

2

Visual Studio 2008

I want to bring some localisation into my ASP.NET MVC site.

Someone suggested creating a resource file "Strings.resx" as a publically strongly typed resource, which works nicely and allows me to write

<title><%= Strings.MyView_Title %></title>

I then proceeded to add a file "Strings.da.resx". This file is created right next to the first one, and defaults to "Access Modifier : No Compilation", whereas the first (the one without language modifier) defaulted to "Interal".

I can see in the bin directory that a directory has been created ("da") with a resource.dll, however, I cannot see any of the translated texts on my site.

I have checked with the browser that the only preferred langauge is Danish (da-DK), but I only see the english texts.

Questions: 1) Do I need to enable something in web.config ? 2) Am i creating the right files, with the correct types (i.e. should #2 be "No compilation") ?

+5  A: 

In your views, do you have a page directive? If so, do you have UICulture="Auto" and Culture="Auto"?

For example...

<%@ Page Language="C#" Inherits="..." 
    culture="auto" uiculture="auto" %>

This will ensure that the Accept-Language header, passed by the browser in the request, gets used to set the Thread cultures. It's the UICulture that influences which resource file to pick.

For more on ASP.NET i18n this book is very good...

http://www.amazon.co.uk/NET-Internationalization-Developers-Guide-Building/dp/0321341384/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1241010151&amp;sr=8-1

It doesn't cover MVC, but covers ASP.NET and, as such, many things continue to be relevant.

Martin Peck
Thanks, that did the trick.
Soraz
Actually, turned out I could set <globalization uiCulture="auto" culture="auto" />in <system.web> section of the web.config file
Soraz
Yes, you can do that too. I tend to do it at the page level so that there isn't any "special magic" coming from config that new developers to a project don't expect.
Martin Peck
And great code reuse sample))
+1  A: 

First you have to create action filter that will switch culture of request thread. OR Set your globalization element to UICulture="Auto" and Culture="Auto"

Check this screencast, it is in Russian but code samples are understandable.