Monday, November 1, 2010

Detecting if your Locale\Culture is RightToLeft

I got several questions about how to know if the locale\culture is right to left. Arabic is not the only RTL language, there is Farsi, Urdu, Dari, some Chinese and Indian scripts, to name some. There are several techniques but after many trials these are my best picks.

In managed code there is a property TextInfo.IsRightToLeft that indicates the direction of each culture. This is a simple C# code snippet to detect if the culture is RightToLeft.

using System.Globalization;



static bool IslanguageRTL()

{

return CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;



}

In unmanaged code, you need to dig deeper in the locale structure to see if it RTL or not. This is a simple C++ code snippet:

#include







bool IsLanguageRTL( void )

{

LOCALESIGNATURE localesig;

LANGID languageID=GetUserDefaultUILanguage();

if (GetLocaleInfoW(languageID,LOCALE_FONTSIGNATURE,(LPWSTR)&localesig,

(sizeof(localesig)/sizeof(wchar_t))) && (localesig.lsUsb[3]&0x08000000))

return true;

return false;

}

I hope this helps!

1 comment:

  1. Thank you for sharing code! I just tried it and it worked great for me. You saved my time. Kudos to you!
    e signatures

    ReplyDelete