Reputation: 271
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.
Upvotes: 27
Views: 16040
Reputation: 40726
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:
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:
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.
Upvotes: 5
Reputation: 969
The next step that you need is to localize your Javascript library. Take a look here: MVC-JavaScript-localization-of-external-js-files
Upvotes: 1
Reputation:
Here is a great detailed post about MVC 3 Globalization/Internationalization http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx
Upvotes: 5
Reputation: 8595
You localize it in the same way as any other application like this:
PublicResXFileCodeGenerator
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
.
Upvotes: 43