本文整理汇总了Java中org.apache.commons.lang3.LocaleUtils类的典型用法代码示例。如果您正苦于以下问题:Java LocaleUtils类的具体用法?Java LocaleUtils怎么用?Java LocaleUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LocaleUtils类属于org.apache.commons.lang3包,在下文中一共展示了LocaleUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toLocale
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Converts the given locale {@link String} to a {@link Locale}.
* The case of the {@link String} is irrelevant unlike {@link LocaleUtils#toLocale(String)}.
*
* @param locale the {@link String} to convert.
* @return the converted {@link Locale}.
*/
public static Locale toLocale(String locale) {
final String[] parts = locale.split("_");
for(int i = 0; i < parts.length; i++) {
switch (i) {
case 0:
locale = parts[0].toLowerCase();
break;
case 1:
locale += "_" + parts[1].toUpperCase();
break;
case 2:
locale += "_" + parts[2];
}
}
return LocaleUtils.toLocale(locale);
}
示例2: getDefaultLocale
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Returns the default locale when getting a string that doesn't have a value for the requested locale.
* If the default locale is not set, the string will have an empty value and no attempt on another locale will be made.
* @return the default locale, or null if no default is set
*/
public Locale getDefaultLocale() {
if (!defaultLocaleChecked) {
defaultLocaleChecked = true;
String localeString = configuration.getString("config/i18n/locale[@default='true']", null);
if (localeString!=null) {
try {
defaultLocale = LocaleUtils.toLocale(localeString);
} catch (IllegalArgumentException e) {
throw new YadaConfigurationException("Locale {} is invalid", localeString);
}
} else {
log.warn("No default locale has been set with <locale default=\"true\">: set a default locale if you don't want empty strings returned for missing localized values in the database");
}
}
return defaultLocale;
}
示例3: validateLocale
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
public static void validateLocale(String str) throws LocaleValidationException {
if (StringUtils.isBlank(str)) {
throw new LocaleValidationException("Locale must be set.");
}
try {
Locale locale = LocaleHelper.fromITunesLocale(str);
if (!LocaleUtils.availableLocaleList().contains(locale)) {
throw new LocaleValidationException(String.format(
"Locale %s is unavailable.", str));
}
} catch (IllegalArgumentException e) {
throw new LocaleValidationException("Locale validation failed.", e);
}
}
示例4: convert
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* JAVADOC Method Level Comments
*
* @param message JAVADOC.
*
* @return JAVADOC.
*/
@Override
public Collection<MessageDto> convert(Message message) {
Collection<MessageDto> result = new LinkedList<MessageDto>();
Collection<MutableI18nMessage> i18nMessages = message.getInternationalisedMessages();
for (MutableI18nMessage mutableI18nMessage : i18nMessages) {
MessageDto mdto = new MessageDto();
mdto.setId(message.getId());
mdto.setApplication(message.getBaseName());
mdto.setCode(message.getMessageCd());
mdto.setLocale(LocaleUtils.toLocale(mutableI18nMessage.getLocaleCd()));
mdto.setText(mutableI18nMessage.getMessageTx());
result.add(mdto);
}
return result;
}
示例5: validateArgs
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
@Override
public void validateArgs(Arguments args) throws ArgumentsException {
args.atMost(1);
String localeStr = getLocaleArg(args);
if (localeStr != null) {
Locale locale;
try {
// LocaleUtils uses an underscore format (e.g. en_US), but the new Java standard
// is a hyphenated format (e.g. en-US). This allows us to use LocaleUtils' validation.
locale = LocaleUtils.toLocale(localeStr.replace('-', '_'));
} catch (IllegalArgumentException e) {
throw new ArgumentsException("Invalid locale: " + localeStr);
}
args.setOpaque(locale);
} else {
args.setOpaque(DEFAULT_LOCALE);
}
}
示例6: lookupLocaleByCountry
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
public static Locale lookupLocaleByCountry(String country) {
List<Locale> localeList = LocaleUtils.languagesByCountry(country);
if (localeList == null || localeList.size() == 0) {
throw new IllegalArgumentException("Invalid country code: " + country);
}
Locale locale = localeList.get(0);
// if English isn't the language of the first Locale, then try to find English.
if (!locale.getLanguage().equals("en")) {
for (Locale option : localeList) {
if (option.getLanguage().equals("en")) {
return option;
}
}
}
return locale;
}
示例7: Language
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
private Language(String language) {
language = language.replace('-', '_');
int index = language.indexOf(';');
if (index == -1) {
locale = LocaleUtils.toLocale(language);
q = 1.0f;
} else {
String value = language.substring(0,index).trim();
locale = LocaleUtils.toLocale(value);
float qFloat;
try {
index = language.lastIndexOf('=') + 1;
value = language.substring(index).trim();
qFloat = Float.parseFloat(value);
} catch (NumberFormatException ex) {
qFloat = 0.0f;
}
q = qFloat;
}
}
示例8: setLocale
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Change locale of user.
* @param token locale pattern.
* @return true if value was changed.LocaleUtils.toLocale(language);
*/
public boolean setLocale(String token) {
if (token == null) {
return false;
} else {
Locale newLocale = LocaleUtils.toLocale(token);
if (newLocale == null) {
return false;
} else if (newLocale.equals(this.locale)) {
return false;
} else {
this.locale = newLocale;
return CHANGED = true;
}
}
}
示例9: showAddLocaleDialog
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
public void showAddLocaleDialog() {
String localeString = "";
Path path = project.getPath();
ResourceType type = project.getResourceType();
while (localeString != null && localeString.isEmpty()) {
localeString = Dialogs.showInputDialog(this,
MessageBundle.get("dialogs.locale.add.title", type),
MessageBundle.get("dialogs.locale.add.text"),
JOptionPane.QUESTION_MESSAGE);
if (localeString != null) {
localeString = localeString.trim();
if (localeString.isEmpty()) {
showError(MessageBundle.get("dialogs.locale.add.error.invalid"));
} else {
try {
Locale locale = LocaleUtils.toLocale(localeString);
Resource resource = Resources.create(path, type, Optional.of(locale), project.getResourceName());
addResource(resource);
} catch (IOException e) {
log.error("Error creating new locale", e);
showError(MessageBundle.get("dialogs.locale.add.error.create"));
}
}
}
}
}
示例10: LanguageTagsMapping
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Instantiates a new Language tags mapping.
*/
public LanguageTagsMapping() {
super(NAME);
this.withCaseSensitive(false);
// Build reverse map. Use a tree map to offer case insensitiveness while preserving keys case (useful for extending)
TreeMap<String, Locale> map = new TreeMap<String, Locale>(this.getCaseSensitive() ? null : String.CASE_INSENSITIVE_ORDER);
for (Locale loc : LocaleUtils.availableLocaleList()) {
String isoCode = loc.getLanguage();
if (isoCode.length() > 0) {
String displayValue = loc.toLanguageTag();
if (!map.containsKey(displayValue)) {
// Also add variant with underscores
map.put(displayValue, loc);
map.put(displayValue.replace('-', '_'), loc);
}
}
}
this.withMapping(map);
}
示例11: DisplayNamesMapping
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Instantiates a new Display names mapping.
*
* @param name the name
* @param displayLocale the display locale
*/
public DisplayNamesMapping(String name, Locale displayLocale) {
super(name);
this.displayLocale = displayLocale;
// Build reverse map
HashMap<String, Locale> map = new HashMap<String, Locale>();
for (String isoCode : Locale.getISOLanguages()) {
Locale loc = LocaleUtils.toLocale(isoCode);
String displayName = loc.getDisplayName(displayLocale).toLowerCase();
if (isoCode.length() > 0 && !map.containsKey(displayName)) {
map.put(displayName, loc);
}
}
this.withMapping(map).withCaseSensitive(false);
}
示例12: ISO639Alpha3Mapping
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Instantiates a new ISO 639 alpha 3 mapping.
*/
public ISO639Alpha3Mapping() {
super(NAME);
// Build reverse map
HashMap<String, Locale> map = new HashMap<String, Locale>();
for (Locale loc : LocaleUtils.availableLocaleList()) {
String isoCode = loc.getLanguage();
if (isoCode.length() > 0) {
String displayValue = loc.getISO3Language().toLowerCase();
if (!map.containsKey(displayValue)) {
map.put(displayValue, LocaleUtils.toLocale(isoCode));
}
}
}
this.withMapping(map).withCaseSensitive(false);
}
示例13: ISO639Alpha2Mapping
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
/**
* Instantiates a new ISO 639 alpha 2 mapping.
*/
public ISO639Alpha2Mapping() {
super(NAME);
// Build reverse map
HashMap<String, Locale> map = new HashMap<String, Locale>();
for (String isoCode : Locale.getISOLanguages()) {
if (isoCode.length() > 0) {
String displayValue = isoCode.toLowerCase();
if (!map.containsKey(displayValue)) {
map.put(displayValue, LocaleUtils.toLocale(isoCode));
}
}
}
this.withMapping(map).withCaseSensitive(false);
}
示例14: testDefaultMappings
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
@Test
public void testDefaultMappings() throws Exception {
MappingsFactory f = new MappingsFactory(Collections.emptyList());
assertEquals(f.getMappings().size(), 4);
assertTrue(f.getMappings().containsKey("ISO-639-ALPHA-2"));
assertEquals(f.getMappings().get("ISO-639-ALPHA-2").getMapping().get("en"), LocaleUtils.toLocale("en"));
assertTrue(f.getMappings().containsKey("ISO-639-ALPHA-3"));
assertEquals(f.getMappings().get("ISO-639-ALPHA-3").getMapping().get("eng"), LocaleUtils.toLocale("en"));
assertTrue(f.getMappings().containsKey("LANGUAGE_TAGS"));
assertEquals(f.getMappings().get("LANGUAGE_TAGS").getMapping().get("en-US"), LocaleUtils.toLocale("en_US"));
assertTrue(f.getMappings().containsKey("ENGLISH_NAMES"));
assertEquals(f.getMappings().get("ENGLISH_NAMES").getMapping().get("french"), LocaleUtils.toLocale("fr"));
}
示例15: testCreateMappingFilterAndOverride
import org.apache.commons.lang3.LocaleUtils; //导入依赖的package包/类
@Test
public void testCreateMappingFilterAndOverride() throws Exception {
MappingConfig config = new MappingConfig();
config.name = "test";
config.extend = ImmutableList.of("ISO-639-ALPHA-3");
config.filter = "en,fr";
config.override = ImmutableMap.of("fr", "french,francais");
MappingsFactory f = new MappingsFactory(Collections.singletonList(config));
assertTrue(f.getMappings().containsKey("test"));
Mapping mapping = f.getMappings().get("test");
assertEquals(mapping.getMapping().size(), 3);
assertEquals(mapping.getMapping().get("eng"), LocaleUtils.toLocale("en")); // Kept by filter
assertFalse(mapping.getMapping().containsKey("fra")); // Kept by filter but overriden
assertFalse(mapping.getMapping().containsKey("ita")); // Removed by filter
assertEquals(mapping.getMapping().get("french"), LocaleUtils.toLocale("fr"));
assertEquals(mapping.getMapping().get("francais"), LocaleUtils.toLocale("fr"));
}