Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change language based on resources files in ASP.NET MVC 4

I have 2 resource file : Resources.resx(has some strings in Romanian) and Resources.en-US.resx (has the same strings in English).

I only want to change(in a dropdownlist, a listbox,...) witch resource file to use. It could be in _Layout.cshtml. I don't need to detect the user's culture.

Q: How can I select a resource file from a page ?

Edit : Can it be done without changing the default MapRoute ?

like image 622
Misi Avatar asked Apr 10 '12 14:04

Misi


2 Answers

One way you can do it is to have the drop down just redirect the page to a language specific URL (this is quite nice as you can send around language specific links) then in a base class on your controller, set the Thread's locale.

This blog post covers what I am talking about in better detail: Localization in ASP.NET MVC – 3 Days Investigation, 1 Day Job

like image 67
kmp Avatar answered Sep 19 '22 15:09

kmp


Check this Blog. Without changing default MapRoute.

The _Layout.cshtml page:

@using Resources;
<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div>
            <form method="post">
                @TestResource.SelectLanguage
                <select name="lang">
                    <option></option>
                    <option value="en-GB" @(Culture == "en-GB" ? "selected=\"selected\"" : "")>English</option>
                    <option value="fr-FR" @(Culture == "fr-FR" ? "selected=\"selected\"" : "")>French</option>
                    <option value="de-DE" @(Culture == "de-DE" ? "selected=\"selected\"" : "")>German</option>
                </select>
                <input type="submit" value="@TestResource.Submit" />
            </form>
        </div>
        @RenderBody()
    </body>
</html>

The culture is set within the _PageStart.cshtml file:

@{
    Layout = "~/_Layout.cshtml";
    if(!Request["lang"].IsEmpty()){
        Culture = UICulture = Request["lang"];
    }
}

The final page is the Default page itself:

@using Resources;
<h1>@TestResource.Welcome</h1>
<p><img src="images/@TestResource.FlagImage" /></p>

http://www.mikesdotnetting.com/Article/183/Globalization-And-Localization-With-Razor-Web-Pages

like image 45
Min Min Avatar answered Sep 21 '22 15:09

Min Min