0
votes

I'm trying to make a page in 3 languages.

English, Danish, Spanish.

I'm using resx files for the languages and I've made it work so I can now see that if my system is set to English I will get the English language or Danish will give the Danish Language. however I am trying to implement a override feature so I can manually change language on the page through a button I imagine that it will be 3 flags button to change languages. However i don't seem to be able to override the language. and I've gotten more problems trying to than just sticking with default settings.

here is how I've set up some of my things:

I've set up my routes like this:

 routes.MapRoute(
            name: "DefaultLocalized",
            url: "{language}-{culture}/{controller}/{action}/{id}",
            defaults: new 
            { 
                controller = "Home",
                action = "Index",
                language = "da",
                culture = "DK",
                id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "DefaultInternationalized",
            url: "{language}-{culture}/{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                language = "en",
                culture = "UK",
                id = UrlParameter.Optional
            }
        );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

when I try this I don't seem to be able to use the last default route. And when I try to override my language which is set to Danish by default so I can see the pages in English the URL sure enough reads en-UK/Home but the language is still my browsers default language.

I tried to make a filter like this

 public class InternationalizationAttribute : ActionFilterAttribute 
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        string language = (string)filterContext.RouteData.Values["language"]; //?? "da";
        string culture = (string)filterContext.RouteData.Values["culture"];// ?? "DK";

        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));

    }

my hope was that this would activate and decide the language but it seems it never does.

My resx files in this path.

View/Languages/

with these names

Resources.resx
Resources.da-DK.resx
Resources.es-ES.resx

I'd like to know where I'm off or what I'm missing. my idea was that I could access the normal page at

mydomain/Home  

Localized in the default language of the user. and then access overridden languages at:

mydomain/en-UK/Home
mydomain/da-DK/Home
mydomain/es-ES/Home

however it does not seem to work that way. I've yet to add so the Spanish could work because I was thinking to make it work with Danish and English first.

Someone Please tell me what I'm missing. or ask if I didn't provide enough detail regarding my problem.

1
I tried to correct my solution according to the post Akh made. it did solve some things now the website always comes up. however it always appears in my language and not the overridden language. and the default route where it should be the browser that decides automatically direct the other pages to the da-DK url instead of staying there if the page decided for itself it should just stay with the standard domain url. if the user want to override it should go to the da-DK or any of the others. there is one more problem when I try to override with en-GB then it still is displayed in DanishHelbo

1 Answers

1
votes
routes.MapRoute(
        name: "DefaultInternationalized",
        url: "{language}-{culture}/{controller}/{action}/{id}",
        defaults: new
        {
            controller = "Home",
            action = "Index",
            language = "en",
            culture = "UK",
            id = UrlParameter.Optional
        }
    );

1st problem with code is en-UK is wrong culture for UK. it is en-GB.

2nd you are not setting default culture in route.MapRoute. use this code

     routes.MapRoute(
          "DefaultLocalized",
          "{language}-{culture}/{controller}/{action}/{id}",
          new
          {
              controller = "Home",
              action = "Index",
              language = "da",
              culture = "DK",
              id = UrlParameter.Optional
          }
       );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new
            {
                controller = "Home",
                action = "Index",
                language = "en",
                culture = "US",
                id = UrlParameter.Optional
            } // Parameter defaults
        );

Or you can change your filter for default culture like this:-

public class InternationalizationAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        string language = (string)filterContext.RouteData.Values["language"]; //?? "da";
        string culture = (string)filterContext.RouteData.Values["culture"];// ?? "DK";
        language = language == null ? "en" : language;
        culture = culture == null ? "US" : culture;

        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(string.Format("{0}-{1}", language, culture));

    }
}

for culture info check out this link