Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

wpf - Richtextbox change the flow direction when the language change( AR/ENG) C#

I have a richtextbox and I would like to change dynamically the flow direction of the text when I click on alt+shift to change from the Arabic language to the English language ( the flow direction should be changed from RightToLeft to LefttoRight ).

Is this possible using .Net libraries?

  private void RichText_TextChanged(object sender, TextChangedEventArgs e)
    {
        var factory = new RankedLanguageIdentifierFactory();
        try
        {
            var identifier = factory.Load("Core14.profile.xml");
            // can be an absolute or relative path. Beware of 260 chars limitation of the path length in Windows. Linux allows 4096 chars.
            string text = new TextRange(RichText.Document.ContentStart, RichText.Document.ContentEnd).Text;
            var languages = identifier.Identify(text);
            var mostCertainLanguage = languages.FirstOrDefault();
            if (mostCertainLanguage != null)
                if ((mostCertainLanguage.Item1.Iso639_3.Equals("eng")) || (mostCertainLanguage.Item1.Iso639_3.Equals("fra")))
                {
                    RichText.FlowDirection = FlowDirection.LeftToRight;
                }
                else if (mostCertainLanguage.Item1.Iso639_3.Equals("dan"))
                {
                    RichText.FlowDirection = FlowDirection.RightToLeft;

                }
        }
        catch (Exception es)
        {
            Console.WriteLine("Exception    " + es);
        }
    }

This what I have tried using this Nuget package ntextcat library

question from:https://stackoverflow.com/questions/65830813/richtextbox-change-the-flow-direction-when-the-language-change-ar-eng-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally solved it

 string lang =InputLanguageManager.Current.CurrentInputLanguage.ToString();
            string langtwo = lang.Substring(0, 3);

                if (langtwo.Equals("ar-")) 
                {
                    RichText.FlowDirection = FlowDirection.RightToLeft;
                }
    
                else
                {
                    RichText.FlowDirection = FlowDirection.LeftToRight;
                }

I followed this documentation:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...