使用TLD的ASP.Net MVC多语言站点

时间:2018-06-05 00:08:00

标签: asp.net model-view-controller cultureinfo

好的,我一直在摸着这个。简单来说,我试图根据用户访问网站的TLD设置文化信息,例如:

www.mysite.co.uk - 将文化设置为en-GB www.mysite.nl - 将文化设置为nl-NL

我在SO上关注了很多帖子,但似乎没有任何效果。创建附加的ActionFilterAttribute是我目前所在的位置:

public class LanguageFilter : ActionFilterAttribute
{
    private string _DefaultLanguage = "en";

    public LanguageFilter(string defaultLanguage)
    {
        _DefaultLanguage = defaultLanguage;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

        string host = null;
        Uri uri = filterContext.RequestContext.HttpContext.Request.Url;
        //host = uri.OriginalString;

        host = (String.IsNullOrEmpty(ConfigurationManager.AppSettings["OverrideDomain"].ToString()) ? uri.OriginalString : ConfigurationManager.AppSettings["OverrideDomain"].ToString());

        //string tld = GetRootDomain(host);
        string tld = GetTLD(host);

        string extension = null;

        if (tld.Contains("/"))
        {
            extension = tld.Split('/').First();
        }

        //extension = host.Split('.').Last().Replace("/", "");

        string cultureString = GetCulture(extension);

        string htmlLang = cultureString;

        var test = filterContext.RequestContext.HttpContext.Request.UserLanguages[0];

        string culture = htmlLang;

        // Save culture in a cookie
        HttpCookie cookie = filterContext.RequestContext.HttpContext.Request.Cookies["_culture"];
        if (cookie != null)
            cookie.Value = culture;   // update cookie value
        else
        {

            cookie = new HttpCookie("_culture");
            cookie.HttpOnly = false; // Not accessible by JS.
            cookie.Value = culture;
            cookie.Expires = DateTime.Now.AddDays(1);
        }

        filterContext.RequestContext.HttpContext.Response.Cookies.Add(cookie);

        Thread.CurrentThread.CurrentCulture =
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

    }

    public string GetTLD(string host)
    {
        // removing https for ease of checking if https or not
        host = host.Replace("https", "http");

        if (host.Contains("localhost"))
        {
            return ".co.uk";
        }

        string nexusSplit = host.Substring(27, host.Length - 27);

        return nexusSplit;
    }

    public string GetCulture(string auth)
    {
        switch (auth)
        {
            case "de":
                return "de-DE";

            case "es":
                return "es-ES";

            case "fi":
                return "fi-FI";

            case "fr":
                return "fr-FR";

            case "it":
                return "it-IT";

            case "nl":
                return "nl-NL";

            case "no":
                return "no-NO";

            case "pl":
                return "pl-PL";

            case "se":
                return "se-SE";

            case "si":
                return "si-SI";
        }

        return (String.IsNullOrEmpty(ConfigurationManager.AppSettings["OverrideCulture"].ToString()) ? "en-GB" : ConfigurationManager.AppSettings["OverrideCulture"].ToString()); ;
    }
}

0 个答案:

没有答案