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.


Attachment: url-title.vtl (3 KB)



${esc.hash}set(${esc.dollar}currentAlphabet = "") ${esc.hash}${esc.hash} No default alphabet:  just URLencode everything
${esc.hash}${esc.hash}set(${esc.dollar}currentAlphabet = "a-zA-Z") ${esc.hash}${esc.hash} English alphabet
${esc.hash}${esc.hash}set(${esc.dollar}currentAlphabet = "[${esc.b}u0400-${esc.b}u052F]") ${esc.hash}${esc.hash} Cyrillic alphabet and supplement, per
${esc.hash}${esc.hash} For more Unicode code points, see

	"use strict";

	function updateDisplayURLTitle(){

		// get the title entered by the user
		var plainTitle = dojo.byId("${esc.dollar}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(/[^${esc.dollar}{currentAlphabet}0-9-]/g , ""); // Strip out everything other than letters in the current alphabet, digits, and hyphens
				urlTitle = urlTitle.replace(/^-|-${esc.dollar}/g,""); // Eliminate leading and trailing hyphens AFTER stripping
				urlTitle = urlTitle.replace(/^-|-${esc.dollar}/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("${esc.dollar}cusFieldVarName"), "onchange", null, "updateDisplayURLTitle");
	// populate the field on load

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