If you want to detect the language orientation of text using JavaScript/jQuery, you can utilize regular expressions. However, keep in mind that JavaScript's built-in regular expressions have limitations, so incorporating the XRegExp library with full Unicode support is recommended. By utilizing expressions like \p{Hebrew}, you can identify which block of Unicodes the characters belong to.
To tackle this issue, I created a function that scans through each character in a string and tallies the occurrences of Hebrew characters (as my website is bilingual in Yiddish/English). Based on the score obtained, an 'rtl' class is added to elements with a high score. To make this approach more generic, you can expand the loop to encompass all RTL languages in Unicode.
Here is a useful link to a sample implementation: http://jsfiddle.net/Swyu4/9/
Make sure to reference the External Resources section linking to the XRegExp libraries within the jsfiddle.
$('p').each(function() {
if(isRTL($(this).text()))
$(this).addClass('rtl');
});
function isRTL(str) {
var isHebrew = XRegExp('[\\p{Hebrew}]');
var isLatin = XRegExp('[\\p{Latin}]');
var partLatin = 0;
var partHebrew = 0;
var rtlIndex = 0;
var isRTL = false;
for(i=0;i<str.length;i++){
if(isLatin.test(str[i]))
partLatin++;
if(isHebrew.test(str[i]))
partHebrew++;
}
rtlIndex = partHebrew/(partLatin + partHebrew);
if(rtlIndex > .5) {
isRTL = true;
}
return isRTL;
}