Check if string is Arabic - C#

I have pretty good experience in bilingual web app development, especially in English & Arabic languages. The challenge with switching website page from English to Arabic is not just the content translation, the whole layout need to be changed and the text direction for all need to be RTL where as for English it need to be LTR.

We developed this one app socialboard which aggregates content from social medias and displays in a single place. Initially, we focused only on English content so everything in LTR even though some aggregated content was in Arabic. But then we got some clients whose primary language is Arabic. i.e most of the content of their social media was in Arabic.

So we had to do some changes, UX team prepared the RTL layout for displaying content. The development team had to classify content (i.e to flag them isRTL or not) during creation and use proper layout based on isRTL field value of the content.

To classify, or to check whether the given content language is Arabic or not, we used the following extension method,

/// <summary>
/// To check whether the given string is Arabic.
/// </summary>
/// <param name="input"></param>
/// <returns>Returns True if Arabic</returns>
public static bool IsRtl(this string input)
{
    return Regex.IsMatch(input, @"\p{IsArabic}");
}

And it is now part of my Code.Library package ValidationHelper module.

If you know any better way to check if the string is Arabic in C#, let me know in the comments.


comments powered by Disqus

About the author

  • Abhith

    Abhith

    Abhith Rajan is a full-stack developer, consultant, Sysberries employee, and biker. Who passionate about C# and JavaScript. Works on his blog, his personal finance web app and its react native mobile app in his free time.

    Buy me a coffeeBuy me a coffee