Android 判斷當前語言環境是否是中文環境

http://www.jb51.net/article/110935.htm


話不多說,請看代碼:

public static boolean isZh(Context context) { 
 Locale locale = context.getResources().getConfiguration().locale; 
 String language = locale.getLanguage(); 
 if (language.endsWith("zh")) 
 return true; 
 else 
 return false; 
} 

PS: android判斷當前系統用的是什麼語言

判斷國家:

中文:getResources().getConfiguration().locale.getCountry().equals("CN")

繁體中文: getResources().getConfiguration().locale.getCountry().equals("TW")

英文(英式):getResources().getConfiguration().locale.getCountry().equals("UK")

英文(美式):getResources().getConfiguration().locale.getCountry().equals("US")

如果不清楚當前國家的簡寫,可以直接

System.out(getResources().getConfiguration().locale.getCountry());打印出來即可

下面是判斷是否是中文或者繁體中文(臺灣):

public boolean isLunarSetting() { 
  String language = getLanguageEnv(); 
 
  if (language != null 
    && (language.trim().equals("zh-CN") || language.trim().equals("zh-TW"))) 
   return true; 
  else 
   return false; 
 } 
private String getLanguageEnv() { 
  Locale l = Locale.getDefault(); 
  String language = l.getLanguage(); 
  String country = l.getCountry().toLowerCase(); 
  if ("zh".equals(language)) { 
   if ("cn".equals(country)) { 
    language = "zh-CN"; 
   } else if ("tw".equals(country)) { 
    language = "zh-TW"; 
   } 
  } else if ("pt".equals(language)) { 
   if ("br".equals(country)) { 
    language = "pt-BR"; 
   } else if ("pt".equals(country)) { 
    language = "pt-PT"; 
   } 
  } 
  return language; 
 } 

String format = Settings.System.getString(context4Year.getContentResolver(), Settings.System.DATE_FORMAT);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章