Hugo Alves
Hugo Alves

Reputation: 1555

best way to multi-language asp.net mvc 3

I'm trying to create a asp.net mvc3 project for a academic project, and one of the requirements is it has to be able to change between different languages. Currently what i have is the following:

I have a external project that works as a repository for languages and for each view i have an interface for each view that defines all the "placeholders" do define all the changeable text. At the beginning of any action i obtain the language that is in the uri (something like /{lng}/{command}/{action}) and pass it to the view using the ViewBag, once inside the view i user the repository to obtain the current implementation of the interface for that view in the chosen language.

I can't find any good topic on this mater. I'm just curios if there is a better way to do this and more efficient. And how is it normally done in a professional level. I'm not very experienced with asp.net just started learning it about a month ago.

Also if it's important i am using the razor engine for the views, and we can't use any JavaScript in this phase of the project.

Upvotes: 9

Views: 17641

Answers (2)

jgauffin
jgauffin

Reputation: 101166

I'm working with a project called Griffin.MvcContrib which has some localization features.

First of all, I use the query string and a cookie to switch language. (Just create a link with a flag in your layout <a href="@Url.Action("Index")?lang=en-us">English</a>)

and tag your controller with my attribute:

[LocalizedAttribute]
public class YourController : Controller
{

}

The next thing is to get localization of views, models and validation messages. The localization of models and validations are described here. As for views, you only need to use @T() to get translated texts:

@Model.Title

<div>@T("This text will get translated")</div>

(you need to change pageBaseType in Views\Web.config to Griffin.MvcContrib.GriffinWebViewPage)

I'm almost done with an adminstration area that any non-technical user can use to manage all translations. Check the Griffin.MvcContrib.Admin project here: https://github.com/jgauffin/griffin.mvccontrib/tree/localization/source/Griffin.MvcContrib.Admin

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039150

You may go through the following guide.

Upvotes: 10

Related Questions