如何在Android设备中选择当前语言?
当前回答
下面是获取设备国家的代码。兼容所有版本的android甚至奥利奥。
解决方案:如果用户没有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;
}
其他回答
如果你想检查当前语言,请使用@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())
如果你使用喷气背包撰写
在可组合功能方面
Context.applicationContext.resources.configuration.locales.get(0).language
再加上约翰·佩尔格里姆的回答
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);
}
and
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 ...
}
您可以使用此代码找出键盘电流
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();
String locale = ims.getLocale();
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://learn.microsoft.com/"+langue+"/learn";
}
推荐文章
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角