只有2种语言 - - 建立一个语言切换器ASP.NET MVC [英] Building a language switcher - 2 languages only - ASP.NET MVC

查看:97
本文介绍了只有2种语言 - - 建立一个语言切换器ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC应用程序,接近完成,那么它决定它需要一个语言切换器。这将仅仅是两种语言:英语和日语。他们应该能够在两者之间切换,但如果不选择,应该选择哪个是浏览器的默认语言。

ASP.NET MVC app, close to completion, then it was decided that it needed a language switcher. It will simply be two languages: English and Japanese. They should be able to switch between the two, but if one is not selected it should choose whichever is the default language of the browser.

我想知道最好的解决办法是在这种情况下的东西。

I'm wondering what the best solution would be in this case.

首先想到,路线!所以我改变路线,有一个 / {1} 每个路线的结束,拖欠 L =。然后,在我的BaseController(每个控制器是基于关闭它),我检查了路线●变量。每个动作返回基于语言的景色。

First thought, routes! So I change the routes to have a /{l} on the end of every route, and defaulted l = "". Then, in my BaseController (every controller is based off of it), I check the l variable in the route. Each action returns a view based on the language.

我想仅仅是能够破解的URL的末尾/ JA-JP,它会显示出日本的看法。它似乎并不很路由正确。然后,我在我的观点和手codeD的联系真正的坏...我不能完全得到辅助输出首先正确的链接...钻进手工编码它们的坏习惯。所以,我不得不再次重新code的每一个环节 - 砍上年底的语言变量

I wanted to simply be able to hack /ja-jp on the end of the url, and it would show the view in Japanese. It didn't quite seem to route correctly. Then, I was real bad in my views and hand coded the links... I couldn't quite get the helper to output the right links at first... and got into a bad habit of hand coding them. So, I'd have to re-code every link again - to hack the language variable on the end.

退一步讲 ...好,使用jQuery语言变量追加到所有锚的href。这似乎过于笨拙,难以在某些情况下的HREF被隐藏,直到需要,等等...

Second thought... ok, use jQuery to append the language variable to all anchor's href. That seems overly clumsy and difficult in certain situations where hrefs are hidden until needed, etc...

所以......在这一点上我已经决定只是不惜一切代价。什么是2种语言之间进行切换最优雅的方式 - 不使用资源文件?我只是希望动作2次之间进行选择,基于语言。

So... at this point I've decided to just do whatever it takes. What is the most elegant way to switch between 2 languages - without using resource files? I simply want the action to choose between 2 views, based on language.

我重写每一次我一定要使用HTML帮助,然后获得工作路线链接?然后在BaseController只需设置语言为英语,如果他们是没有价值?

Do I rewrite every link I've got to use the Html helper and then get the routes working? Then in the BaseController just set the language to English if their is no value?

任何意见是非常AP preciated,谢谢你。

Any advice is much appreciated, thank you.

推荐答案

我觉得最简单的和用户友好的方法之一是使用域(子域实际上),以确定它说什么语言。它并不需要改变任何东西在HTML帮助(这些HTML.ActionLink),因为它的工作原理只是相对域。此外,它可能看起来整洁用户,看到该域的时候,他们确切地知道它应该用什么语言来定,并没有使得网址太长。所有你需要做的是做一些在语言切换工作。

I think one of the most easiest and user-friendly method is to use the domain (sub-domain actually) to identify what language it speaks. It doesn't need to change almost anything in the Html helper (those HTML.ActionLink) since it works only relatively to domain. Also, it might looks neat to user that when seeing that domain, they know exactly what language it supposed to be, and without making the url too long. All you need to do is to do something in the language switcher to work.

不管用什么办法,我强烈反对使用第二个想法,因为你不能否认(甚至0.1%)没有JavaScript的人民一道,访问您的日本网站,对吧?

No matter what approach, I strongly discourage using the second thought, since you can't deny (even 0.1%) people not having javascript to work to visit your japanese web site, right?

这篇关于只有2种语言 - - 建立一个语言切换器ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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