asp.net-mvc 根据 ASP.NET MVC 4 中的资源文件更改语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10090815/
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-08 01:51:35  来源:igfitidea点击:

Change language based on resources files in ASP.NET MVC 4

asp.net-mvcrazorresourcesasp.net-mvc-4

提问by Misi

I have 2 resource file : Resources.resx(has some strings in Romanian) and Resources.en-US.resx(has the same strings in English).

我有 2 个资源文件:Resources.resx(有一些罗马尼亚语字符串)和Resources.en-US.resx(有相同的英语字符串)。

I only want to change(in a dropdownlist, a listbox,...) witch resource file to use. It could be in _Layout.cshtml. I don't need to detect the user's culture.

我只想更改(在下拉列表、列表框、...中)要使用的女巫资源文件。它可能在 _Layout.cshtml 中。我不需要检测用户的文化。

Q: How can I select a resource file from a page ?

问:如何从页面中选择资源文件?

Edit : Can it be done without changing the default MapRoute ?

编辑:可以在不更改默认 MapRoute 的情况下完成吗?

采纳答案by kmp

One way you can do it is to have the drop down just redirect the page to a language specific URL (this is quite nice as you can send around language specific links) then in a base class on your controller, set the Thread's locale.

您可以这样做的一种方法是让下拉菜单仅将页面重定向到特定语言的 URL(这非常好,因为您可以发送特定语言的链接),然后在控制器的基类中设置线程的区域设置。

This blog post covers what I am talking about in better detail: Localization in ASP.NET MVC – 3 Days Investigation, 1 Day Job

这篇博文更详细地介绍了我正在谈论的内容: ASP.NET MVC 中的本地化 – 3 天调查,1 天工作

回答by Min Min

Check this Blog. Without changing default MapRoute.

检查此博客。无需更改默认 MapRoute。

The _Layout.cshtml page:

_Layout.cshtml 页面:

@using Resources;
<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div>
            <form method="post">
                @TestResource.SelectLanguage
                <select name="lang">
                    <option></option>
                    <option value="en-GB" @(Culture == "en-GB" ? "selected=\"selected\"" : "")>English</option>
                    <option value="fr-FR" @(Culture == "fr-FR" ? "selected=\"selected\"" : "")>French</option>
                    <option value="de-DE" @(Culture == "de-DE" ? "selected=\"selected\"" : "")>German</option>
                </select>
                <input type="submit" value="@TestResource.Submit" />
            </form>
        </div>
        @RenderBody()
    </body>
</html>

The culture is set within the _PageStart.cshtml file:

文化在 _PageStart.cshtml 文件中设置:

@{
    Layout = "~/_Layout.cshtml";
    if(!Request["lang"].IsEmpty()){
        Culture = UICulture = Request["lang"];
    }
}

The final page is the Default page itself:

最后一页是默认页面本身:

@using Resources;
<h1>@TestResource.Welcome</h1>
<p><img src="images/@TestResource.FlagImage" /></p>

http://www.mikesdotnetting.com/Article/183/Globalization-And-Localization-With-Razor-Web-Pages

http://www.mikesdotnetting.com/Article/183/Globalization-And-Localization-With-Razor-Web-Pages