Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localization in ASP.NET MVC

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") ?

like image 541
Soraz Avatar asked Mar 01 '23 19:03

Soraz


1 Answers

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&s=books&qid=1241010151&sr=8-1

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

like image 121
Martin Peck Avatar answered Mar 13 '23 07:03

Martin Peck