Internationalized URL Title code


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.




#set($currentAlphabet = "") ## No default alphabet:  just URLencode everything
##set($currentAlphabet = "a-zA-Z") ## English alphabet
##set($currentAlphabet = "[\u0400-\u052F]") ## Cyrillic alphabet and supplement, per
## For more Unicode code points, see

	"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 = "";
			// 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

				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
				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

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

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

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