简体   繁体   English

在javascript中本地化字符串的最简单方法

[英]Easiest way to localize string in javascript

in my mvc4 application I have inside layout view javascript function which has only 3 strings which are rendered inside users view. 在我的mvc4应用程序中,我具有内部布局视图javascript函数,该函数只有3个在用户视图内部呈现的字符串。

My mvc4 app is localized on 4 languages using cookie value and based on that value current thread is set. 我的mvc4应用使用cookie值本地化为4种语言,并基于该值设置了当前线程。

What is the easiest solution to localize those 3 strings. 定位这3个字符串的最简单解决方案是什么。

<script type="text/javascript">
   var message = "Please enter value in valid range in order to ...";
   ....
</script>

The easiest sting to do would be to "set" the localized strings in your controller, in a variable, then build a JavaScript variable to use, from that: 最简单的方法是在控制器中的变量中“设置”本地化的字符串,然后从中构建要使用的JavaScript变量:

<script type="text/javascript">
    userName = "@Model.UserName";
    someSetting = parseInt("@Model.someSetting");
    messages = { // Or even an object
        InvalidRights: "@Html.Raw(ValidationMessages.UserHasInvalidRights)",
        FileRequired: "@Html.Raw(ValidationMessages.FileIsRequired)"
    };
</script>

You can also make use of standard resource files within your model and property annotations to help a bit. 您还可以在模型和属性注释中使用标准资源文件以提供帮助。

public class MyModel:BaseModel
{
    [Required(ErrorMessage = "*")]
    [Display(ResourceType = typeof(RES.labels),Name = "lblLanguageCode")]
    public string LanguageCode { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(ResourceType = typeof(RES.labels), Name = "lblCountryCode")]
    public string CountryCode{ get; set; }
...
}

You can return anything in the model I just showed a way to nicely wire up the translations in the model. 您可以返回模型中的任何内容,我刚刚展示了一种很好地连接模型中翻译的方法。

<script type="text/javascript">
    test= '@Model.SomeErrorString';
</script>

You could use the 'Global Resources' functionality. 您可以使用“全局资源”功能。

Using this, you can create a set of 'translation' files (known as Resource files) which contain all the various translations you would need for your web application. 使用此功能,您可以创建一组“翻译”文件(称为资源文件),其中包含Web应用程序所需的所有各种翻译。

The following article gives you an example of doing this: http://www.developmentalmadness.com/archive/2009/05/20/aspnet-mvc-globalizationlocalization.aspx 以下文章提供了执行此操作的示例: http : //www.developmentalmadness.com/archive/2009/05/20/aspnet-mvc-globalizationlocalization.aspx

That article uses the older Web Form View Engine, but it would work within the Razor View Engine too (@ symbol instead of the <%= %>). 该文章使用了较旧的Web Form View Engine,但它也可以在Razor View Engine中使用(@符号代替<%=%>)。

Let's say you added a Resource file called 'MyTranslations', and then added your translations to this file, your html would now look something like the following: 假设您添加了一个名为“ MyTranslations”的资源文件,然后将翻译添加到了该文件中,那么您的html现在将类似于以下内容:

<script type="text/javascript">
  messages = {
      AccessDenied: "@Html.Raw(Resources.MyTranslations.AccessIsDenied)",
      FieldRequired: "@Html.Raw(Resources.MyTranslations.FieldRequired)"
  };
</script>

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

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