blob: 9f15a185f661aeea7882a2a0bc0b4e5544e90e50 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.preferences;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
public class LocaleUtils {
private LocaleUtils() {
}
/**
* @param locale
* @param displayByRespectiveLocale true, if the locale itself should be used to get the display name
* @return the display name for the given locale
*/
public static String getDisplayName(Locale locale, boolean displayByRespectiveLocale) {
String displayName = displayByRespectiveLocale ?
locale.getDisplayLanguage(locale) :
locale.getDisplayLanguage();
if (locale.getLanguage().length()>0 && locale.getCountry().length()>0){
displayName = displayName + " " + ( displayByRespectiveLocale ?
locale.getDisplayCountry(locale) :
locale.getDisplayCountry() );
if (locale.getVariant().length()>0) {
displayName = displayName + " " + ( displayByRespectiveLocale ?
locale.getDisplayVariant(locale):
locale.getDisplayVariant() );
}
}
return displayName;
}
/**
* @param locales the array of locales, that should be sorted
* @param displayByRespectiveLocale true, if the locale itself should be used to get the display name for sorting
*/
public static void sortLocalesOnDisplayName(Locale[] locales, final boolean displayByRespectiveLocale) {
Comparator<Locale> localeComparator = (locale1, locale2) -> {
String locale1Name = displayByRespectiveLocale ? locale1.getDisplayName(locale1) : locale1.getDisplayName();
String locale2Name = displayByRespectiveLocale ? locale2.getDisplayName(locale2) : locale2.getDisplayName();
return locale1Name.compareTo(locale2Name);
};
Arrays.sort(locales, localeComparator);
}
}