Click here to Skip to main content
12,301,965 members (54,625 online)
Click here to Skip to main content

Stats

28.5K views
257 downloads
10 bookmarked
Posted

Silverlight 1.1 Hebrew and Arabic Language Support

, 31 Jan 2008 Ms-PL
An article presenting Silverlight 1.1 Hebrew and Arabic language support
using System;
using System.Collections.Generic;
using NBidi;

namespace NBiDi.Extensions
{
    public static class NBiDiExtensions
    {
        public static bool IsTextNeedAlignToRight(string TextToCheckIfNeedsRTL)
        {
            for (int i = 0; i < TextToCheckIfNeedsRTL.Length; i++)
            {
                if (IsRTLChar(TextToCheckIfNeedsRTL[i]))
                    return true;
                if (IsLTRChar(TextToCheckIfNeedsRTL[i]))
                    return false;
            }
            return false;
        }

        public static bool IsRTLChar(char charToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR =
                       BidiCharacterTypeResolver.GetBidiCharacterType(charToCheck);
            return (IsBidiCharTypeRTL(curBidiCharTypeToCheckIfRTLorLTR));
        }


        public static bool IsLTRChar(char charToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR =
                       BidiCharacterTypeResolver.GetBidiCharacterType(charToCheck);
            return (IsBidiCharTypeLTR(curBidiCharTypeToCheckIfRTLorLTR));
        }


        private static bool IsBidiCharTypeLTR(BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR)
        {
            return (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.L)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.LRE)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.LRO);
        }

        private static bool IsBidiCharTypeRTL(BidiCharacterType curBidiCharTypeToCheckIfRTLorLTR)
        {
            return (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.R)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.RLE)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.AL)
                                || (curBidiCharTypeToCheckIfRTLorLTR == BidiCharacterType.RLO);
        }

        public static bool IsSpacerCharcter(char charcterToCheck)
        {

            return (IsWhitespace(charcterToCheck)
                || IsPunctionChar(charcterToCheck));
        }

        public static bool IsPunctionChar(char charcterToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfWhitespace =
    BidiCharacterTypeResolver.GetBidiCharacterType(charcterToCheck);
            return (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.CS);
            //    return Char.IsPunctuation(charcterToCheck);
        }

        public static bool IsWhitespace(char charcterToCheck)
        {
            BidiCharacterType curBidiCharTypeToCheckIfWhitespace =
                BidiCharacterTypeResolver.GetBidiCharacterType(charcterToCheck);
            return ((curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.WS)
                || (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.B)
                || (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.S)
                || (curBidiCharTypeToCheckIfWhitespace == BidiCharacterType.ON))
            ;
        }

        public static bool IsNumberCharcter(char charToCheck)
        {
            return Char.IsDigit(charToCheck);
        }

        public static string[] SplitToParagraphs(string StringToSplit)
        {
            string[] ReturnValues = StringToSplit.Split('\u000A', '\u000D', '\u001C', '\u001E', '\u0085', '\u2029',
                /*Used by blend for breaklines */ '\u2028');

            return ReturnValues;
        }

        public static char GetLTRorRTLStarterCharcterForLine(bool RTL)
        {
            if (RTL)
                return RTLFirstCharMark;
            else
                return LTRFirstCharMark;
        }

        public static char LTRFirstCharMark
        {
            get
            {
                return NBidi.BidiChars.LRE;
            }
        }

        public static char RTLFirstCharMark
        {
            get
            {
                return NBidi.BidiChars.RLE;
            }
        }

        public static bool? GetLTRorRTLStarterCharcterForLineFromLastCharToFirst(string TextToCheckIfNeedsRTL)
        {
            for (int i = TextToCheckIfNeedsRTL.Length - 1; i >= 0; i--)
            {
                if (IsRTLChar(TextToCheckIfNeedsRTL[i]))
                    return true;
                if (IsLTRChar(TextToCheckIfNeedsRTL[i]))
                    return false;
            }
            return null;
        }

        public static string ReverseStringByWhiteSpaceForNumbersOnly(string stringToReverseByWhitespaces)
        {
            char[] whiteSpaceChars =
                new char[] {'\u000C', '\u0020', '\u1680', '\u180E', '\u2000', '\u200A', '\u2028', '\u205F', '\u3000'};

            List<string> seperatedWords = new List<string>(stringToReverseByWhitespaces.Split(whiteSpaceChars));

            seperatedWords.Reverse();

            return String.Join("\u000C", seperatedWords.ToArray());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Justin-Josef Angel [MVP]
JustinAngel.Net, Senior .Net consultant
Israel Israel
Justin-Josef Angel is a C# Microsoft Most Valuable professional, a Senior .Net consultant in Israel with 4 years of .Net experience and has 8 years of Web experience.

Justin's been working this past year on two Enterprise sized Silverlight projects with his customers. During that time he's gained a real-insight into Silverlight's inner workings and how to integrate Silverlight into the real world of software development. Additionally, During that time he's developed a few well-known projects like the "Silverlight 1.0 Javascript Intellisense", "Silverlight 1.1 Hebrew & Arabic Languages support" and a few others you might know.

Justin is also a seasoned presenter with an impressive track-record of talking in front of thousands of people in Israel.

Justin owns the first .Net blog written in Hebrew - http://www.JustinAngel.Net .
And he also owns an additional blog with mixed Hebrew & English content - http://blogs.Microsoft.co.il/blogs/JustinAngel.

A full list of his articles (all 100+ of them) can be found at: http://www.JustinAngel.Net/#index




You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 31 Jan 2008
Article Copyright 2008 by Justin-Josef Angel [MVP]
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid