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.