Internationalized URL Title code

Description

The default url-title.vtl is ASCII-only:  any characters that are not letters of the English alphabet (or a digit or a hyphen) are discarded.  This version allows you to specify your alphabet, and if no alphabet is specified then it URLencodes every character.  This is ugly but reversible.

Link: https://groups.google.com/d/msg/dotcms/PCSJKiDGkbo/Wxr5ac6PR3UJ

Attachment: url-title.vtl (3 KB)

Code

#set($cusFieldVarName="title")

#set($currentAlphabet = "") ## No default alphabet:  just URLencode everything
##set($currentAlphabet = "a-zA-Z") ## English alphabet
##set($currentAlphabet = "[\u0400-\u052F]") ## Cyrillic alphabet and supplement, per http://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode
## For more Unicode code points, see http://kourge.net/projects/regexp-unicode-block

<script>
	"use strict";

	function updateDisplayURLTitle(){

		// get the title entered by the user
		var plainTitle = dojo.byId("$cusFieldVarName");  

		if(plainTitle == undefined || plainTitle.value.length ==0){
			dojo.byId("displayURLTitle").innerHTML = "";
			dojo.byId("urlTitle").value="";
		}else{
			// make a friendly url
			var urlTitle = plainTitle.value.replace(/\s+/g,"-"); // Replace whitespace with hyphens
			urlTitle = urlTitle.replace(/-{2,}/g,"-"); // Replace sequences of hyphens with a single hyphen
			urlTitle = urlTitle.toLowerCase(); // This line is optional.  It does respect locale which is nice:  the lowercase version of "I", for instance, is different in English and Turkish

			#if($UtilMethods.isSet($currentAlphabet))
				urlTitle = urlTitle.replace(/[^${currentAlphabet}0-9-]/g , ""); // Strip out everything other than letters in the current alphabet, digits, and hyphens
				urlTitle = urlTitle.replace(/^-|-$/g,""); // Eliminate leading and trailing hyphens AFTER stripping
			#else
				urlTitle = urlTitle.replace(/^-|-$/g,""); // Eliminate leading and trailing hyphens BEFORE encoding
				urlTitle = encodeURIComponent(urlTitle); // If no alphabet is specified then encode reserved ASCII characters and all non-ASCII characters
			#end

			// set the values of the display place holder and the custom field
			dojo.byId("displayURLTitle").innerHTML = urlTitle;
			dojo.byId("urlTitle").value=urlTitle;
		}
	}

	// attach this the text1 field onchange
	dojo.addOnLoad(function(){
		dojo.connect(dojo.byId("$cusFieldVarName"), "onchange", null, "updateDisplayURLTitle");
	});
	// populate the field on load
	dojo.addOnLoad(updateDisplayURLTitle);

</script>
<div id="displayURLTitle" style="height:20px"> </div>