ASP.Net MVC 3 中的全球化
我正在尝试在我的 MVC 3 应用程序中实现全球化/本地化。我不希望每种语言有不同的视图。请建议我如何继续。任何受支持的链接/URL 都会有很大帮助。
I am trying to achieve globalization/localization in my MVC 3 application. I don't want different Views for each language. Please suggest how I can proceed. Any supported links/URLs will be of great help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用与任何其他应用程序相同的方式对其进行本地化,如下所示:
PublicResXFileCodeGenerator
此步骤意味着您不必在每次想要引用翻译时完全限定视图中的资源类。
使用翻译来代替视图中的文本,如以下代码所示:
<前><代码>@Strings.MyString
字符串将根据 CultureInfo.CurrentCulture 在视图中自动翻译,但这不会为您自动设置,
您需要更改
CurrentCulture
(可能在Application_BeginRequest
中)。如何执行此操作取决于您,它可以是设置它的路由值,或者您可以读取用户的浏览器语言。您可以在 HttpContext.Current.Request 中找到用户首选语言的列表(按顺序)。用户语言。
You localize it in the same way as any other application like this:
PublicResXFileCodeGenerator
This step means you don't have to fully qualify the resource classes in your views each time you want to reference a translation.
Use the translations in place of text in your views like with the following code:
Strings will be automatically translated in the view depending on CultureInfo.CurrentCulture but this is not set automatically for you
You will need to change the
CurrentCulture
(potentially inApplication_BeginRequest
). How you do this is up to you, it could be a route value which sets it or you can read the user's browser languageYou can find a list of the user's prefered languages (in order) in
HttpContext.Current.Request.UserLanguages
.这是一篇关于 MVC 3 全球化/国际化的详细文章 http://afana .me/post/aspnet-mvc-internationalization-part-2.aspx
Here is a great detailed post about MVC 3 Globalization/Internationalization http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx
要向 Martin Booth 的精彩答案 添加一些详细信息(以防他的 MediaFire 链接可能消失),我的做法如下:
我使用了两个文件,因为我现在只需要英语和德语(“de”):
对于每个文件的属性,我必须为每个文件手动输入自定义工具以及自定义工具命名空间值:
最后,我在根 Web.Config 文件中的
部分下方输入了以下内容:当然,我还在 Web.Config 文件中的 Views 文件夹(即不是根文件夹),正如 Martin 所描述的:
然后我终于可以访问我的(部分)Razor 视图中强类型的资源:
顺便说一句:这也适用于 MVC 4,不仅如此3.MVC
To add some details to Martin Booth's great answer (in case his MediaFire link might disappear), here is how I idid it:
I've used two files, since I only need English and German ("de") for now:
For the properties of each file, I had to manually enter the Custom Tool as well as the Custom Tool Namespace values, for each file:
And finally, I entered the following inside the root Web.Config file, below the
<system.web>
section:Of course I've also added the namespace directive in the Web.Config file below the Views folder (i.e. not the root one), as Martin describes:
And then I could finally access the resources strongly-typed in my (partial) Razor view:
BTW: this worked with MVC 4, too, not only MVC 3.
您需要的下一步是本地化您的 Javascript 库。
看一下这里: MVC-JavaScript-localization- of-external-js-files
The next step that you need is to localize your Javascript library.
Take a look here: MVC-JavaScript-localization-of-external-js-files