Search code examples
c#asp.net-mvcmultilingual

MVC 5 localization working on localhost but not server


i try to create a multi language mvc website based on resource files. I found on youtube this video, which explains the concept and gives some example code: https://youtu.be/oGeAYd3idBc

The problem now is. It works localhost but if i upload it on my server, it doesn't works.

On this point i started a lot of research. I could not found much, everything i found and tried, didn't worked.

Many solution proposals suggested to try set globalization culture/uiCulture in web.config to auto. This also didn't worked.

On my index page i output the current culture (CultureInfo.CurrentCulture.Name). On localhost it changed if the user set his language. But on my server its always english.

So i hope you understand my problem.

Now my code:

web.config

<system.web>
<globalization culture="auto" uiCulture="auto"></globalization>  
</system.web>

index.cshtml

@using System.Globalization

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="pushContent"></div>
<div class="container">
    <div class="row">
        <ul>
            <li>@Html.ActionLink("English", "Change", "Language", new { LanguageAbbrevation = "en" }, null)</li>
            <li>@Html.ActionLink("German", "Change", "Language", new { LanguageAbbrevation = "de-DE" }, null)</li>
        </ul>
    </div>
    <div class="row">
        <p>
            CurrentCulture is now @CultureInfo.CurrentCulture.Name
        </p>
    </div>
</div>
<div class="pushFooter"></div>

LanguageController

public ActionResult Change(string LanguageAbbrevation)
        {
            if (LanguageAbbrevation != null)
            {
                CultureInfo.CurrentCulture.ClearCachedData();
                CultureInfo culture;
                culture = CultureInfo.CreateSpecificCulture(LanguageAbbrevation);

                CultureInfo.DefaultThreadCurrentCulture = culture;
                CultureInfo.DefaultThreadCurrentUICulture = culture;
            }

            HttpCookie cookie = new HttpCookie("Language");
            cookie.Value = LanguageAbbrevation;
            cookie.Expires = DateTime.MaxValue;
            Response.Cookies.Add(cookie);

            return View("Index");
        }

Global.asax.cs

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];

            if(cookie != null && cookie.Value != null)
            {
                CultureInfo.CurrentCulture.ClearCachedData();
                CultureInfo culture;
                culture = CultureInfo.CreateSpecificCulture(cookie.Value);

                CultureInfo.DefaultThreadCurrentCulture = culture;
                CultureInfo.DefaultThreadCurrentUICulture = culture;
            }
            else
            {
                CultureInfo.CurrentCulture.ClearCachedData();
                CultureInfo culture;
                culture = CultureInfo.CreateSpecificCulture("en");

                CultureInfo.DefaultThreadCurrentCulture = culture;
                CultureInfo.DefaultThreadCurrentUICulture = culture;
            }
        }

Some screenshots which maybe helps.

project structur enter image description here


Solution

  • I found the "solution"... I know its silly, but i don't know this.

    Seemingly if you use resources for localization it will create a extra YOURPROJECT.resources.dll which be located at /bin/de-DE/ (in my case).

    You also have to upload this folder/.dll

    enter image description here