How To Localize Display Template In SharePoint 2013

Display Templates are localized with the help of resource files. A resource file is referenced inside a Display Template in the following way.

 

<script>

 $includeLanguageScript(this.url,"~sitecollection/_catalogs/masterpage/Display Templates/Language Files/{Locale}/Strings.js");

</script>

 

A resource file is a JavaScript file which registers a resource dictionary. The resource dictionary contains the resource strings.  For example, a resource file for "en-us" Locale with some resource strings is shown below.

 

// Add your custom localized strings and then include these string dictionaries in your display templates using the $includeLanguageScript function

$registerResourceDictionary("en-us", {"resourceKey1": "resourceValue1", "resourceKey2": "resourceValue2", "resourceKey3": "resourceValue3"});

 

For changing Display Template content with culture, create a resource file for each of the supporting culture and deploy it to the master page gallery in the language files folder, in the corresponding Locale folder.

Then, use the following code to reference the resource key in the display template.

 

var resourceString1 = $resource("resourceKey1");   //This will fetch the value - resourceValue1