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.

13 comments to Language and Text Direction in WP 2.1

  1. Matt says:

    Nice. For those of us using a blog that is in a language that reads right to left, I could see this being very useful.

    Also very useful in a way of bringing WordPress even further beyond the rest of the blogging systems out there.

  2. Ajay says:

    Great edition Ryan,

    Any suggestions on how to differentiate between 2.1 and 2.0.x in order to ensure the theme works for both versions?

  3. [...] de las mejoras que esta nueva versión nos va a ofrecer van a ser unas nuevas funciones para templates que nos permitirán mejorar la semántica de nuestro theme. Como el tema de los lenguajes es algo [...]

  4. I probably wont personally use it, but it’s a great leap forward in terms of accessibility that the team should be proud of. It might encourage some bloggers in other languages to use wordpress, too.

  5. [...] I caught Ryan Boren’s post about a new theme function called language_attributes(), which adds xml:lang and dir= (if not [...]

  6. Aziz Ashofte says:

    I do agree with Matt. this will put WordPress far beyond not only the current blogging systems but also all the web applications which simply ignored right to left languages and localization in general.
    Localization is not simply translating the phrases and I am really delighted to see wordpress is proving it in action. Thanks to Ryan, Matt and Mani.

  7. Gonahkar says:

    this is NOT “Farsi language”, this is “Persian language” ;)

  8. Sarit says:

    I’m glad you’ve finally taken steps to help RTL language bloggers.
    The Hebrew WordPress community is still growing slowly with our own adaptions.
    I would however, like to try this version and the new RTL features. I’m not sure how much impact this has, because directionality is not enough; themes need to be carefully fitted to RTL, with additions such as {text-align:right;) etc.
    What else does this affect?

  9. Sarit says:

    Also, I think a better thing to do, would be to set that if the directionality is RTL, WordPress should load “stylertl.css” instead the normal packages. That’s in case you would like to display the RTL abilities with English, international WordPress installation out of the box.
    I hope you’re catching my drift.
    I’ve seen it working wonderfully on LifeType and I’m sure it can be done on WP.

  10. Ryan, are those get_bloginfo() arguments new ones for 2.1? I’m just wondering what I can do to improve the situation for blogs running (and still running, once 2.1 comes out) 2.0.x that improves on just leaving my theme with xml:lang="en" lang="en" in as the default.

  11. [...] better support in the languages you use your blog in. Ryan Boren describes how this is done at his blog. Since I do a lot of business with Chinese and Americans, it will be nice to know how characters [...]

  12. Martin Hipp says:

    Ajay use:

    if ( function_exists(‘language_attributes’) ) { language_attributes(); }

  13. [...] Found in header.php Add lang, xml:lang and dir attributes to the html tag. For more info, see Language and Text Direction in WP 2.1 By TawnyGirl Feedbacks on this entry via RSS 2.0 Please leave a Comment or discuss via [...]