使 URL 语言特定(通过路由) [英] Make URL language-specific (via routes)

查看:24
本文介绍了使 URL 语言特定(通过路由)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难找到这个特定问题的答案,所以在这里弹出它:

Its hard to find an answer to this specific question, so ill pop it up here:

我们希望建立完全特定于语言的网址,含义

We want to build up our urls completely language specific, meaning

www.mysite.com/EN

www.mysite.com/EN

www.mysite.com/DE

www.mysite.com/DE

这是在 RouteConfig 中使用

this is done in the RouteConfig with

url: "{language}/{controller}/{action}/{id}"

然后是棘手的部分:

www.mysite.com/EN/Categories

www.mysite.com/EN/Categories

www.mysite.com/DE/Kategorien

www.mysite.com/DE/Kategorien

即使控制器名称以多种语言出现,但指向同一个控制器.这甚至可能吗?

I.e. make the controllername appear in a multiple languages, but point to the same controller. Is this even possible?

推荐答案

嗯,这是部分可能的(仅语言部分).不同语言的控制器名称绝对是一个有趣的点,但我认为很难做到这一点.想想像阿拉伯语和希伯来语这样的比迪语会是什么样子.在不同的语言中使用控制器可能是个好主意,但您会为自己造成严重破坏,我相信您必须更改底层 MVC 结构才能实现这一点.

Well, this is partially possible (the language part only). The controller name in a different language is definitely an interesting point, but I think it would be hard to achieve that. Think about what it would look like for Bidi languages like Arabic and Hebrew. It would probably be a good idea to use controller in different languages but you would create a havoc for yourself and I believe that you would have to change the underlying MVC structure to allow for this.

语言更改部分很容易,可以像下面这样完成.

The language changing part is easy and could be done like below.

您可能想看的是全球化.基本上语言部分对应于当前线程 UI 文化.您需要的是以下内容:

What you might want to look at is the globalization. Basically the language part corresponds to the current threads UI culture. What you need is the following:

  1. 定义一条路线,如下所示:

  1. Define a route, something like this:

var lang = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;

routes.MapRoute(
    name: "Default",
    url: "{language}/{controller}/{action}/{id}",
    defaults: new { language = lang, controller = "Home", 
                    action = "Index", id = UrlParameter.Optional }
 );

  • 注册 Application_AcquireRequestState 并定义如下:

    protected void Application_AcquireRequestState()
    {
        var routes = RouteTable.Routes;
    
        var httpContext = Request.RequestContext.HttpContext;
        if (httpContext == null) return;
    
        var routeData = routes.GetRouteData(httpContext);
    
        var language = routeData.Values["language"] as string;
        var cultureInfo = new CultureInfo(language);
    
        System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
        System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }
    

    虽然 CurrentUICulture 是您需要从资源文件中切换语言加载信息,但您还应该将 CurrentCulture 更改为相同的 CultureInfo

    Although CurrentUICulture is what you need switch languages load information from a resource file, you should also change the CurrentCulture to the same CultureInfo;

    最后,确保你有相应的资源文件和备用资源文件.

    Last, make sure you have corresponding resource files and a fallback resource file.

    我使用了 CultureInfoName 属性,因此您的德语将是 de-DE、English en-US 等.这应该可以解决问题.

    I used Name property of CultureInfo so you German would be de-DE, English en-US, etc. This should do the trick.

    如果您需要更多信息,我可以上传一个示例 MVC 项目供您检查.

    In case you need more information I could upload a sample MVC project for you to examine.

    更新:做你想做的一种粗略的方法是将路线段的顺序颠倒成这样的:

    UPDATE: One crude way of doing what you want is to invert the order of route segments to something like this:

    routes.MapRoute(
        name: "NewDefault",
        url: "{language}/{id}/{action}/{controller}",
        defaults: new { language = lang, controller = "Home", action = "Index", id = "Category"}
    );
    

    这样您就可以提出以下请求http://www.your_url.com/de/Kategorien.在这种情况下,类别映射到 id 而不是控制器.您的控制器使用英语或德语(取决于您如何命名),但用户会看到不同的语言.在后台,您的视图可能是这样的:

    This way you could make a following request http://www.your_url.com/de/Kategorien. In this case Kategorien maps to the id rather than a controller. You controller stays in English or German (depending how you named it) but user sees a different language. In the background your view might be something like this:

    public ActionResult Index(string id, string categoryName)
    {
        // Fetch data based on category name (categoryName)
        return View();
    }
    

    您可以将其他信息作为参数传递,但您需要调整您的路线,使其看起来像:{language}/{category}/{subcategory}/{action}/{controller}

    You could pass additional information as parameters, but you will need to adjust your route to look something like: {language}/{category}/{subcategory}/{action}/{controller}

    要知道,从长远来看,这可能会导致颈部疼痛,如果您尝试这样做,请确保做好记录.

    Just know that this can become pain in the neck in the long run and if you try this, then make sure you document it well.

    这篇关于使 URL 语言特定(通过路由)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆