turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 156 forks source link

In i18n.langtag using DefaultLanguage other than "en" is not working #365

Closed Jenitta-Rachel closed 6 years ago

Jenitta-Rachel commented 6 years ago

In my MVC application the user can change the language inside the WebApp using the i18n.langtag. When the default language is set to "en", the redirects according to the user's preference occur normally and everything is working fine, but when setting another default language than "en" (i.e. "es") on Application_Start the user is always redirected to the default language without respecting the set preference.

Am facing the same issue as mentioned in the issue link https://github.com/turquoiseowl/i18n/issues/250

I just want to know, if the User language is "English(en)" in browser and our Application language we setting by default is "Spanish(es)". Can't we get the default language as spanish ? Because on my startup am getting as "en" eventhough i set "es " in my startup() of global.asax

i18n.LocalizedApplication.Current.DefaultLanguage = "es-ES";

Can you please provide solution for this?

turquoiseowl commented 6 years ago

Do you have a locale\en folder?

If so, what happens when you delete it?

Jenitta-Rachel commented 6 years ago

I have the following structure in my locale folder

image

image

If I delete that, all my texts will be in english because i have my server application in english text. Is it possible to achieve my query ? or else I wanna change the browser setting to other language than "english" to achieve this.?

I have tried applying scheme2 as follows in startup() method of global.asax

        i18n.LocalizedApplication.Current.DefaultLanguage = "es-ES";
        i18n.UrlLocalizer.UrlLocalizationScheme = i18n.UrlLocalizationScheme.Scheme2;
        i18n.UrlLocalizer.DetermineDefaultLanguageFromRequest = delegate (HttpContextBase context)
        {
            if (context != null && context.Request.Url.Host.EndsWith(".es-ES", StringComparison.OrdinalIgnoreCase))
            {
                return i18n.LanguageTag.GetCachedInstance("es-ES");
            }
            return i18n.LocalizedApplication.Current.DefaultLanguageTag;
        };

it works fine, my application starts in spanish even if my browser language is in english, but here the language is implicit. But I want explicitly as scheme1 but scheme1 doesn't worked. Can you please update whether its feasible or not ? If possible, drop your solution. Thanks in Advance.

Jenitta-Rachel commented 6 years ago

Hi,

I have added the following code to resolve my issue after initializing i18n.UrlLocalizer.IncomingUrlFilters += delegate (Uri url),

var cookieLanguage = LanguageTag.GetCachedInstance(i18n.LocalizedApplication.Current.DefaultLanguage); HttpContext.Current.Response.Cookies.Add(new HttpCookie("i18n.langtag") { Value = cookieLanguage.ToString(), HttpOnly = true, Expires = DateTime.UtcNow.AddYears(1) }); HttpContext.Current.SetPrincipalAppLanguageForRequest(cookieLanguage);

turquoiseowl commented 6 years ago

Thanks for the update. That looks like a good solution. Are you happy to close the issue?

Jenitta-Rachel commented 6 years ago

yes am. You can close this issue. Hope it will be helpful for others. Thanks for your quick response @turquoiseowl