27

I am trying to achieve globalization/localization in my MVC 3 application. I don't want different Views for each language. Please suggest how I can proceed. Any supported links/URLs will be of great help.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Vivek
  • 271
  • 1
  • 3
  • 3

4 Answers4

43

You localize it in the same way as any other application like this:

  1. Create a folder, call it e.g. Resources
  2. Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
  3. Under the properties of file, change Custom Tool to be PublicResXFileCodeGenerator
  4. Populate the Resource file with Translation key and value pairs (this will be the default translation)
  5. Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
  6. (This is for Razor) crack open the web.config in the Views folder and add this to /configuration/system.web.webPages.razor/pages/namespaces: <add namespace="Resources" /> (assuming resources is the name of the folder you created the resources in and you haven't changed the default namespace on the resouce files themselves). This step means you don't have to fully qualify the resource classes in your views each time you want to reference a translation.
  7. Use the translations in place of text in your views like with the following code:

    @Strings.MyString
    

Strings will be automatically translated in the view depending on CultureInfo.CurrentCulture but this is not set automatically for you

You will need to change the CurrentCulture (potentially in Application_BeginRequest). How you do this is up to you, it could be a route value which sets it or you can read the user's browser language

You can find a list of the user's prefered languages (in order) in HttpContext.Current.Request.UserLanguages.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Martin Booth
  • 8,485
  • 31
  • 31
  • 2
    Oon the step 7 I do the same in web. config as highligted but getting the error "The type or namespace name *****'Resource' could not be found (are you missing a using directive or an assembly reference?)"***** " Please elaborate more or provide any sample application. – sandeep May 21 '11 at 10:20
  • 1
    Here is the minimum you need to get this working http://www.mediafire.com/file/5843d7tyqalv451/Globalization.zip – Martin Booth May 27 '11 at 02:33
  • 5
    You should add – Simon Epskamp Dec 01 '11 at 17:25
  • How do you access the Resource strings in javascript? – aggaton Feb 24 '13 at 05:25
  • If you want to access them via javascript, you'll need to do some ajax call to a service that returns the strings. – Captain Kenpachi Jun 13 '13 at 08:23
  • @aggaton, you can create literals in Javascript that use Razor to pull the values from the back end – ekkis Nov 03 '13 at 21:52
5

To add some details to Martin Booth's great answer (in case his MediaFire link might disappear), here is how I idid it:

I've used two files, since I only need English and German ("de") for now:

enter image description here

For the properties of each file, I had to manually enter the Custom Tool as well as the Custom Tool Namespace values, for each file:

enter image description here

enter image description here

And finally, I entered the following inside the root Web.Config file, below the <system.web> section:

<globalization uiCulture="auto" culture="auto" />

Of course I've also added the namespace directive in the Web.Config file below the Views folder (i.e. not the root one), as Martin describes:

<add namespace="ViewResources" />

And then I could finally access the resources strongly-typed in my (partial) Razor view:

<h2>@ViewResources.Test1</h2>

BTW: this worked with MVC 4, too, not only MVC 3.

Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
5

Here is a great detailed post about MVC 3 Globalization/Internationalization http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx

  • This post is excellent for covering the difference between language, culture, and region - but it makes the same mistake as the above post in the way it calls the incomplete namespace in Web.config. – Chris Moschini Apr 04 '12 at 23:51
  • It mentions you need to add namespace to your views web.config –  Apr 09 '12 at 03:11
1

The next step that you need is to localize your Javascript library. Take a look here: MVC-JavaScript-localization-of-external-js-files