Identifying RTL language in Android

前端 未结 16 1748
既然无缘
既然无缘 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:24

    Thanks to all.

    If you look at the code of LayoutUtil.getLayoutDirectionFromLocale() (and, I assume Confuiguration.getLayoutDirection() as well), it ends up with analysing the starting letter of locale display name, using Character.getDirectionality.

    Since Character.getDirectionality was around from Android 1, the following code will be compatible with all Android releases (even those, not supporting RTL correctly :)):

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

提交回复
热议问题