简体   繁体   English

在ASP.NET,Razor,javascript,dhtmlx或纯javascript中启用本地化

[英]Enabling localization in ASP.NET, Razor, javascript, dhtmlx or pure javascript

How do you go about enabling Globalization and Localization in ASP.NET, MVC, Razor, javascript and dhtmlx? 您如何在ASP.NET,MVC,Razor,javascript和dhtmlx中启用全球化和本地化? I have been looking around for answers and all references I found used the Globalization.js library from jquery. 我一直在寻找答案,发现的所有参考文献都使用了jquery的Globalization.js库。 Does anybody have example code using dhtmlx or pure javascript instead? 有人使用dhtmlx或纯javascript代替示例代码吗? I know how to reference resources within the .cshtml files (Razor) with @using Resources, but how do you reference these resources, like @Resources.UserID, within javascripts embeded within cshtml? 我知道如何使用@using资源引用.cshtml文件(剃刀)中的资源,但是如何在嵌入cshtml的javascript中引用这些资源,例如@ Resources.UserID? I am talking about scripts like ~/Scripts/Global.js. 我说的是〜/ Scripts / Global.js之类的脚本。

When I put this in web.config under system.web, create a resources.de.resx file and add de-DE to languages in IE, the title changes as expected using below code, my problem is referencing this in javascripts, so I can change text on forms etc. 当我将其放在system.web下的web.config中时,创建一个resources.de.resx文件并在IE中将de-DE添加到语言中,使用下面的代码按预期更改标题,我的问题是在javascript中引用了它,所以我可以更改表格等上的文本

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

login.cshtml: login.cshtml:

@using SquishIt.Framework
@using SquishIt.Mvc
@using Resources 
@{
    Layout = null;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>@Resources.Title</title>
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="-1" />
    <link rel="shortcut icon" type="image/x-icon" href="@Url.Content("~/Content/favicon.ico")" />
    @(Bundle.Css()
          .Add("~/Scripts/dhtmlx/dhtmlxLayout/codebase/dhtmlxlayout.css")
          .Add("~/Scripts/dhtmlx/dhtmlxLayout/codebase/skins/dhtmlxlayout_dhx_skyblue.css")
          .Add("~/Scripts/dhtmlx/dhtmlxWindows/codebase/dhtmlxwindows.css")
          .Add("~/Scripts/dhtmlx/dhtmlxWindows/codebase/skins/dhtmlxwindows_dhx_skyblue.css")
          .Add("~/Scripts/dhtmlx/dhtmlxForm/codebase/skins/dhtmlxform_dhx_skyblue.css")
          .Add("~/Scripts/dhtmlx/dhtmlxForm/codebase/skins/dhtmlxform_dhx_skyblue_custom.css")
          .Add("~/Content/Site.css")
          .MvcRender("~/Content/SquishIt/BaseLogon_#.css"))
</head>
<body oncontextmenu="return false;">
    <!-- Empty on purpose, JavaScript populates HTML -->
    <!-- Text ruler used to measure text from JavaScript -->
    <span id="TextRuler" class="TextRuler"></span>
</body>
<script language="javascript" type="text/javascript">
    var SKIN_NAME          = "dhx_skyblue";

    var URL_WINDOWS_IMAGES = "@Url.Content("~/Scripts/dhtmlx/dhtmlxWindows/codebase/imgs/")";
    var URL_LOGIN_FORM     = "@Url.Content("~/XML/Forms/Base/Logon.xml")";
    var URL_LOGIN          = "@Url.Content("~/Base/Login/")";
    var URL_MAIN           = "@Url.Content("~/Base/Main/")";

    var LOGON_SESSION_ID   = "@(Session["LOGON_SESSION_ID"])";
    var LOGOFF_MESSAGE     = "@(ViewData.ContainsKey("LogOffMessage") ? ViewData["LogOffMessage"] : "")";
</script>
@(Bundle.JavaScript()
      .Add("~/Scripts/dhtmlx/dhtmlxLayout/codebase/dhtmlxcommon.js")
      .Add("~/Scripts/dhtmlx/dhtmlxLayout/codebase/dhtmlxlayout.js")
      .Add("~/Scripts/dhtmlx/dhtmlxLayout/codebase/dhtmlxcontainer.js")
      .Add("~/Scripts/dhtmlx/dhtmlxWindows/codebase/dhtmlxwindows.js")
      .Add("~/Scripts/dhtmlx/dhtmlxForm/codebase/dhtmlxform.js")
      .Add("~/Scripts/Global.js")
      .Add("~/Scripts/Utility/Browser.js")
      .Add("~/Scripts/Utility/XML.js")
      .Add("~/Scripts/Utility/ErrorHandler.js")
      .Add("~/Scripts/Utility/Form.js")
      .Add("~/Scripts/Utility/MessageBox.js")
      .Add("~/Scripts/Base/Logon.js")
    .MvcRender("~/Content/SquishIt/BaseLogon_#.js"))
</html>

Take a look at this post, it offers an alternative way that i have implemented on my web app.. 看一下这篇文章,它提供了我在Web应用程序中实现的另一种方法。

http://madskristensen.net/post/Localize-text-in-JavaScript-files-in-ASPNET.aspx http://madskristensen.net/post/Localize-text-in-JavaScript-files-in-ASPNET.aspx

You will have to create an HTTML handler that will translate your js files according to specific tags. 您将必须创建一个HTTML处理程序,该处理程序将根据特定标签转换js文件。

There is also some configuration required for the routing engine of mvc. mvc的路由引擎还需要一些配置。 This code should go in your global.asax.cs: 此代码应放在您的global.asax.cs中:

    public static void RegisterRoutes(RouteCollection routes)
    {
        //This will send .axd filse to the custom translate handler
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("Scripts/{folder}/{resource}.js.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

NOTE! 注意! If you use ISS 7 then you should register the handler like this: 如果使用ISS 7,则应按以下方式注册处理程序:

 <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <add name="ScriptTranslatorHandler" path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
    </handlers>
  </system.webServer>

Use validateIntegratedModeConfiguration="false" to support backward comparability and add the previously known HTTP handler element. 使用validateIntegratedModeConfiguration="false"支持向后可比性,并添加以前已知的HTTP处理程序元素。

  <system.web>
     <httpHandlers>
          <add path="*.js.axd" verb="*" type="CamelotShiftManagement.HttpHandlers.ScriptTranslator" />
          <add path="*" verb="*" type="System.Web.HttpNotFoundHandler" />
        </httpHandlers>
  </system.web>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM