3

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

egolive
  • 399
  • 5
  • 20
  • The code you have is going to set the default culture for the entire application, not the user's culture, so you probably don't want this code to work anyway. – stephen.vakil Oct 04 '17 at 16:19
  • 2
    See [ASP.NET MVC 5 culture in route and url](https://stackoverflow.com/a/32839796). There is no reason at all to use cookies for localization, and using them is also bad for SEO. – NightOwl888 Oct 04 '17 at 18:34
  • Yup, go with @NightOwl888's approach :-) I'll remove my "answer" – DiskJunky Oct 04 '17 at 21:08
  • If i remove the whole cookie stuff it's still not working. I uploaded the project (i changed nothing) to a azure test web app and it just works. I don't get it... – egolive Oct 05 '17 at 13:34

1 Answers1

2

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

egolive
  • 399
  • 5
  • 20