如果我有英文(美国)浏览器,那么标题会返回给我en_US
。我在这里收到美国国家代码。但是,例如,当我使用日语并下载标题时,我只收到它ja
。没有给出国家代码,例如JP
。是否有可能根据语言获得国家代码?
答案 0 :(得分:1)
您想检查Locale并构建适合您用例的内容。
以下是一个快速示例,展示如何使用Locale来实现所需的行为。代码仍然可以改进。
package stackoverflow;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
public final class CountriesList {
private final Map<String, String> map = new TreeMap<>();
private static CountriesList instance = null;
private CountriesList() {
init();
}
// static method to create instance of Singleton class
public static CountriesList getInstance() {
if (instance == null) {
instance = new CountriesList();
}
return instance;
}
public static void main(String[] args) {
CountriesList list = CountriesList.getInstance();
System.out.println(list.getCounryCode("da"));
System.out.println(list.getLanguage("ja"));
System.out.println(list.getCounryName("en-US"));
System.out.println(list.get("ar-MA"));
}
public String getCounryCode(String code) {
Locale locale = Locale.forLanguageTag(code);
if ("".equals(locale.getCountry())) {
locale = new Locale(code, map.get(code));
}
return locale.getCountry();
}
public String getCounryName(String code) {
Locale locale = Locale.forLanguageTag(code);
if ("".equals(locale.getCountry())) {
locale = new Locale(code, map.get(code));
}
return locale.getDisplayCountry();
}
public String getLanguage(String code) {
Locale locale = Locale.forLanguageTag(code);
if ("".equals(locale.getCountry())) {
locale = new Locale(code, map.get(code));
}
return locale.getDisplayLanguage();
}
public String get(String code) {
Locale locale = Locale.forLanguageTag(code);
if ("".equals(locale.getCountry())) {
locale = new Locale(code, map.get(code));
}
StringBuilder sb = new StringBuilder();
sb.append("Language = ").append(locale.getDisplayLanguage());
sb.append(", Country = ").append(locale.getDisplayCountry());
sb.append(", Language (Country) = ").append(locale.getDisplayName());
sb.append("\n");
return sb.toString();
}
// create map with languages and corresponding countries
public void init() {
Locale[] locales = Locale.getAvailableLocales();
for (Locale locale : locales) {
if ((locale.getDisplayCountry() != null) && (!"".equals(locale.getDisplayCountry()))) {
map.put(locale.getLanguage(), locale.getCountry());
}
}
}
}
输出:
// DK
// Japanese
// United States
// Language = Arabic, Country = Morocco, Language (Country) = Arabic (Morocco)
我希望这有帮助!