Android 기기에서 현재 언어를 선택하려면 어떻게해야합니까?
Android 기기에서 현재 언어를 선택하려면 어떻게해야합니까?
답변:
장치의 선택된 언어를 얻으려면 다음이 도움이 될 수 있습니다.
Locale.getDefault().getDisplayLanguage();
Locale.getDefault().getLanguage();일반적인 언어 코드 (예 : "de", "en")를 얻는 데 사용할 수 있습니다 .
Locale.getDefault().toString()로케일을 완전히 식별하는 문자열 (예 : "en_US")을 선호 합니다.
내 Android 4.1.2 기기에서 로케일 메소드와 결과를 확인했습니다.
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
Locale.getDefault().toLanguageTag() ---> en-US
Locale.getDefault().getDisplayLanguage()어떤 반환이 있습니다"English"
나를 위해 일한 것은 다음과 같습니다.
Resources.getSystem().getConfiguration().locale;
Resources.getSystem() 시스템 리소스 (응용 프로그램 리소스 없음)에만 액세스 할 수 있고 현재 화면에 대해 구성되지 않은 전역 공유 리소스 개체를 반환합니다 (차원 단위를 사용할 수없고 방향에 따라 변경되지 않음 등).
때문에 getConfiguration.locale지금은 사용되지 않습니다, 안드로이드 누가의 기본 로케일을 얻을 수있는 좋은 방법입니다 :
Resources.getSystem().getConfiguration().getLocales().get(0);
이전 Android 버전과의 호환성을 보장하기 위해 가능한 해결책은 간단한 점검입니다.
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;
}
최신 정보
지원 라이브러리 26.1.0를 시작 하면 이전 버전과 호환되는 편리한 방법을 제공하므로 Android 버전을 확인할 필요가 없습니다 getLocales().
간단히 전화하십시오 :
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())
getLocales것이 좋습니다. 내 답변을 업데이트했습니다.
현재 로케일에서 언어를 '추출'할 수 있습니다. 표준 Java API 또는 Android 컨텍스트를 사용하여 로케일을 추출 할 수 있습니다. 예를 들어 아래 두 줄은 동일합니다.
String locale = context.getResources().getConfiguration().locale.getDisplayName();
String locale = java.util.Locale.getDefault().getDisplayName();
Locale.setDefault()미리 다른 로케일로 전화 하면 다른 결과를 얻을 수 있습니다.
다른 사람들의 시간 및 / 또는 혼란을 줄이기 위해 위에서 Johan Pelgrim이 제안한 두 가지 대안을 시도했으며 내 장치에서 기본 위치 변경 여부에 관계없이 동등하다는 것을 공유하고 싶었습니다.
따라서 내 장치의 기본 설정은 영어 (United Kindom)이며 Johan의 대답에있는 두 문자열 모두 동일한 결과를 제공합니다. 전화 설정에서 로케일을 변경하고 (예 : italiano (Italia)) 다시 실행하면 Johan의 답변에있는 두 문자열 모두 로케일을 italiano (이탈리아)로 지정합니다.
따라서 나는 Johan의 원래 게시물이 정확하고 gregm의 의견이 잘못되었다고 생각합니다.
로케일 참조에 설명 된대로 언어를 얻는 가장 좋은 방법은 다음과 같습니다.
Locale.getDefault().getLanguage()
이 메소드는 ISO 639-1 표준 에 따라 언어 ID를 가진 문자열을 리턴합니다.
이것을 사용할 수 있습니다
boolean isLang = Locale.getDefault().getLanguage().equals("xx");
"xx"가 "en", "fr", "sp", "ar".... 등과 같은 언어 코드 인 경우
API 레벨이 24 이상이면 LocaleList.getDefault().get(0).getLanguage()
다른 것을 사용하십시오.Locale.getDefault.getLanguage()
private fun getSystemLocale(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return LocaleList.getDefault().get(0).getLanguage();
} else {
return Locale.getDefault.getLanguage();
}
}
참조 : https://developer.android.com/guide/topics/resources/multilingual-support
Johan Pelgrim의 답변에 추가하려면
context.getResources().getConfiguration().locale
Locale.getDefault()
android.text.format.DateFormat클래스가 서로 호환 가능 하기 때문에 동일합니다.
private static String zeroPad(int inValue, int inMinDigits) {
return String.format(Locale.getDefault(), "%0" + inMinDigits + "d", inValue);
}
과
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 ...
}
시스템 리소스에서 로캘을 가져 오려고 시도 할 수 있습니다.
PackageManager packageManager = context.getPackageManager();
Resources resources = packageManager.getResourcesForApplication("android");
String language = resources.getConfiguration().locale.getLanguage();
위의 답변은 중국어 간체와 번체 중국어를 구분하지 않습니다.
Locale.getDefault().toString()"zh_CN", "zh_TW", "en_US"등을 반환하는 작동
참조 : https://developer.android.com/reference/java/util/Locale.html , ISO 639-1은 OLD입니다.
언어를 선택하면이 그리스어를 입력 할 수 없습니다.
getDisplayLanguage().toString() = English
getLanguage().toString() = en
getISO3Language().toString() = eng
getDisplayLanguage()) = English
getLanguage() = en
getISO3Language() = eng
이제 그리스어로 사용해보십시오
getDisplayLanguage().toString() = Ελληνικά
getLanguage().toString() = el
getISO3Language().toString() = ell
getDisplayLanguage()) = Ελληνικά
getLanguage() = el
getISO3Language() = ell
현재 언어를 확인하려면 @Sarpe (@Thorbear)의 답변을 사용하십시오.
val language = ConfigurationCompat.getLocales(Resources.getSystem().configuration)?.get(0)?.language
// Check here the language.
val format = if (language == "ru") "d MMMM yyyy г." else "d MMMM yyyy"
val longDateFormat = SimpleDateFormat(format, Locale.getDefault())
DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(Date()).
if(Locale.getDefault().getDisplayName().equals("हिन्दी (भारत)")){
// your code here
}
장치의 언어를 얻는 올바른 방법은 다음과 같습니다.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return context.getResources().getConfiguration().getLocales().get(0);
} else {
return context.getResources().getConfiguration().locale;
}
도움이 되길 바랍니다.
Locale.getDefault().getDisplayLanguage()
Written예를 들어 언어의 이름을 알려줍니다 .English, Dutch, French
Locale.getDefault().getLanguage()
당신을 줄 것이다 language code, 예를 들면 :en, nl, fr
두 방법 모두 String을 반환
public class LocalUtils {
private static final String LANGUAGE_CODE_ENGLISH = "en";
// returns application language eg: en || fa ...
public static String getAppLanguage() {
return Locale.getDefault().getLanguage();
}
// returns device language eg: en || fa ...
public static String getDeviceLanguage() {
return ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0).getLanguage();
}
public static boolean isDeviceEnglish() {
return getDeviceLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
}
public static boolean isAppEnglish() {
return getAppLanguage().equals(new Locale(LANGUAGE_CODE_ENGLISH).getLanguage());
}
}
Log.i("AppLanguage: ", LocalUtils.getAppLanguage());
Log.i("DeviceLanguage: ", LocalUtils.getDeviceLanguage());
Log.i("isDeviceEnglish: ", String.valueOf(LocalUtils.isDeviceEnglish()));
Log.i("isAppEnglish: ", String.valueOf(LocalUtils.isAppEnglish()));
다른 사람들은 장치 언어에 대한 좋은 답변을 주었고
당신이 원하는 경우 응용 프로그램 언어에게 그것이 추가하는 것입니다 할 수있는 가장 쉬운 방법은 app_lang당신의 열쇠 strings.xml파일을, 그리고뿐만 아니라 랭 가문의 각각에 대해 LANG을 지정합니다.
이렇게하면 앱의 기본 언어가 장치 언어와 다른 경우 서비스의 매개 변수로 보내도록 선택할 수 있습니다.
public void GetDefaultLanguage( ) {
try {
String langue = Locale.getDefault().toString(); // ---> en_US
/*
Log.i("TAG", Locale.getDefault().getLanguage() ); // ---> en
Log.i("TAG", Locale.getDefault().getISO3Language() ); // ---> eng
Log.i("TAG", Locale.getDefault().getCountry() ); // ---> US
Log.i("TAG", Locale.getDefault().getISO3Country() ); // ---> USA
Log.i("TAG", Locale.getDefault().getDisplayCountry() ); // ---> United States
Log.i("TAG", Locale.getDefault().getDisplayName() ); // ---> English (United States)
Log.i("TAG", Locale.getDefault().toString() ); // ---> en_US
Log.i("TAG", Locale.getDefault().getDisplayLanguage() ); //---> English
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
langue = Locale.getDefault().toLanguageTag(); // ---> en-US
url_Api = getUrlMicrosoftLearn(langue);
Log.i("TAG", url_Api );
Log.i("TAG", langue );
}else{
langue = langue.replace("_","-"); // ---> en-US
url_Api = getUrlMicrosoftLearn(langue);
Log.i("TAG", url_Api );
Log.i("TAG", langue );
}
}catch (Exception ex) {
Log.i("TAG", "Exception:GetDefaultLanguage()", ex);
}
}
public String getUrlMicrosoftLearn(String langue) {
return "https://docs.microsoft.com/"+langue+"/learn";
}
내 솔루션은 다음과 같습니다
@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);
}
그리고
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.e("Locale", getCurrentLocale().getLanguage());
} else {
Log.e("Locale", getCurrentLocale2().toString());
}
표시된 ---> ko
다음은 장치 국가를 가져 오는 코드입니다. 모든 안드로이드 버전의 오레오와 호환됩니다.
해결책 : 사용자가 국가를 얻는 것보다 SIM 카드를 가지고 있지 않은 경우 전화 설정 또는 현재 언어 선택 중에 사용됩니다.
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;
}