0

in my mvc4 application I have inside layout view javascript function which has only 3 strings which are rendered inside users view.

My mvc4 app is localized on 4 languages using cookie value and based on that value current thread is set.

What is the easiest solution to localize those 3 strings.

<script type="text/javascript">
   var message = "Please enter value in valid range in order to ...";
   ....
</script>
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • 1
    The easiest way (in your case) is to leverage razor to set the JavaScript variable, see this post: http://stackoverflow.com/questions/4599169/using-razor-within-javascript. – Mike Perrenoud May 05 '14 at 13:41

3 Answers3

0

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:

<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>
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • Since this javascript string is in Layout view I would than need to send variable from every controller which returns view if I want to see localized string on every page. – user1765862 May 05 '14 at 13:58
  • Unless you add something like `` to your `Web.config`, and have your localized strings in that namespace. – Cerbrus May 05 '14 at 14:01
  • can you please be more specific: I have only 3 strings and each one is already localized in LocStrings.en-US, LocStrings.es-MX, .... – user1765862 May 05 '14 at 14:05
0

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>
Ross Bush
  • 14,648
  • 2
  • 32
  • 55
-1

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.

The following article gives you an example of doing this: 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 <%= %>).

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:

<script type="text/javascript">
  messages = {
      AccessDenied: "@Html.Raw(Resources.MyTranslations.AccessIsDenied)",
      FieldRequired: "@Html.Raw(Resources.MyTranslations.FieldRequired)"
  };
</script>
ctrlplusb
  • 12,847
  • 6
  • 55
  • 57
  • Hmmm, I disagree with you that this is a verbatim of your answer. You talk about "setting the localized strings in your controller, in a variable". My solution is disconnected from any controller or viewmodel. It uses the built in Resource Management features of .NET allowing for reuse of your translations without having to attach them to your view models repeatedly. Perhaps you should read peoples answers more deeply before rebutting with unfriendly sarcasm. This is a site to helpful on. – ctrlplusb Jun 05 '14 at 09:43