How do I determine programmatically whether a particular language is LTR or RTL?


Given an LCID, how does one determine whether the language lays out left-to-right or right-to-left? One suggestion was simply to hard-code the list of known right-to-left languages, and if the language isn't on the list, then assume that it is left-to-right. This technique is clearly fragile, because Windows adds support for new languages not infrequently, and if one of those is a right-to-left language, then your table is now out of date. And besides, there are languages whose layout is neither left-to-right nor right-to-left. For example, Chinese and Japanese traditionally lay out top-to-bottom.

To obtain the text layout direction programmatically, call Get­Locale­Info­Ex with LOCALE_IREADINGLAYOUT.

And just hope that nobody ever hands you a boustrophedonic language.

Comments (17)
  1. Ochs von Lechernau says:

    Perhaps you could extend GetLocaleInfoEx with a parameter for line parity, then?

  2. I agree, the first technique is clearly fragile.  But MSDN says LOCALE_IREADINGLAYOUT is Windows 7 or later.  So what's the best solution on Windows XP/Vista?

    [Use a hard-coded list and hope that new locale support is not backported. (I don't believe it is, but I'm not the expert.) -Raymond]
  3. Joshua Ganes says:

    I am lucky enough (so far) to have never worked on any application requiring anything more than english and french. I can imagine that working with right-to-left languages would cause no end of headaches when trying to provide a reasonable layout. It's nice to know that there is a built-in way to determine what you're working with.

  4. Joshua says:

    Incidentally, Chinese can be laid out either RTL or LTR (the rule says to use natural alignment according to the object that it is laid out on, such as head of bus).

    I've seen worse problems than this one. E.G. We must uppercase w/o changing word length, and word contains ß.

  5. Ivo says:

    Michael Kaplan mentioned a solution long time ago. It was to use LOCALE_FONTSIGNATURE and check for a particular bit in the LOCALESIGNATURE structure. Works at least on Vista, maybe XP too.

  6. alexcohn says:

    @Ivo: msdn.microsoft.com/…/dd374090 says it should work for XP:

    The Unicode subset bitfields (USBs) are used in the FONTSIGNATURE and LOCALESIGNATURE structures

    […]

    bit #123  Windows 2000 and later: Layout progress, horizontal from right to left

    and michkap confirms: blogs.msdn.com/…/8977109.aspx

  7. Andy Janata says:

    Joshua Ganes: I once worked on a project that had a right-to-left version of English for testing. It didn't make making it work any easier, bit it sure helped with debugging.

  8. Joshua says:

    So, it does exist. But square block is not acceptable not font support is still extremely spotty. Too bad.

    @Andy Janata: nice. I can read RTL English words but not whole sentences.

  9. Anonymous Coward says:

    Regarding Japanese writing direction, in literature it traditionally was and still is top-right to bottom, but for technical and scientific works top-left to right is generally employed. This is because the Japanese basically got their taste of modern science from the Dutch, who wrote horizontally.

    This horizontal mode of writing is now recommended by the Ministry of Education. Apart from scientific works you also find it in building inscriptions, signs above store displays, television and other such situations where space restrictions make vertical writing impractical. It is also often but not by any means exclusively in newspaper headlines, though the text tends to be vertical.

    And horizontal writing is also used… in computing, presumably a combined influence from television and the fact that computers came from America and hence used horizontal writing.

  10. Gabe says:

    @AC: I'm not convinced that computers in Japan came from America. My understanding is that computers essentially evolved separately there, as early American computers weren't capable of displaying Japanese. The NEC PC-98 architecture, while still being 8086-based, was completely different from the IBM PC architecture. It had a 640×400 display in 1982!

    Since the machine was essentially designed from scratch in Japan, there's probably little reason that it couldn't have had top-down writing being the standard.

  11. Schmulik Raskin says:

    Do the .NET globalization classes provide this information? If yes, where would it be found?

  12. Anon says:

    Hello,

    static readonly Regex MatchArabicHebrew = new Regex(@"[u0600-u06FF,u0590-u05FF]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);

    public static bool IsRtl(string data)

    {

     if (string.IsNullOrEmpty(data)) return false;

     return MatchArabicHebrew.IsMatch(data);

    }

    [That's a different question from whether a language is RTL. And it also says that the string "Hello, my name is أشرف ماهر, but you can call me Bob." is RTL, when it is clearly LTR (just with embedded RTL content). It's also not clear why you need the +. You're creating more work for the regular expression engine for no apparent benefit. -Raymond]
  13. anonymous says:

    And here I thought you were going to talk about Lun-time ribraries…

  14. James says:

    I wonder does windows even support a boustrophedonic language ?

    "Chinese and Japanese traditionally lay out top-to-bottom"

    Chinese do top-to-bottom and right-to-left.

    When i think about i've never seen windows do top-to-bottom text either do windows support it ?

  15. alexcohn says:

    @James, for top-to-bottom locale, look at bit 124 in the same structure.

  16. yuhong2 says:

    "The NEC PC-98 architecture, while still being 8086-based, was completely different from the IBM PC architecture. It had a 640×400 display in 1982!"

    Yea, I wondered for a while why EGA dating back to 1984 used a 640×350 instead of 640×400 resolution exactly because of this.

Comments are closed.