asp.net-mvc ASP.NET MVC 切换语言,如何实现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/928053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:37:02  来源:igfitidea点击:

ASP.NET MVC switch language, how to implement?

asp.net-mvcasp.net-mvc-routing

提问by miccet

I have started converting my simple website to ASP.NET MVC, just to mess around with it. I have a switch language feature on there that basically sets the Session["language"] to another language and refreshes the page. Please correct me if this could be done better, but I have made two controllers for this and setting the session in there. The problem is the routing at the end. Could I refresh the page in some neat way, or can I get the current Action and re-route it to that? Or is this more a scenario for Ajax?

我已经开始将我的简单网站转换为 ASP.NET MVC,只是为了弄乱它。我在那里有一个切换语言功能,它基本上将 Session["language"] 设置为另一种语言并刷新页面。如果这可以做得更好,请纠正我,但我为此制作了两个控制器并在那里设置会话。问题是最后的路由。我可以以某种巧妙的方式刷新页面,还是可以获得当前的 Action 并将其重新路由到该页面?或者这更像是 Ajax 的场景?

Thankful for advice!

多谢指教!

采纳答案by russau

is there any reason why you are using the session variable? a more common solution is to include the language code in the route, i.e. blah.com/en/info or blah.com/jp/info (for english and japanese)

你有什么理由使用会话变量吗?更常见的解决方案是在路线中包含语言代码,即 blah.com/en/info 或 blah.com/jp/info(适用于英语和日语)

if you did this every page on the site could contain links to each language. if you are writing a publicly accessible site this would also make it easier for google to index all your content.

如果您这样做,网站上的每个页面都可以包含指向每种语言的链接。如果您正在编写一个可公开访问的网站,这也将使谷歌更容易索引您的所有内容。

this article explains how to include the language in the domain, ie. en.blah.com or jp.blah.com: http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx

这篇文章解释了如何在域中包含语言,即。en.blah.com或jp.blah.com:http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx

UPDATED: Here's a simpleexample of including the language code in the URL route.

更新:这是一个在 URL 路由中包含语言代码的简单示例。

Change the default route to include a language parameter:

更改默认路由以包含语言参数:

routes.MapRoute(
"Default", 
"{language}/{controller}/{action}/{id}", 
new { language = "en", controller = "Home", action = "Index", id = "" }
);

Add links for each language to your masterpage:

将每种语言的链接添加到您的主页:

<li><%= Html.ActionLink(
    "Spanish", 
    ViewContext.RouteData.Values["action"].ToString(), 
    new { language = "es" })%></li>
<li><%= Html.ActionLink(
    "French", 
    ViewContext.RouteData.Values["action"].ToString(), 
    new { language = "fr" })%></li>
<li><%= Html.ActionLink(
    "English", 
    ViewContext.RouteData.Values["action"].ToString(), 
    new { language = "en" })%></li>    

These will render as links back to the page you are on - only with the language changed.

这些将呈现为返回您所在页面的链接 - 仅当语言发生变化时。

回答by omoto

Following approach works good for me:

以下方法对我有用:

I'm using cookies and my own engine for localization All you need to put some link on the page that will redirect to something like this:

我正在使用 cookie 和我自己的本地化引擎所有你需要在页面上放置一些链接,这些链接将重定向到这样的内容:

public class LanguageController : Controller
{
    //
    // GET: /Language/

    public void Change(string id)
    {
        var cuka = new HttpCookie("lang", id + "");
        cuka.Expires = DateTime.Now.AddYears(10);
        System.Web.HttpContext.Current.Response.Cookies.Add(cuka);

        if (Request.UrlReferrer.IsNotNull())
            Response.Redirect(Request.UrlReferrer.AbsoluteUri);
        else
            Response.Redirect("/");
    }

}

}

}

If you're interested in this engine, let me know.

如果你对这个引擎感兴趣,请告诉我。

回答by Hamid Jolany

there is a controller for the language management

有一个用于语言管理的控制器

    public class LocalesController : Controller
{

    public ActionResult Index(string lang = "en_US")
    {
        Response.Cookies["CacheLang"].Value = lang;

        if (Request.UrlReferrer != null)
            Response.Redirect(Request.UrlReferrer.ToString());

        var message = Localization.Get("changedlng");

        return Content(message);
    }

}

you can call it separately

你可以单独调用

new LocalesController().Index("fa");