ASP.Net MVC 3 中的全球化

发布于 2024-10-28 09:08:11 字数 82 浏览 6 评论 0原文

我正在尝试在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浊酒尽余欢 2024-11-04 09:08:11

您可以使用与任何其他应用程序相同的方式对其进行本地化,如下所示:

  1. 创建一个文件夹,将其命名为资源,
  2. 右键单击该文件夹并添加类...选择资源文件。将其命名为您喜欢的任何名称,例如 Strings.resx
  3. 在文件的属性下,将自定义工具更改为 PublicResXFileCodeGenerator
  4. 使用翻译键和值对填充资源文件(这将是默认翻译)
  5. 使用以下命令创建其他资源他们所适用的区域性的名称,格式如下:{name}.de.resx 例如 Strings.de.resx
  6. (适用于 Razor)破解打开 Views 文件夹中的 web.config 并将其添加到 /configuration/system .web.webPages.razor/pages/namespaces:<添加命名空间=“资源”/>> (假设 resources 是您在其中创建资源的文件夹的名称,并且您尚未更改资源文件本身的默认命名空间)。
    此步骤意味着您不必在每次想要引用翻译时完全限定视图中的资源类。
  7. 使用翻译来代替视图中的文本,如以下代码所示:

    <前><代码>@Strings.MyString

字符串将根据 CultureInfo.CurrentCulture 在视图中自动翻译,但这不会为您自动设置,

您需要更改CurrentCulture(可能在 Application_BeginRequest 中)。如何执行此操作取决于您,它可以是设置它的路由值,或者您可以读取用户的浏览器语言。

您可以在 HttpContext.Current.Request 中找到用户首选语言的列表(按顺序)。用户语言。

You localize it in the same way as any other application like this:

  1. Create a folder, call it e.g. Resources
  2. Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
  3. Under the properties of file, change Custom Tool to be PublicResXFileCodeGenerator
  4. Populate the Resource file with Translation key and value pairs (this will be the default translation)
  5. Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
  6. (This is for Razor) crack open the web.config in the Views folder and add this to /configuration/system.web.webPages.razor/pages/namespaces: <add namespace="Resources" /> (assuming resources is the name of the folder you created the resources in and you haven't changed the default namespace on the resouce files themselves).
    This step means you don't have to fully qualify the resource classes in your views each time you want to reference a translation.
  7. Use the translations in place of text in your views like with the following code:

    @Strings.MyString
    

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 in Application_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 language

You can find a list of the user's prefered languages (in order) in HttpContext.Current.Request.UserLanguages.

旧情勿念 2024-11-04 09:08:11

这是一篇关于 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

逆蝶 2024-11-04 09:08:11

要向 Martin Booth 的精彩答案 添加一些详细信息(以防他的 MediaFire 链接可能消失),我的做法如下:

我使用了两个文件,因为我现在只需要英语和德语(“de”):

在此处输入图像描述

对于每个文件的属性,我必须为每个文件手动输入自定义工具以及自定义工具命名空间值:

在此处输入图像描述

在此处输入图像描述

最后,我在根 Web.Config 文件中的 部分下方输入了以下内容:

<globalization uiCulture="auto" culture="auto" />

当然,我还在 Web.Config 文件中的 Views 文件夹(即不是根文件夹),正如 Martin 所描述的:

<add namespace="ViewResources" />

然后我终于可以访问我的(部分)Razor 视图中强类型的资源:

<h2>@ViewResources.Test1</h2>

顺便说一句:这也适用于 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:

enter image description here

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:

enter image description here

enter image description here

And finally, I entered the following inside the root Web.Config file, below the <system.web> section:

<globalization uiCulture="auto" culture="auto" />

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:

<add namespace="ViewResources" />

And then I could finally access the resources strongly-typed in my (partial) Razor view:

<h2>@ViewResources.Test1</h2>

BTW: this worked with MVC 4, too, not only MVC 3.

ら栖息 2024-11-04 09:08:11

您需要的下一步是本地化您的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文