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.
-
See [ASP.NET MVC 5 culture in route and url](https://stackoverflow.com/q/32764989) – NightOwl888 Mar 12 '18 at 09:05
4 Answers
You localize it in the same way as any other application like this:
- Create a folder, call it e.g. Resources
- Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
- Under the properties of file, change Custom Tool to be
PublicResXFileCodeGenerator
- Populate the Resource file with Translation key and value pairs (this will be the default translation)
- Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
- (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.
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
.

- 398,270
- 210
- 566
- 880

- 8,485
- 31
- 31
-
2Oon 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
-
1Here 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
-
-
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
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.
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
-
The next step that you need is to localize your Javascript library. Take a look here: MVC-JavaScript-localization-of-external-js-files

- 939
- 12
- 21