2

this is my scenario: I have a multilanguage Asp.Net Mvc Application. Here is my localized default route:

routes.MapRoute(
    name: "DefaultLocalized",
    url: "{lang}/{controller}/{action}/{id}",
    constraints: new { lang = @"(\w{2})|(\w{2}-\w{2})" },   // en or en-US
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

My ControllerActivator:

public class LocalizedControllerActivator : IControllerActivator
{
    private string _DefaultLanguage = "it";

    public IController Create(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        //Get the {language} parameter in the RouteData
        string lang = (string)requestContext.RouteData.Values["lang"] ?? _DefaultLanguage;

        if (lang != _DefaultLanguage)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture =
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
            }
            catch (Exception e)
            {
                throw new NotSupportedException(String.Format("ERROR: Invalid language code '{0}'.", lang));
            }
        }
        return DependencyResolver.Current.GetService(controllerType) as IController;
    }
}

That I initialize in Global.asax:

ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new App_Start.LocalizedControllerActivator()));

And this is the method that redirects localized routes:

public ActionResult Language(string languageCode)
{
    //ViewBag.lang = languageCode;
    string basePath = string.Format("{0}://{1}{2}", Request.UrlReferrer.Scheme, Request.UrlReferrer.Authority, Url.Content("~"));
    object lang;
    if (!RouteData.Values.TryGetValue("lang", out lang))
        return Redirect(Request.UrlReferrer.ToString().Replace(basePath, string.Format("{0}{1}/", basePath, languageCode)));
    else
    {
        string basePathWithLang = string.Format("{0}{1}", basePath, lang.ToString());
        return Redirect(Request.UrlReferrer.ToString().Replace(basePathWithLang, string.Format("{0}{1}", basePath, languageCode)));
    }
}

Problem:

On my machine works like a charm, but when I put on the external hosting that I work with, words that I use in global resources files, doesn't work. In my home page I have language selection:

<li><a href="@Url.Action("Language","Home",new { languageCode="it" })">Italiano</a></li>
<li><a href="@Url.Action("Language","Home",new { languageCode="en" })">English</a></li>

The word contained in: @Resources.Global.contacts if I change language is not translated. Maybe this can help: when I open my website at abcxxx.it the start language is english, instead of italian that I set as default in my code.

SamDroid
  • 601
  • 1
  • 10
  • 28

1 Answers1

1

You know what, there is no problem with the code. It's the problem with logic.

You are setting _DefaultLanguage to it.

And now when you are getting lang from the routevalues that also come as it. So it won't go inside block if (lang != _DefaultLanguage) because they both are same and CurrentThread's CurrentCulture and CurrentUICulture won't be changed. It will remain the unchanged and set to en-US.

So what you need to do is remove if clause and assign 'lang to the CurrentThread's CurrentCulture and CurrentUICulture in any case.

public class LocalizedControllerActivator : IControllerActivator
{
    private string _DefaultLanguage = "it";

    public IController Create(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        //Get the {language} parameter in the RouteData
        string lang = (string)requestContext.RouteData.Values["lang"] ?? _DefaultLanguage;

        try
        {
            System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
        }
        catch (Exception e)
        {
            throw new NotSupportedException(String.Format("ERROR: Invalid language code '{0}'.", lang));
        }
        return DependencyResolver.Current.GetService(controllerType) as IController;
    }
}

This should resolve the issue.

Chetan
  • 6,711
  • 3
  • 22
  • 32