I'm in the middle of a project to localize some 14,000 flat html pages of UK English to US English. Most of the content is OK as it is, so in order to get the site up in the US as quickly as possible we've decided to do a jquery/javascript find and replace on these pages at runtime.
Here is the code that I have so far:
//us-local.js
function usLocalize() {
$.each(usEnglishMap, function(ukEnglish, usEnglish) {
$("#layer-content *").each(function () {
if ($(this).children().length == 0) {
var regExp = new RegExp("\\b"+ukEnglish+"\\b", "g");
$(this).text($(this).text().replace(regExp, usEnglish));
}
});
});
}
var usEnglishMap = {
'aeroplane' : 'airplane',
'aluminium' : 'aluminum',
'analyse' : 'analyze',
'appertaining' : 'pertaining',
'artefacts' : 'artifacts',
'behaviour' : 'behavior',
'centimetre' : 'centimeter',
'centre' : 'center',
'coat-hanger' : 'coat hanger',
'colour' : 'color',
'defence' : 'defense',
'dustbin' : 'trash can',
'favourite' : 'favorite',
'forwards' : 'forward',
'jewellery' : 'jewelry',
'kilometre' : 'kilometer',
'landslip' : 'landslide',
'maths' : 'math',
'metre' : 'meter',
'mould' : 'mold',
'mum' : 'mom',
'programme' : 'program',
'practise' : 'practice',
'rubbish' : 'garbage',
'spellings' : 'spelling',
'tonnes' : 'tones',
'torch' : 'flashlight',
'travelled' : 'traveled',
'traveller' : 'traveler',
'tyres' : 'tires',
};
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Localization Find and Replace Proof of Concept</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="us-local.js"></script> </head> <body onload='usLocalize()'> <div id='layer-content' style='display:none'> <p>aeroplane, aluminium, analyse, appertaining, artefacts, behaviour, centimetre, centre, coat-hanger, colour, defence, favourite, fowards, jewellery, kilometre, metre, mould, programme, tonnes, travelled, traveller, tyres</p> <p>mould, mould, kilometre, kilometre, metre, metre, mould, programme, tonnes, travelled, traveller, tyres, aeroplane, aluminium, analyse, appertaining, artefacts, behaviour, centimetre, centre, coat-hanger, colour, defence, favourite, fowards, jewellery</p> <p> <ul> <li>dustbin</li> <li>landslip</li> <li>mum</li> <li>rubbish</li> <li>torch</li> </ul> </p> <p><a href='#'>maths</a>, <a href='#'>spellings</a> </p> <p>aeroplane, aluminium <a href="#" class="test" title="this is my title">kilometre, metre</a></p> </div> </body> </html>
So far it has been working great, except for the last test case on the page. I cannot get it to work on the last paragraph. I could just delete the part that says if ($(this).children().length == 0) and every word would replace correctly, but then I would loose the html formatting (which is kind of important). So far I'm only worried about lower-case letters. I'll worry about other cases later.
Any help from the DIC community would be appreciated! Thanks.

New Topic/Question
Reply



MultiQuote


|