Identifying RTL language in Android

前端 未结 16 1596
既然无缘
既然无缘 2020-11-28 06:59

Is there a way to identify RTL (right-to-left) language, apart from testing language code against all RTL languages?

Since API 17+ allows several resources for RTL a

相关标签:
16条回答
  • 2020-11-28 07:28

    There's a really simple way to check the layout direction of a view, but it falls back to LTR on pre API 17 devices:

    ViewUtils.isLayoutRtl(View view);
    

    the ViewUtils class comes bundled with the support v7 library, so it should be available already if you're using the appcompat library.

    0 讨论(0)
  • 2020-11-28 07:30

    Just use this code:

     public static boolean isRTL() {
       return isRTL(Locale.getDefault());
     }
    
     public static boolean isRTL(Locale locale) {
      final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
      return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
           directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
     }
    
     if (isRTL()) {
       // The view has RTL layout
     }
     else {
       // The view has LTR layout
     }
    

    This will work for all Android API lavels.

    0 讨论(0)
  • 2020-11-28 07:33

    @cyanide's answer has the right approach but a critical bug.

    Character.getDirectionality returns the Bi-directional (bidi) character type. Left-to-right text is a predictable type L and right-to-left is also predictably type R. BUT, Arabic text returns another type, type AL.

    I added a check for both type R and type AL and then manually tested every RTL language Android comes with: Hebrew (Israel), Arabic (Egypt), and Arabic (Israel).

    As you can see, this leaves out other right-to-left languages, so I was concerned that as Android adds these languages, there might have a similar issue and one might not notice right away.

    So I tested manually each RTL language.

    • Arabic (العربية) = type AL
    • Kurdish (کوردی) = type AL
    • Farsi (فارسی) = type AL
    • Urdu (اردو) = type AL
    • Hebrew (עברית) = type R
    • Yiddish (ייִדיש) = type R

    So it looks like this should work great:

    public static boolean isRTL() {
        return isRTL(Locale.getDefault());
    }
    
    public static boolean isRTL(Locale locale) {
        final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
        return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
               directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
    }
    

    Thanks @cyanide for sending me the right direction!

    0 讨论(0)
  • 2020-11-28 07:33

    This will work in all SDKS:

    private boolean isRTL() {
        Locale defLocale = Locale.getDefault();
        return  Character.getDirectionality(defLocale.getDisplayName(defLocale).charAt(0)) == Character.DIRECTIONALITY_RIGHT_TO_LEFT;
    }
    
    0 讨论(0)
  • 2020-11-28 07:36

    If you're using the support library, you can do the following:

    if (ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL) {
        // The view has RTL layout
    } else {
        // The view has LTR layout
    }
    
    0 讨论(0)
  • 2020-11-28 07:36

    Native RTL support in Android 4.2

        public static ComponentOrientation getOrientation(Locale locale) 
        {
                // A more flexible implementation would consult a ResourceBundle
                // to find the appropriate orientation.  Until pluggable locales
                // are introduced however, the flexiblity isn't really needed.
                // So we choose efficiency instead.
                String lang = locale.getLanguage();
                if( "iw".equals(lang) || "ar".equals(lang)
                    || "fa".equals(lang) || "ur".equals(lang) )
                {
                    return RIGHT_TO_LEFT;
                } else {
                    return LEFT_TO_RIGHT;
                }
        }
    
    0 讨论(0)
提交回复
热议问题