1. 01. Incoming
  2. 02. Hifi
  3. 03. Lofi
  4. 04. The Lab
  5. 05. Tags

frequency decoder

Automatically detect RTL text

Posted Friday December 12, 2008

frequency decoder

A script that can automatically set the correct direction for RTL based character sets

One of my coworkers has shown me a very nice little U.I. feature that the youTube (account) interface provides.

If you start to type Arabic text into a textarea or text input, the form element’s direction attribute automatically gets set to “rtl” (right to left), the correct direction for Arabic text. Should you type in standard Latin text, the direction gets set to “ltr” (left to right); again, the correct direction for Latin text.

Looking through the Google i18n.bidi Javascript code, we can see it’s all done by attaching the keydown/keyup events to a small Javascript function that uses two very clever regular expressions to test if the form element’s value contains RTL or LTR characters.

As I’m currently writing an Enterprise CMS that has to deal with multiple languages, RTL languages included; I thought it would make a nice addition to the CMS functionality and so hacked together a quick script that uses the aforementioned “very clever” regular expressions to set the direction attribute of either text inputs or textareas depending on their content.

Let me just be crystal clear here: it’s the google developers who should get all the credit for this script as it was their idea and it’s their regular expressions used within my derivative!

Anyway, you can check the demo to get a better idea of exactly what I’m talking about. I’ve included some Arabic text within the page to get you started.

Update: Perhaps I wasn’t clear enough within the article (as I’ve already had a few emails asking for help with the code) – I’m not actually saying you should use my code (which is why there’s no download link), if your working with RTL text, do yourself a favour and use the Google i18n.bidi code instead.

Tags: google, i18n, javascript, rtl

Previous Comments ~

Alex
#1 · Thursday April 16, 2009

Thnx for solution.

A penny for your thoughts…

Remember: Off-topic or dumb-ass comments will, of course, be deleted. Spammers shall have the scary flying-monkeys from “The Wizard of Oz” dispatched to their abode. Please remember to RTFM before asking questions pertaining to any of the Lab experiments.

Reporting a bug? The error message and browser and operating system details would be much appreciated. A URL would be even better. Thanks.

Posting a code block? Please use the Pastebin service to stop textile from mangling your nicely formatted code.

Popular Frequencies

  • Unobtrusive Table Sort Script (revi…
    Saturday September 16, 2006
  • Unobtrusive Date-Picker Widget Upda…
    Monday October 02, 2006
  • Unobtrusive Date-Picker Widget V4
    Tuesday February 03, 2009
  • Unobtrusive Table Sort Script
    Friday November 18, 2005
  • Unobtrusive Date-Picker Widgit
    Friday October 14, 2005

Google Ads

All articles