Created
February 10, 2013 05:26
-
-
Save spacehunter/4748507 to your computer and use it in GitHub Desktop.
Typically an English sentence translated to Spanish has more words for the translation. Therefore text may not align properly on a web page for a particular language. The function below will position the element based on language selection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Use Case | |
<div class="bullet" | |
data-0="margin-top:-475px;margin-left:?es?(-425px, -525px);transform-origin:50% 50%;"> | |
<span data-lang="Gane <b>Ingreso Mensual de Energía</b> (MEI)">Earn <b>Monthly Energy Income</b> (MEI)</span> | |
</div> | |
*/ | |
var val, | |
elementIndex, | |
elements = document.getElementsByTagName('*'), | |
esRegExp = /\?es\?\(([^\)]+)\)/, | |
dataRegExp = /data-(_start|[0-9]|-[0-9])*/, | |
espanol = (this.getStorageData('bp-spanish') === true) ? 1 : 0; | |
for(elementIndex = 0; elementIndex < elements.length; elementIndex++) { | |
var el = elements[elementIndex]; | |
/* skip elements with no attributes */ | |
if(!el.attributes) continue; | |
// iterate over all attributes - search for 'data-xxxx' | |
for (var attributeIndex = 0; attributeIndex < el.attributes.length; attributeIndex++) { | |
var attr = el.attributes[attributeIndex], | |
data = attr.name.match(dataRegExp) | |
? el.getAttribute(attr.name.match(dataRegExp)[0]) | |
: null; | |
/* we have a match, now search for '?es?(xxx, xxx)' value */ | |
if (data){ | |
var esMatch = data.match(esRegExp); | |
if (esMatch){ | |
/* replace the attribute with the proper (EN/ES) value */ | |
val = esMatch[1].split(',')[espanol]; | |
el.setAttribute(attr.name, data.replace(esRegExp, val)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment