Obtener el idioma actual en el dispositivo


¿Cómo podemos obtener el idioma actual seleccionado en el dispositivo Android?

Author: Idolon, 2010-11-18

20 answers

Si desea obtener el idioma seleccionado de su dispositivo, esto podría ayudarlo:

Locale.getDefault().getDisplayLanguage();
 743
Author: DeRagan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-23 17:14:59

He comprobado los métodos de configuración regional en mi dispositivo Android 4.1.2, y los resultados:

Locale.getDefault().getLanguage()       ---> en      
Locale.getDefault().getISO3Language()   ---> eng 
Locale.getDefault().getCountry()        ---> US 
Locale.getDefault().getISO3Country()    ---> USA 
Locale.getDefault().getDisplayCountry() ---> United States 
Locale.getDefault().getDisplayName()    ---> English (United States) 
Locale.getDefault().toString()          ---> en_US
Locale.getDefault().getDisplayLanguage()---> English
 644
Author: trante,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-30 09:38:38

Lo que funcionó para mí fue:

Resources.getSystem().getConfiguration().locale;

Recurso.getSystem () devuelve un objeto global shared Resources que proporciona acceso solo a los recursos del sistema (sin recursos de la aplicación) y no está configurado para la pantalla actual (no puede usar unidades de dimensión, no cambia según la orientación, etc.).

Dado que getConfiguration.locale ha quedado obsoleto, la forma preferida de obtener la configuración regional principal en Android Nougat es:

Resources.getSystem().getConfiguration().getLocales().get(0);

Para garantizar la compatibilidad con las versiones anteriores de Android una posible solución sería una simple comprobación:

Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
    //noinspection deprecation
    locale = Resources.getSystem().getConfiguration().locale;
}

Actualización

Comenzando con la biblioteca de soporte 26.1.0 no es necesario comprobar la versión de Android, ya que ofrece un método conveniente compatible con versiones anteriores getLocales().

Simplemente llame:

ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
 67
Author: Sarpe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-12 17:35:39

Puede 'extraer' el idioma de la configuración regional actual. Puede extraer la configuración regional a través de la API estándar de Java o mediante el contexto de Android. Por ejemplo, las dos líneas siguientes son equivalentes:

String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
 43
Author: Johan Pelgrim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-18 07:51:32

Para ahorrar tiempo y/o confusión a los demás, quería compartir que he probado las dos alternativas propuestas por Johan Pelgrim anteriormente y en mi dispositivo son equivalentes, ya sea que se cambie o no la ubicación predeterminada.

Así que la configuración predeterminada de mi dispositivo es Inglés(Reino Unido) y en este estado, como se esperaba, ambas cadenas en la respuesta de Johan dan el mismo resultado. Si luego cambio la configuración regional en la configuración del teléfono(digamos italiano (Italia)) y vuelvo a ejecutar, entonces ambas cadenas en la respuesta de Johan indique la localización como italiano (Italia).

Por lo tanto, creo que el post original de Johan es correcto y el comentario de gregm es incorrecto.

 34
Author: airewyre,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-20 06:36:54

Como se describe en Referencia regional la mejor manera de obtener el idioma es:

Locale.getDefault().getLanguage()

Este método devuelve cadena con id de idioma de acuerdo con ISO 639-1 standart

 16
Author: Opsenas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-12 07:41:22

Puedes usar esto

boolean isLang = Locale.getDefault().getLanguage().equals("xx");

Cuando "xx" es cualquier código de lenguaje como "en", "fr", "sp", "ar".... y así sucesivamente

 13
Author: Simon K. Gerges,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-15 16:24:33

Para añadir a La respuesta de Johan Pelgrim

context.getResources().getConfiguration().locale
Locale.getDefault()

Son equivalentes porque android.text.format.DateFormat la clase usa ambos indistintamente, por ejemplo,

private static String zeroPad(int inValue, int inMinDigits) {
    return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}

Y

public static boolean is24HourFormat(Context context) {
    String value = Settings.System.getString(context.getContentResolver(),
            Settings.System.TIME_12_24);

    if (value == null) {
        Locale locale = context.getResources().getConfiguration().locale;

    // ... snip the rest ...
}
 6
Author: armandino,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:18:20

Puede intentar obtener la configuración regional de los recursos del sistema:

PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();
 5
Author: stefan.nsk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-20 11:24:45

Hay dos idiomas.

Idioma predeterminado del sistema operativo:

Locale.getDefault().getDisplayLanguage();

Idioma actual de Aplicación:

getResources().getConfiguration().locale.getDisplayLanguage();//return string
 4
Author: ayed abboushi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-18 17:15:38

Puede usar este código para averiguar la corriente del teclado

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
 2
Author: Ramkailash,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-11 05:38:35

Las respuestas anteriores no distinguen entre chino simple y chino tradicional. Locale.getDefault().toString() funciona que devuelve "zh_CN", "zh_TW", "en_US" y etc.

Referencias a: https://developer.android.com/reference/java/util/Locale.html , ISO 639-1 es ANTIGUA.

 2
Author: rensq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-06 14:32:17

Si elige un idioma no puede escribir este griego puede ser útil.

getDisplayLanguage().toString() = English
getLanguage().toString() = en 
getISO3Language().toString() = eng
getDisplayLanguage()) = English
getLanguage() = en
getISO3Language() = eng

Ahora pruébalo con griego

getDisplayLanguage().toString() = Ελληνικά
getLanguage().toString() = el
getISO3Language().toString() = ell
getDisplayLanguage()) = Ελληνικά
getLanguage() = el
getISO3Language() = ell
 2
Author: user462990,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-06 14:33:34
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
    // your code here
}
 1
Author: Pratibha Sarode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-06 14:34:24

La forma correcta de obtener el idioma de su dispositivo es la siguiente:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    return context.getResources().getConfiguration().getLocales().get(0);
} else {
    return context.getResources().getConfiguration().locale;
}

Espero que ayude.

 1
Author: andrea.rinaldi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-11 16:09:50

Si el nivel de API es 24 o superior, use LocaleList.getDefault().get(0).getLanguage() else use Locale.getDefault.getLanguage()

private fun getSystemLocale(): String {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return LocaleList.getDefault()[0].language
    } else {
        return Locale.getDefault().language
    }
}

Referencia: https://developer.android.com/guide/topics/resources/multilingual-support

 1
Author: user7471982,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-25 10:48:02

Mi solución es así

@SuppressWarnings("deprecation")
public String getCurrentLocale2() {
    return Resources.getSystem().getConfiguration().locale.getLanguage();
}

@TargetApi(Build.VERSION_CODES.N)
public Locale getCurrentLocale() {
    getResources();
    return Resources.getSystem().getConfiguration().getLocales().get(0);
}

Y luego

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Log.e("Locale", getCurrentLocale().getLanguage());
            } else {
                Log.e("Locale", getCurrentLocale2().toString());
            }

Mostrado - - - > es

 0
Author: biyoalp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-10 05:06:55
Locale.getDefault().getDisplayLanguage()

Te dará Written nombre para el idioma, por ejemplo, English, Dutch, French

Locale.getDefault().getLanguage()

Te dará language code, por ejemplo: en, nl, fr

Ambos métodos devuelven String

 0
Author: Shamsul Arefin Sajib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-10 10:18:13

Aquí está el código para obtener el país del dispositivo. Compatible con todas las versiones de Android, incluso oreo.

Solución: si el usuario no tiene tarjeta sim que obtener el país que se utiliza durante la configuración del teléfono , o la selección de idioma actual.

public static String getDeviceCountry(Context context) {
    String deviceCountryCode = null;

    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if(tm != null) {
            deviceCountryCode = tm.getNetworkCountryIso();
        }

    if (deviceCountryCode != null && deviceCountryCode.length() <=3) {
        deviceCountryCode = deviceCountryCode.toUpperCase();
    }
    else {
        deviceCountryCode = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getCountry().toUpperCase();
    }

  //  Log.d("countryCode","  : " + deviceCountryCode );
    return deviceCountryCode;
}
 0
Author: Abhishek Garg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-24 10:45:26

Si desea hacer una tarea específica para los usuarios reside en la India que habla hindi a continuación, utilice a continuación si la condición

if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
 //Block executed only for the users resides in India who speaks Hindi 
}
 -3
Author: Divyaadz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-29 07:09:24