下面列出了java.util.Locale#getDisplayLanguage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main (String[] args) throws Exception {
Locale es = new Locale ("es");
String[] isoLangs = es.getISOLanguages ();
String error = "";
for (int i = 0; i < isoLangs.length; i++) {
Locale current = new Locale (isoLangs[i]);
String localeString = current.getDisplayLanguage (es);
String startLetter = localeString.substring (0,1);
if (!startLetter.toLowerCase (es).equals (startLetter)){
error = error + "\n\t"+ isoLangs[i] + " " + localeString;
}
}
if (error.length () > 0){
throw new Exception ("\nFollowing language names starts with upper-case letter: "
+ error + "\nLower-case expected!");
}
}
public static void main (String[] args) throws Exception {
Locale es = new Locale ("es");
String[] isoLangs = es.getISOLanguages ();
String error = "";
for (int i = 0; i < isoLangs.length; i++) {
Locale current = new Locale (isoLangs[i]);
String localeString = current.getDisplayLanguage (es);
String startLetter = localeString.substring (0,1);
if (!startLetter.toLowerCase (es).equals (startLetter)){
error = error + "\n\t"+ isoLangs[i] + " " + localeString;
}
}
if (error.length () > 0){
throw new Exception ("\nFollowing language names starts with upper-case letter: "
+ error + "\nLower-case expected!");
}
}
public Languages(Context context) {
((SyncthingApp) context.getApplicationContext()).component().inject(this);
Map<String, String> tmpMap = new TreeMap<>();
List<Locale> locales = Arrays.asList(LOCALES_TO_TEST);
// Capitalize language names
Collections.sort(locales, (l1, l2) -> l1.getDisplayLanguage().compareTo(l2.getDisplayLanguage()));
for (Locale locale : locales) {
String displayLanguage = locale.getDisplayLanguage(locale);
displayLanguage = displayLanguage.substring(0, 1).toUpperCase(locale) + displayLanguage.substring(1);
tmpMap.put(locale.getLanguage(), displayLanguage);
}
// remove the current system language from the menu
tmpMap.remove(Locale.getDefault().getLanguage());
/* SYSTEM_DEFAULT is a fake one for displaying in a chooser menu. */
tmpMap.put(USE_SYSTEM_DEFAULT, context.getString(R.string.pref_language_default));
mAvailableLanguages = Collections.unmodifiableMap(tmpMap);
}
public static void main (String[] args) throws Exception {
Locale es = new Locale ("es");
String[] isoLangs = es.getISOLanguages ();
String error = "";
for (int i = 0; i < isoLangs.length; i++) {
Locale current = new Locale (isoLangs[i]);
String localeString = current.getDisplayLanguage (es);
String startLetter = localeString.substring (0,1);
if (!startLetter.toLowerCase (es).equals (startLetter)){
error = error + "\n\t"+ isoLangs[i] + " " + localeString;
}
}
if (error.length () > 0){
throw new Exception ("\nFollowing language names starts with upper-case letter: "
+ error + "\nLower-case expected!");
}
}
public static void main (String[] args) throws Exception {
Locale es = new Locale ("es");
String[] isoLangs = es.getISOLanguages ();
String error = "";
for (int i = 0; i < isoLangs.length; i++) {
Locale current = new Locale (isoLangs[i]);
String localeString = current.getDisplayLanguage (es);
String startLetter = localeString.substring (0,1);
if (!startLetter.toLowerCase (es).equals (startLetter)){
error = error + "\n\t"+ isoLangs[i] + " " + localeString;
}
}
if (error.length () > 0){
throw new Exception ("\nFollowing language names starts with upper-case letter: "
+ error + "\nLower-case expected!");
}
}
private String summarizeLocale(final Locale locale, final String localeAndroidCode) {
String country = locale.getDisplayCountry(locale);
String language = locale.getDisplayLanguage(locale);
String ret = locale.getDisplayLanguage(Locale.ENGLISH)
+ " (" + language.substring(0, 1).toUpperCase(Locale.getDefault()) + language.substring(1)
+ ((!country.isEmpty() && !country.toLowerCase(Locale.getDefault()).equals(language.toLowerCase(Locale.getDefault()))) ? (", " + country) : "")
+ ")";
if (localeAndroidCode.equals("zh-rCN")) {
ret = ret.substring(0, ret.indexOf(" ") + 1) + "Simplified" + ret.substring(ret.indexOf(" "));
} else if (localeAndroidCode.equals("zh-rTW")) {
ret = ret.substring(0, ret.indexOf(" ") + 1) + "Traditional" + ret.substring(ret.indexOf(" "));
} else if (localeAndroidCode.equals("sr-rRS")) {
ret = ret.substring(0, ret.indexOf(" ") + 1) + "Latin" + ret.substring(ret.indexOf(" "));
} else if (localeAndroidCode.startsWith("sr")) {
ret = ret.substring(0, ret.indexOf(" ") + 1) + "Cyrillic" + ret.substring(ret.indexOf(" "));
} else if (localeAndroidCode.equals("fil")) {
ret = ret.substring(0, ret.indexOf("(") + 1) + "Philippines)";
}
return ret;
}
/**
** Return a locale's display Name
**
** @return String used to display Locale
**
** @author Jean-Francois Leveque (Universite Pierre et Marie Curie - Paris 6)
**/
public String getLocaleDisplayName(Locale loc) {
Locale preferedLoc = getLocale();
StringBuilder displayName = new StringBuilder(loc.getDisplayLanguage(loc));
if (StringUtils.isNotBlank(loc.getDisplayCountry(loc))) {
displayName.append(" - ").append(loc.getDisplayCountry(loc));
}
if (StringUtils.isNotBlank(loc.getVariant())) {
displayName.append(" (").append(loc.getDisplayVariant(loc)).append(")");
}
displayName.append(" [").append(loc.toString()).append("] ");
displayName.append(loc.getDisplayLanguage(preferedLoc));
if (StringUtils.isNotBlank(loc.getDisplayCountry(preferedLoc))) {
displayName.append(" - ").append(loc.getDisplayCountry(preferedLoc));
}
return displayName.toString();
}
public static void main (String[] args) throws Exception {
Locale es = new Locale ("es");
String[] isoLangs = es.getISOLanguages ();
String error = "";
for (int i = 0; i < isoLangs.length; i++) {
Locale current = new Locale (isoLangs[i]);
String localeString = current.getDisplayLanguage (es);
String startLetter = localeString.substring (0,1);
if (!startLetter.toLowerCase (es).equals (startLetter)){
error = error + "\n\t"+ isoLangs[i] + " " + localeString;
}
}
if (error.length () > 0){
throw new Exception ("\nFollowing language names starts with upper-case letter: "
+ error + "\nLower-case expected!");
}
}
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
if (locales2Test.length != expectedNames.length) {
throw new Exception("\nData sizes does not match!\n");
}
StringBuffer message = new StringBuffer("");
Locale dutch = new Locale("nl", "BE");
String current;
for (int i = 0; i < locales2Test.length; i++) {
Locale locale = locales2Test[i];
current = dutch.getDisplayLanguage(locale);
if (!current.equals(expectedNames[i])) {
message.append("[");
message.append(locale.getDisplayLanguage());
message.append("] ");
message.append("Language name is ");
message.append(current);
message.append(" should be ");
message.append(expectedNames[i]);
message.append("\n");
}
}
if (message.length() >0) {
throw new Exception("\n" + message.toString());
}
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
@Override
public String interpret(Locale locale, String text) throws InterpretationException {
ResourceBundle language = ResourceBundle.getBundle(LANGUAGE_SUPPORT, locale);
Rule[] rules = getRules(locale);
if (language == null || rules.length == 0) {
throw new InterpretationException(
locale.getDisplayLanguage(Locale.ENGLISH) + " is not supported at the moment.");
}
TokenList tokens = new TokenList(tokenize(locale, text));
if (tokens.eof()) {
throw new InterpretationException(language.getString(SORRY));
}
InterpretationResult result;
InterpretationResult lastResult = null;
for (Rule rule : rules) {
if ((result = rule.execute(language, tokens)).isSuccess()) {
return result.getResponse();
} else {
if (result != InterpretationResult.SYNTAX_ERROR) {
lastResult = result;
}
}
}
if (lastResult == null) {
throw new InterpretationException(language.getString(SORRY));
} else {
throw lastResult.getException();
}
}
@Override
public String interpret(Locale locale, String text) throws InterpretationException {
ResourceBundle language = ResourceBundle.getBundle(LANGUAGE_SUPPORT, locale);
Rule[] rules = getRules(locale);
if (language == null || rules.length == 0) {
throw new InterpretationException(
locale.getDisplayLanguage(Locale.ENGLISH) + " is not supported at the moment.");
}
TokenList tokens = new TokenList(tokenize(locale, text));
if (tokens.eof()) {
throw new InterpretationException(language.getString(SORRY));
}
InterpretationResult result;
InterpretationResult lastResult = null;
for (Rule rule : rules) {
if ((result = rule.execute(language, tokens)).isSuccess()) {
return result.getResponse();
} else {
if (result != InterpretationResult.SYNTAX_ERROR) {
lastResult = result;
}
}
}
if (lastResult == null) {
throw new InterpretationException(language.getString(SORRY));
} else {
throw lastResult.getException();
}
}
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
if (locales2Test.length != expectedNames.length) {
throw new Exception("\nData sizes does not match!\n");
}
StringBuffer message = new StringBuffer("");
Locale dutch = new Locale("nl", "BE");
String current;
for (int i = 0; i < locales2Test.length; i++) {
Locale locale = locales2Test[i];
current = dutch.getDisplayLanguage(locale);
if (!current.equals(expectedNames[i])) {
message.append("[");
message.append(locale.getDisplayLanguage());
message.append("] ");
message.append("Language name is ");
message.append(current);
message.append(" should be ");
message.append(expectedNames[i]);
message.append("\n");
}
}
if (message.length() >0) {
throw new Exception("\n" + message.toString());
}
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
if (locales2Test.length != expectedNames.length) {
throw new Exception("\nData sizes does not match!\n");
}
StringBuffer message = new StringBuffer("");
Locale dutch = new Locale("nl", "BE");
String current;
for (int i = 0; i < locales2Test.length; i++) {
Locale locale = locales2Test[i];
current = dutch.getDisplayLanguage(locale);
if (!current.equals(expectedNames[i])) {
message.append("[");
message.append(locale.getDisplayLanguage());
message.append("] ");
message.append("Language name is ");
message.append(current);
message.append(" should be ");
message.append(expectedNames[i]);
message.append("\n");
}
}
if (message.length() >0) {
throw new Exception("\n" + message.toString());
}
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
private void languagestoLocales()
{
String spellLanguage = SpellcheckManager.getInstance().getSpellcheckerPreference().getPreferences().getSpellLanguage();
_languages = new HashMap<String, String>();
Locale[] locales = Locale.getAvailableLocales();
ArrayList<String> languages = SpellcheckManager.getInstance().getSupportedLanguages();
for (int i = 0; i < languages.size(); i++) {
for (final Locale locale : locales) {
if (locale.toString().equals(languages.get(i))) {
String label = locale.getDisplayLanguage(Locale
.getDefault());
if (locale.getDisplayCountry(locale) != null
&& locale.getDisplayCountry(locale).trim().length() > 0) {
label = label + "-"
+ locale.getDisplayCountry(locale).trim();
}
_languages.put(label, languages.get(i));
_languageSelection.addItem(label);
if (languages.get(i).equals(spellLanguage))
{
_languageSelection.setSelectedItem(label);
}
}
}
}
}
public static void main(String[] args) throws Exception {
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
if (locales2Test.length != expectedNames.length) {
throw new Exception("\nData sizes does not match!\n");
}
StringBuffer message = new StringBuffer("");
Locale dutch = new Locale("nl", "BE");
String current;
for (int i = 0; i < locales2Test.length; i++) {
Locale locale = locales2Test[i];
current = dutch.getDisplayLanguage(locale);
if (!current.equals(expectedNames[i])) {
message.append("[");
message.append(locale.getDisplayLanguage());
message.append("] ");
message.append("Language name is ");
message.append(current);
message.append(" should be ");
message.append(expectedNames[i]);
message.append("\n");
}
}
if (message.length() >0) {
throw new Exception("\n" + message.toString());
}
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
/**
* Compares the english timezone name and timezone name in specified locale
* @param timeZoneName - name of the timezone to compare
* @param locale - locale to test against english
* @return empty string when passed, descriptive error message in other cases
*/
private static String testTZ(String timeZoneName, Locale locale) {
StringBuffer timeZoneResult = new StringBuffer("");
TimeZone tz = TimeZone.getTimeZone(timeZoneName);
sdfEn.setTimeZone(tz);
sdfEnShort.setTimeZone(tz);
sdfLoc.setTimeZone(tz);
sdfLocShort.setTimeZone(tz);
String en, enShort, loc, locShort;
en = sdfEn.format(date);
enShort = sdfEnShort.format(date);
loc = sdfLoc.format(date);
locShort = sdfLocShort.format(date);
String displayLanguage = locale.getDisplayLanguage();
String displayCountry = locale.getDisplayCountry();
if (loc.equals(en)) {
timeZoneResult.append("[");
timeZoneResult.append(displayLanguage);
if (!"".equals(displayCountry)) {
timeZoneResult.append(" ");
timeZoneResult.append(displayCountry);
}
timeZoneResult.append("] timezone \"");
timeZoneResult.append(timeZoneName);
timeZoneResult.append("\" long name \"" + en);
timeZoneResult.append("\" not localized!\n");
}
if (!locShort.equals(enShort)) {
timeZoneResult.append("[");
timeZoneResult.append(displayLanguage);
if (!"".equals(displayCountry)) {
timeZoneResult.append(" ");
timeZoneResult.append(displayCountry);
}
timeZoneResult.append("] timezone \"");
timeZoneResult.append(timeZoneName);
timeZoneResult.append("\" short name \"" + enShort);
timeZoneResult.append("\" is localized \"");
timeZoneResult.append(locShort);
timeZoneResult.append("\"!\n");
}
return timeZoneResult.toString();
}
private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
if (defaultIsFrench && !Locale.getDefault().getLanguage().equals("fr"))
errln("Default locale should be French, but it's really " + Locale.getDefault().getLanguage());
else if (!defaultIsFrench && !Locale.getDefault().getLanguage().equals("en"))
errln("Default locale should be English, but it's really " + Locale.getDefault().getLanguage());
for (int i = 0; i <= MAX_LOCALES; i++) {
Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
logln(" Testing " + testLocale + "...");
String testLang;
String testCtry;
String testVar;
String testName;
if (inLocale == null) {
testLang = testLocale.getDisplayLanguage();
testCtry = testLocale.getDisplayCountry();
testVar = testLocale.getDisplayVariant();
testName = testLocale.getDisplayName();
}
else {
testLang = testLocale.getDisplayLanguage(inLocale);
testCtry = testLocale.getDisplayCountry(inLocale);
testVar = testLocale.getDisplayVariant(inLocale);
testName = testLocale.getDisplayName(inLocale);
}
String expectedLang;
String expectedCtry;
String expectedVar;
String expectedName;
expectedLang = dataTable[compareIndex][i];
if (expectedLang.equals("") && defaultIsFrench)
expectedLang = dataTable[DLANG_EN][i];
if (expectedLang.equals(""))
expectedLang = dataTable[DLANG_ROOT][i];
expectedCtry = dataTable[compareIndex + 1][i];
if (expectedCtry.equals("") && defaultIsFrench)
expectedCtry = dataTable[DCTRY_EN][i];
if (expectedCtry.equals(""))
expectedCtry = dataTable[DCTRY_ROOT][i];
expectedVar = dataTable[compareIndex + 2][i];
if (expectedVar.equals("") && defaultIsFrench)
expectedVar = dataTable[DVAR_EN][i];
if (expectedVar.equals(""))
expectedVar = dataTable[DVAR_ROOT][i];
expectedName = dataTable[compareIndex + 3][i];
if (expectedName.equals("") && defaultIsFrench)
expectedName = dataTable[DNAME_EN][i];
if (expectedName.equals(""))
expectedName = dataTable[DNAME_ROOT][i];
if (!testLang.equals(expectedLang))
errln("Display language mismatch: " + testLang + " versus " + expectedLang);
if (!testCtry.equals(expectedCtry))
errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
if (!testVar.equals(expectedVar))
errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
if (!testName.equals(expectedName))
errln("Display name mismatch: " + testName + " versus " + expectedName);
}
}
private void doTestDisplayNames(Locale inLocale, int compareIndex, boolean defaultIsFrench) {
if (defaultIsFrench && !Locale.getDefault().getLanguage().equals("fr"))
errln("Default locale should be French, but it's really " + Locale.getDefault().getLanguage());
else if (!defaultIsFrench && !Locale.getDefault().getLanguage().equals("en"))
errln("Default locale should be English, but it's really " + Locale.getDefault().getLanguage());
for (int i = 0; i <= MAX_LOCALES; i++) {
Locale testLocale = new Locale(dataTable[LANG][i], dataTable[CTRY][i], dataTable[VAR][i]);
logln(" Testing " + testLocale + "...");
String testLang;
String testCtry;
String testVar;
String testName;
if (inLocale == null) {
testLang = testLocale.getDisplayLanguage();
testCtry = testLocale.getDisplayCountry();
testVar = testLocale.getDisplayVariant();
testName = testLocale.getDisplayName();
}
else {
testLang = testLocale.getDisplayLanguage(inLocale);
testCtry = testLocale.getDisplayCountry(inLocale);
testVar = testLocale.getDisplayVariant(inLocale);
testName = testLocale.getDisplayName(inLocale);
}
String expectedLang;
String expectedCtry;
String expectedVar;
String expectedName;
expectedLang = dataTable[compareIndex][i];
if (expectedLang.equals("") && defaultIsFrench)
expectedLang = dataTable[DLANG_EN][i];
if (expectedLang.equals(""))
expectedLang = dataTable[DLANG_ROOT][i];
expectedCtry = dataTable[compareIndex + 1][i];
if (expectedCtry.equals("") && defaultIsFrench)
expectedCtry = dataTable[DCTRY_EN][i];
if (expectedCtry.equals(""))
expectedCtry = dataTable[DCTRY_ROOT][i];
expectedVar = dataTable[compareIndex + 2][i];
if (expectedVar.equals("") && defaultIsFrench)
expectedVar = dataTable[DVAR_EN][i];
if (expectedVar.equals(""))
expectedVar = dataTable[DVAR_ROOT][i];
expectedName = dataTable[compareIndex + 3][i];
if (expectedName.equals("") && defaultIsFrench)
expectedName = dataTable[DNAME_EN][i];
if (expectedName.equals(""))
expectedName = dataTable[DNAME_ROOT][i];
if (!testLang.equals(expectedLang))
errln("Display language mismatch: " + testLang + " versus " + expectedLang);
if (!testCtry.equals(expectedCtry))
errln("Display country mismatch: " + testCtry + " versus " + expectedCtry);
if (!testVar.equals(expectedVar))
errln("Display variant mismatch: " + testVar + " versus " + expectedVar);
if (!testName.equals(expectedName))
errln("Display name mismatch: " + testName + " versus " + expectedName);
}
}
/**
* Compares the english timezone name and timezone name in specified locale
* @param timeZoneName - name of the timezone to compare
* @param locale - locale to test against english
* @return empty string when passed, descriptive error message in other cases
*/
private static String testTZ(String timeZoneName, Locale locale) {
StringBuffer timeZoneResult = new StringBuffer("");
TimeZone tz = TimeZone.getTimeZone(timeZoneName);
sdfEn.setTimeZone(tz);
sdfEnShort.setTimeZone(tz);
sdfLoc.setTimeZone(tz);
sdfLocShort.setTimeZone(tz);
String en, enShort, loc, locShort;
en = sdfEn.format(date);
enShort = sdfEnShort.format(date);
loc = sdfLoc.format(date);
locShort = sdfLocShort.format(date);
String displayLanguage = locale.getDisplayLanguage();
String displayCountry = locale.getDisplayCountry();
if (loc.equals(en)) {
timeZoneResult.append("[");
timeZoneResult.append(displayLanguage);
if (!"".equals(displayCountry)) {
timeZoneResult.append(" ");
timeZoneResult.append(displayCountry);
}
timeZoneResult.append("] timezone \"");
timeZoneResult.append(timeZoneName);
timeZoneResult.append("\" long name \"" + en);
timeZoneResult.append("\" not localized!\n");
}
if (!locShort.equals(enShort)) {
timeZoneResult.append("[");
timeZoneResult.append(displayLanguage);
if (!"".equals(displayCountry)) {
timeZoneResult.append(" ");
timeZoneResult.append(displayCountry);
}
timeZoneResult.append("] timezone \"");
timeZoneResult.append(timeZoneName);
timeZoneResult.append("\" short name \"" + enShort);
timeZoneResult.append("\" is localized \"");
timeZoneResult.append(locShort);
timeZoneResult.append("\"!\n");
}
return timeZoneResult.toString();
}
@Nullable
public static String getDisplayLanguageNameForComponent(@Nonnull Component component) {
final Locale locale = getLocaleForComponent(component);
return locale == null ? null : locale.getDisplayLanguage();
}