Localizing content on MVC sites

To serve content in multiple languages on your MVC site, you first need to set up functionality that detects and sets the current culture for each request. You can then assign cultures to your site in Kentico, and localize the content displayed on the site’s pages.

Localizing site content

When retrieving page content, load the correct culture version of pages based on the current culture of the request.

For individual text strings displayed on the site (which are not stored within page fields), we recommend using multilingual resource strings. You can create and edit the strings in the Localization application of the Kentico administration interface.

To retrieve localized strings in the MVC application, add a using statement for the CMS.Helpersnamespace, and use the ResHelper class:




@using CMS.Helpers;
...
<h2>@ResHelper.GetString("SiteName.OurProducts")</h2>





@using CMS.Helpers;
...
@{
    ViewBag.Title = ResHelper.GetString("SiteName.OurProducts");
}


Localizing validation results and model properties

The default approach to validating the data model of MVC applications is to decorate the model and its properties with attributes from the System.ComponentModel.DataAnnotations namespace. You use the attributes to define common validation patterns, such as range checking, string length and required fields.

The Kentico.AspNet.Mvc integration package provides a feature that allows you to use localized Kentico resource string keys as error messages in data annotation attributes. The strings are localized based on the current culture context of the current visitor. The Kentico.LanguagePack.English integration package (installed with the Kentico.AspNet.Mvc integration package) contains a .resx filewith the resource strings used in the English localization of Kentico. Aside from that, you can also use the standard resource strings stored in the database.

The feature supports localization of the following data annotation attributes:

  • Display
  • DisplayName
  • DataType
  • MaxLength
  • MinLength
  • Range
  • RegularExpression
  • Required
  • StringLength

Enabling data annotation localization

To enable localization of data annotations in your MVC application:

  1. Open your MVC project in Visual Studio.

  2. Enable the localization feature by calling the UseDataAnnotationsLocalization() method of the ApplicationBuilder instance.

    • Enable the feature at the start of your application’s life cycle, for example in the Application_Start method of your project’s Global.asax file.

      MVC projects created by the installer contain the ApplicationConfig class, whose RegisterFeatures method is called in the Application_Start method by default. You can use this class to encapsulate all of your ApplicationBuilder code (enabling and configuring of Kentico MVC features).

      
      
      
        using Kentico.Web.Mvc;
      
        ...
      
        protected void Application_Start()
        {
            ...
      
            // Gets the ApplicationBuilder instance
            // Allows you to enable and configure selected Kentico MVC integration features
            ApplicationBuilder builder = ApplicationBuilder.Current;
      
            // Enables the data annotation localization feature
            builder.UseDataAnnotationsLocalization();
      
            ...
        }
      
      
        

If the feature is enabled, validation results and display names of model properties are localized using Kentico localization services.




public class MessageModel
{
...
     [Required(ErrorMessage = "General.RequiresMessage")]
     [Display(Name = "General.Message")]
     [DataType(DataType.MultilineText)]
     [MaxLength(500, ErrorMessage = "General.MaxlengthExceeded")]
     public string MessageText
     {
         get;
         set;
     }
...
}


Note: Individual error messages are processed by the System.String.Format method and support composite formatting. That is, the strings themselves can contain format items that are specific to each validation attribute and represent their parameters. For example, the minimum length for the MinLenghtAttribute or the minimum and maximum for the RangeAttribute.