本文整理汇总了Java中org.apache.commons.lang3.LocaleUtils.toLocale方法的典型用法代码示例。如果您正苦于以下问题:Java LocaleUtils.toLocale方法的具体用法?Java LocaleUtils.toLocale怎么用?Java LocaleUtils.toLocale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.lang3.LocaleUtils
的用法示例。
在下文中一共展示了LocaleUtils.toLocale方法的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: 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);
}
}
示例4: 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;
}
}
示例5: 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;
}
}
}
示例6: 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);
}
示例7: testJpLocales
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
@Test
public void testJpLocales() {
final Calendar cal= Calendar.getInstance(GMT);
cal.clear();
cal.set(2003, Calendar.FEBRUARY, 10);
cal.set(Calendar.ERA, GregorianCalendar.BC);
final Locale locale = LocaleUtils.toLocale("zh"); {
// ja_JP_JP cannot handle dates before 1868 properly
final SimpleDateFormat sdf = new SimpleDateFormat(LONG_FORMAT, locale);
final DateParser fdf = getInstance(LONG_FORMAT, locale);
try {
checkParse(locale, cal, sdf, fdf);
} catch(final ParseException ex) {
fail("Locale "+locale+ " failed with "+LONG_FORMAT+"\n" + trimMessage(ex.toString()));
}
}
}
示例8: applyLocale
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
private Statement applyLocale(final SystemDefaults defaults, final Statement stmt) {
if (defaults.locale().isEmpty()) {
return stmt;
}
final Locale newLocale = LocaleUtils.toLocale(defaults.locale());
return new Statement() {
@Override
public void evaluate() throws Throwable {
final Locale save = Locale.getDefault();
try {
Locale.setDefault(newLocale);
stmt.evaluate();
} finally {
Locale.setDefault(save);
}
}
};
}
示例9: testJpLocales
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
@Test
public void testJpLocales() {
final Calendar cal= Calendar.getInstance(GMT);
cal.clear();
cal.set(2003, Calendar.FEBRUARY, 10);
cal.set(Calendar.ERA, GregorianCalendar.BC);
final Locale locale = LocaleUtils.toLocale("zh"); {
// ja_JP_JP cannot handle dates before 1868 properly
final SimpleDateFormat sdf = new SimpleDateFormat(LONG_FORMAT, locale);
final DateParser fdf = getInstance(LONG_FORMAT, locale);
try {
checkParse(locale, cal, sdf, fdf);
} catch(final ParseException ex) {
Assert.fail("Locale "+locale+ " failed with "+LONG_FORMAT+"\n" + trimMessage(ex.toString()));
}
}
}
示例10: getLastModificationTimeForLocale
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
/**
* Gets the last modification time formatted with the specified locale.
*
* @param locale The locale (will be parsed).
*
* @return The last modification time formatted with the specified locale.
*/
public final String getLastModificationTimeForLocale(final String locale) {
Locale currentLocale = null;
try {
currentLocale = LocaleUtils.toLocale(locale);
}
catch(final Exception ex) {}
return getLastModificationTimeForLocale(currentLocale);
}
示例11: getLocaleStrings
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
/**
* Get a list of iso2 locales that the webapp can handle
* @return
*/
public List<String> getLocaleStrings() {
if (locales==null) {
locales = Arrays.asList(configuration.getStringArray("config/i18n/locale"));
for (String locale : locales) {
try {
LocaleUtils.toLocale(locale); // Validity check
} catch (IllegalArgumentException e) {
throw new YadaConfigurationException("Locale {} is invalid", locale);
}
}
}
return locales;
}
示例12: setDefaultLocaleString
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
/**
* Set default Locale. Validates locale String is valid before setting
* locale
*
* @param locale
*/
public void setDefaultLocaleString(String locale) {
try {
defaultLocale = LocaleUtils.toLocale(locale);
} catch (IllegalArgumentException e) {
LOG.warn("Invalid locale has been set up [" + locale + "]");
throw e;
}
}
示例13: get
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
/**
* Gets all resources from the given {@code rootDir} directory path.
*
* <p>The {@code baseName} is the base of the filename of the resource files to look for.<br>
* The base name is without extension and without any locale information.<br>
* When a resource type is given, only resources of that type will returned.</p>
*
* <p>This function will not load the contents of the file, only its description.<br>
* If you want to load the contents, use {@link #load(Resource)} afterwards.</p>
*
* @param rootDir the root directory of the resources
* @param baseName the base name of the resource files to look for
* @param type the type of the resource files to look for
* @return list of found resources
* @throws IOException if an I/O error occurs reading the directory.
*/
public static List<Resource> get(Path rootDir, String baseName, Optional<ResourceType> type) throws IOException {
List<Resource> result = Lists.newLinkedList();
List<Path> files = Files.walk(rootDir, 1).collect(Collectors.toList());
for (Path p : files) {
ResourceType resourceType = null;
for (ResourceType t : ResourceType.values()) {
if (isResourceType(type, t) && isResource(rootDir, p, t, baseName)) {
resourceType = t;
break;
}
}
if (resourceType != null) {
String fileName = p.getFileName().toString();
String extension = resourceType.getExtension();
Locale locale = null;
Path path = null;
if (resourceType.isEmbedLocale()) {
String pattern = "^" + baseName + "_(" + LOCALE_REGEX + ")" + extension + "$";
Matcher match = Pattern.compile(pattern).matcher(fileName);
if (match.find()) {
locale = LocaleUtils.toLocale(match.group(1));
}
path = Paths.get(rootDir.toString(), baseName + (locale == null ? "" : "_" + locale.toString()) + extension);
} else {
locale = LocaleUtils.toLocale(fileName);
path = Paths.get(rootDir.toString(), locale.toString(), baseName + extension);
}
result.add(new Resource(resourceType, path, locale));
}
};
return result;
}
示例14: getLocale
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
private Locale getLocale(final ServletRequest request) {
Locale locale = request.getLocale();
final String paramLocale = request.getParameter("locale");
if (paramLocale == null || paramLocale.isEmpty()) {
return locale;
}
try {
locale = LocaleUtils.toLocale(paramLocale);
} catch (Exception e) {
//Swallow. Locale is initially set to ServletRequest locale
}
return locale;
}
示例15: getLocaleFromLanguage
import org.apache.commons.lang3.LocaleUtils; //导入方法依赖的package包/类
/**
* Gets a correct Locale (language + country) from given language.
*
* @param language
* as 2char
* @return Locale
*/
public static Locale getLocaleFromLanguage(String language) {
if (StringUtils.isBlank(language)) {
return Locale.getDefault();
}
// do we have a newer locale settings style?
if (language.length() > 2) {
return LocaleUtils.toLocale(language);
}
if (language.equalsIgnoreCase("en")) {
return new Locale("en", "US"); // don't mess around; at least fixtate this
}
Locale l = null;
List<Locale> countries = LocaleUtils.countriesByLanguage(language.toLowerCase(Locale.ROOT));
for (Locale locale : countries) {
if (locale.getCountry().equalsIgnoreCase(language)) {
// map to main countries; de->de_DE (and not de_CH)
l = locale;
}
}
if (l == null && countries.size() > 0) {
// well, take the first one
l = countries.get(0);
}
return l;
}