Language and Text Direction in WP 2.1

WordPress 2.1 introduces a new template function called language_attributes(). This function can be used to add lang, xml:lang and dir attributes to the html tag of your theme.

<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

If your locale is currently set to “en”, the result looks like this:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

If you are using a right-to-left language like Farsi, the output looks like this:

<html xmlns="http://www.w3.org/1999/xhtml" dir="rtl" lang="fa" xml:lang="fa">

To get the language and direction individually, use get_bloginfo().


$direction = get_bloginfo('text_direction');
$language = get_bloginfo('language');

language_attributes() is in 2.1 only, so adding it to your theme will make it incompatible with 2.0.x unless you take special precautions. Once 2.1 is out, this might be backported to 2.0.x.

RTL support in WordPress 2.1

Thanks to Sewar, Mani, and others, WordPress 2.1 will have better support for RTL languages. The mechanics of this support are quite simple. Currently, WordPress gets the current locale (let’s use “fa” as our example locale) and then looks in wp-content/languages/ for a file called fa.mo. fa.mo contains all of the Farsi string translations. WordPress 2.1 will also load fa.php after loading fa.mo. fa.php can contain any locale specific code the translator sees fit to include. Support for the Jalāli calendar could be included in this fashion, for example. Further, the global variable $text_direction can be set inside of fa.php. Setting it to ‘rtl’ will tell WordPress that the current locale is an RTL locale. For an RTL locale, WordPress will load wp-admin/rtl.css after loading wp-admin/wp-admin.css when displaying an admin page. When viewing a blog page, WordPress will look in the current theme for an rtl.css file and load it, if present, after loading the theme’s style.css. Themes can now build in RTL support by including an rtl.css alongside the usual style.css.

Maintainers of RTL languages are encouraged to include a locale specific php file that defines $text_direction and includes any other bits needed for a more complete localization.  Theme maintainers are encouraged to provide rtl.css files for their themes.