如何根据国家code在Java中获得语言code [英] How to get the language code based on country code in java
问题描述
如何根据国家code在Java中
获得语言code
喜
我有一个情况我需要找到基于国家code语言。
对于国名瑞士我的国家code为CH。
但是可以有3 languges像德国,法国和ukenglish在瑞士。我需要找到基于国家code语言code。
场景:我国code是CH。我需要在此基础上国家code语言code。
How to get the language code based on country code in java
Hi
I had a situation where I need to find the language based on country code.
For country name "switzerland" I had country code as "CH" .
But there can be 3 languges like German,french and ukenglish in Switzerland . I need to find the language code based on country code.
Scenario : My country code is "CH" . I need to get the language code based on this country code.
在此先感谢
推荐答案
该程序将打印语言codeS为瑞士所有可用的语言环境。它可以很容易地修改,以例如返回名单,其中,语言codeS的
;字符串&GT。挑选你想要的是作为练习留给读者之一 - 瑞士没有选择接单preferred的语言为他们的国家
This program prints the language codes for all available locales for Switzerland. It can easily be modified to e.g. return a List<String>
of language codes. Picking the one you want is left as an exercise for the reader - the Swiss have not chosen to pick a single preferred language for their country.
import java.util.Locale;
public class Test {
public static void main(String[] args) {
Locale[] all = Locale.getAvailableLocales();
for (Locale locale : all) {
String country = locale.getCountry();
if(country.equalsIgnoreCase("CH")){
System.out.println(locale.getLanguage());
}
}
}
}
输出:
fr
de
it
这篇关于如何根据国家code在Java中获得语言code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!