Identifying RTL language in Android

不问归期 提交于 2019-11-26 08:10:13

问题


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 and LTR, I assume, there should be a way, at least from API 17.


回答1:


Get it from Configuration.getLayoutDirection():

Configuration config = getResources().getConfiguration();
if(config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
    //in Right To Left layout
}



回答2:


@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!




回答3:


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
}



回答4:


You can use TextUtilsCompat from the support library.

TextUtilsCompat.getLayoutDirectionFromLocale(locale)




回答5:


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.




回答6:


I gathered many information and finally made my own, hopefully complete, RTLUtils class.

It allows to know if a given Locale or View is 'RTL' :-)

package com.elementique.shared.lang;

import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import android.support.v4.view.ViewCompat;
import android.view.View;

public class RTLUtils
{

    private static final Set<String> RTL;

    static
    {
        Set<String> lang = new HashSet<String>();
        lang.add("ar"); // Arabic
        lang.add("dv"); // Divehi
        lang.add("fa"); // Persian (Farsi)
        lang.add("ha"); // Hausa
        lang.add("he"); // Hebrew
        lang.add("iw"); // Hebrew (old code)
        lang.add("ji"); // Yiddish (old code)
        lang.add("ps"); // Pashto, Pushto
        lang.add("ur"); // Urdu
        lang.add("yi"); // Yiddish
        RTL = Collections.unmodifiableSet(lang);
    }

    public static boolean isRTL(Locale locale)
    {
        if(locale == null)
            return false;

        // Character.getDirectionality(locale.getDisplayName().charAt(0))
        // can lead to NPE (Java 7 bug)
        // https://bugs.openjdk.java.net/browse/JDK-6992272?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab
        // using hard coded list of locale instead
        return RTL.contains(locale.getLanguage());
    }

    public static boolean isRTL(View view)
    {
        if(view == null)
            return false;

        // config.getLayoutDirection() only available since 4.2
        // -> using ViewCompat instead (from Android support library)
        if (ViewCompat.getLayoutDirection(view) == View.LAYOUT_DIRECTION_RTL)
        {
            return true;
        }
        return false;
    }
}



回答7:


You can check like this if you want to check for API lower than 17

boolean isRightToLeft = TextUtilsCompat.getLayoutDirectionFromLocale(Locale
               .getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;

OR for API 17 or above

boolean isRightToLeft = TextUtils.getLayoutDirectionFromLocale(Locale
               .getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;



回答8:


For more precise control over your app UI in both LTR and RTL mode, Android 4.2 includes the following new APIs to help manage View components:

android:layoutDirection — attribute for setting the direction of a component's layout.
android:textDirection — attribute for setting the direction of a component's text.
android:textAlignment — attribute for setting the alignment of a component's text.
getLayoutDirectionFromLocale() — method for getting the Locale-specified direction

Thus getLayoutDirectionFromLocale() should help you out. Refer the sample code here : https://android.googlesource.com/platform/frameworks/base.git/+/3fb824bae3322252a68c1cf8537280a5d2bd356d/core/tests/coretests/src/android/util/LocaleUtilTest.java




回答9:


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; 
}



回答10:


When building library you also always need to check if application is supporting RTL by using

(getApplicationInfo().flags &= ApplicationInfo.FLAG_SUPPORTS_RTL) != 0

When application is running on RTL locale, but it isn't declared in manifest android:supportsRtl="true" then it is running in LTR mode.




回答11:


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;
}



回答12:


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.




回答13:


You can detect if a string is RTL/LTR with Bidi. Example:

import java.text.Bidi;

Bidi bidi = new Bidi( title, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT );

if( bidi.isLeftToRight() ) {
   // it's LTR
} else {
   // it's RTL
}



回答14:


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;
            }
    }



回答15:


Because English language devices are supporting RTL, you can use this code in your MainActivity to change device language to english and you don't need to "supportRTL" code.

String languageToLoad  = "en"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());


来源:https://stackoverflow.com/questions/18996183/identifying-rtl-language-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!