How to get the current locale text direction in Flutter using Intl

4,345

Solution 1

Thanks to @chunhunghan I created a static method, this method takes the context and returns true based on the current locale of the app, because if you do not pass language code the function always returns false.

  import 'package:intl/intl.dart' as intl;
  static bool isDirectionRTL(BuildContext context){
   return intl.Bidi.isRtlLanguage( Localizations.localeOf(context).languageCode);
  }

Solution 2

Directionality.of(context) == TextDirection.rtl

Solution 3

You can directly use intl.Bidi.isRtlLanguage() with import 'package:intl/intl.dart' as intl;
inside this function if you do not pass language code such as en or ar, it will use Intl.getCurrentLocale()

code snippet from bidi_util.dart

 static bool isRtlLanguage([String languageString]) {
    var language = languageString ?? Intl.getCurrentLocale();
    if (_lastLocaleCheckedForRtl != language) {
      _lastLocaleCheckedForRtl = language;
      _lastRtlCheck = _rtlLocaleRegex.hasMatch(language);
    }
    return _lastRtlCheck;
  }
Share:
4,345
Shady Mohamed Sherif
Author by

Shady Mohamed Sherif

A software engineer with over 12 years' experience involving software development practices, analysis, design and implementation of enterprise mobile and web applications using open source technologies. A critical thinker with analytical, design and problem-solving capabilities; which allows me to be a problem finder and brainstorming facilitator for technical and business issues. Excellent communication skills and able to work in requirement gathering and analysis to define and refine new functionality

Updated on December 17, 2022

Comments

  • Shady Mohamed Sherif
    Shady Mohamed Sherif over 1 year

    I'm creating a new flutter UI component that contains selection and getting more info about the product.

    enter image description here

    I want this component to support RTL also, So I need to get the current locale language direction which will allow me to know which corners of the selection shape will be rounded.

    The LTR shape code is like this

    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.only(
        bottomLeft: Radius.circular(35),
        topLeft: Radius.circular(35),
        ),
      )
    

    The RTL shape code will be like

    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.only(
        bottomRight: Radius.circular(35),
        topRight: Radius.circular(35),
        ),
      )
    

    I know that intl provides functionality to get the direction of specific text while I want to get the default direction of the current select locale, So if the current locale is Arabic, Farsi or any other right to left language I will return the RLT component. I don't know exactly how to do it.

  • Shady Mohamed Sherif
    Shady Mohamed Sherif over 4 years
    Thanks for your help, It worked with some edits, I will write the full answer
  • shield
    shield over 3 years
    You need import dart:ui. In case of conflict with bidi_utils, see stackoverflow.com/questions/49647795/….
  • Eslam Sameh Ahmed
    Eslam Sameh Ahmed about 2 years
    It searches nearest Directionality widget and return its text direction.