java.util.Locale.forLanguageTag()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(695)

本文整理了Java中java.util.Locale.forLanguageTag()方法的一些代码示例,展示了Locale.forLanguageTag()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Locale.forLanguageTag()方法的具体详情如下:
包路径:java.util.Locale
类名称:Locale
方法名:forLanguageTag

Locale.forLanguageTag介绍

暂无

代码示例

代码示例来源:origin: Graylog2/graylog2-server

private static Locale buildLocale(Object languageTag) {
  if (languageTag instanceof String) {
    try {
      return Locale.forLanguageTag((String) languageTag);
    } catch (IllegalArgumentException e) {
      return DEFAULT_LOCALE;
    }
  } else {
    return DEFAULT_LOCALE;
  }
}

代码示例来源:origin: wildfly/wildfly

public static Locale getLocaleFromString(String localeString) {
  if (localeString == null) {
    return null;
  }
  return Locale.forLanguageTag(localeString);
}

代码示例来源:origin: apache/incubator-druid

public LowerExtractionFn(@JsonProperty("locale") String localeString)
{
 this.localeString = localeString;
 this.locale = localeString == null ? Locale.getDefault() : Locale.forLanguageTag(localeString);
}

代码示例来源:origin: apache/incubator-druid

public UpperExtractionFn(@JsonProperty("locale") String localeString)
{
 this.localeString = localeString;
 this.locale = localeString == null ? Locale.getDefault() : Locale.forLanguageTag(localeString);
}

代码示例来源:origin: stanfordnlp/CoreNLP

public void init(Properties props) {
 lang = Locale.forLanguageTag(props.getProperty(Constants.LANGUAGE_PROP, "en"));
}

代码示例来源:origin: google/ExoPlayer

private String buildLanguageString(String language) {
 Locale locale = Util.SDK_INT >= 21 ? Locale.forLanguageTag(language) : new Locale(language);
 return locale.getDisplayLanguage();
}

代码示例来源:origin: oblac/jodd

@Override
public Locale convert(final Object value) {
  if (value == null) {
    return null;
  }
  if (value.getClass() == Locale.class) {
    return (Locale) value;
  }
  return Locale.forLanguageTag(value.toString());
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * A variant of {@link #getAcceptLanguage()} that converts each
 * {@link java.util.Locale.LanguageRange} to a {@link Locale}.
 * @return the locales or an empty list
 * @throws IllegalArgumentException if the value cannot be converted to a locale
 * @since 5.0
 */
public List<Locale> getAcceptLanguageAsLocales() {
  List<Locale.LanguageRange> ranges = getAcceptLanguage();
  if (ranges.isEmpty()) {
    return Collections.emptyList();
  }
  return ranges.stream()
      .map(range -> Locale.forLanguageTag(range.getRange()))
      .filter(locale -> StringUtils.hasText(locale.getDisplayName()))
      .collect(Collectors.toList());
}

代码示例来源:origin: MovingBlocks/Terasology

private static Locale localeFromFilename(String filename) throws IOException {
    Matcher m = FILENAME_PATTERN.matcher(filename);
    if (m.matches()) {
      String langTag = m.group(2);
      return (langTag != null) ? Locale.forLanguageTag(langTag) : Locale.ROOT;
    }
    throw new IOException("Could not parse locale: " + filename);
  }
}

代码示例来源:origin: apache/storm

public static Locale getMetricsReporterLocale(Map<String, Object> topoConf) {
  String languageTag = ObjectReader.getString(topoConf.get(Config.STORM_DAEMON_METRICS_REPORTER_PLUGIN_LOCALE), null);
  if (languageTag != null) {
    return Locale.forLanguageTag(languageTag);
  }
  return null;
}

代码示例来源:origin: MovingBlocks/Terasology

public Locale getLocale() {
  if (locale == null) {
    setLocale(Locale.getDefault(Category.DISPLAY));
  }
  return Locale.forLanguageTag(locale);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Parse the given {@code String} value into a {@link Locale}, accepting
 * the {@link Locale#toString} format as well as BCP 47 language tags.
 * @param localeValue the locale value: following either {@code Locale's}
 * {@code toString()} format ("en", "en_UK", etc), also accepting spaces as
 * separators (as an alternative to underscores), or BCP 47 (e.g. "en-UK")
 * as specified by {@link Locale#forLanguageTag} on Java 7+
 * @return a corresponding {@code Locale} instance, or {@code null} if none
 * @throws IllegalArgumentException in case of an invalid locale specification
 * @since 5.0.4
 * @see #parseLocaleString
 * @see Locale#forLanguageTag
 */
@Nullable
public static Locale parseLocale(String localeValue) {
  String[] tokens = tokenizeLocaleSource(localeValue);
  if (tokens.length == 1) {
    validateLocalePart(localeValue);
    Locale resolved = Locale.forLanguageTag(localeValue);
    return (resolved.getLanguage().length() > 0 ? resolved : null);
  }
  return parseLocaleTokens(localeValue, tokens);
}

代码示例来源:origin: oblac/jodd

/**
 * Saves Locale to HTTP session.
 */
public static void setSessionLocale(final HttpSession session, final String localeCode) {
  if (log.isDebugEnabled()) {
    log.debug("Locale stored to session: " + localeCode);
  }
  Locale locale = Locale.forLanguageTag(localeCode);
  session.setAttribute(SESSION_LOCALE_ATTR, locale);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void preferredLocalesFromAcceptLanguageHeader() {
  String headerValue = "fr-ch, fr;q=0.9, en-*;q=0.8, de;q=0.7, *;q=0.5";
  request.addHeader("Accept-Language", headerValue);
  List<Locale> actual = Collections.list(request.getLocales());
  assertEquals(Arrays.asList(Locale.forLanguageTag("fr-ch"), Locale.forLanguageTag("fr"),
      Locale.forLanguageTag("en"), Locale.forLanguageTag("de")), actual);
  assertEquals(headerValue, request.getHeader("Accept-Language"));
}

代码示例来源:origin: Graylog2/graylog2-server

@AssistedInject
public CEFCodec(@Assisted Configuration configuration) {
  this.configuration = configuration;
  this.parser = CEFParserFactory.create();
  DateTimeZone timezone;
  try {
    timezone = DateTimeZone.forID(configuration.getString(CK_TIMEZONE));
  } catch (Exception e) {
    LOG.warn("Could not configure CEF input timezone. Falling back to local default. Please check the error message:", e);
    timezone = DEFAULT_TIMEZONE;
  }
  this.timezone = timezone;
  this.locale = Locale.forLanguageTag(configuration.getString(CK_LOCALE, ""));
  this.useFullNames = configuration.getBoolean(CK_USE_FULL_NAMES);
}

代码示例来源:origin: hibernate/hibernate-orm

@BeforeClass
public static void beforeClass() {
  currentLocale = Locale.getDefault();
  
  // Turkish will generate a "dotless i" when toLowerCase is used on "I".
  Locale.setDefault(Locale.forLanguageTag("tr-TR"));
}

代码示例来源:origin: robolectric/robolectric

@Implementation
public static String addLikelySubtags(String locale) {
 if (RuntimeEnvironment.getApiLevel() >= N) {
  return ULocale.addLikelySubtags(ULocale.forLanguageTag(locale)).toLanguageTag();
 } else {
  // Return what is essentially the given locale, normalized by passing through the Locale
  // factory method.
  return Locale.forLanguageTag(locale).toLanguageTag();
 }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void acceptLanguage() {
  String headerValue = "fr-ch, fr;q=0.9, en-*;q=0.8, de;q=0.7, *;q=0.5";
  headers.setAcceptLanguage(Locale.LanguageRange.parse(headerValue));
  assertEquals(headerValue, headers.getFirst(HttpHeaders.ACCEPT_LANGUAGE));
  List<Locale.LanguageRange> expectedRanges = Arrays.asList(
      new Locale.LanguageRange("fr-ch"),
      new Locale.LanguageRange("fr", 0.9),
      new Locale.LanguageRange("en-*", 0.8),
      new Locale.LanguageRange("de", 0.7),
      new Locale.LanguageRange("*", 0.5)
  );
  assertEquals(expectedRanges, headers.getAcceptLanguage());
  assertEquals(Locale.forLanguageTag("fr-ch"), headers.getAcceptLanguageAsLocales().get(0));
  headers.setAcceptLanguageAsLocales(Collections.singletonList(Locale.FRANCE));
  assertEquals(Locale.FRANCE, headers.getAcceptLanguageAsLocales().get(0));
}

代码示例来源:origin: oblac/jodd

@Test
  void testConversion() {
    LocaleConverter localeConverter = new LocaleConverter();

    assertNull(localeConverter.convert(null));

    assertEquals(new Locale("en"), localeConverter.convert("en"));
    assertEquals(new Locale("en", "US"), localeConverter.convert("en-US"));
    assertEquals(new Locale("en", "US"), localeConverter.convert("en-US-win"));
    assertEquals(Locale.forLanguageTag("ja-JP-x-lvariant-JP"), localeConverter.convert("ja-JP-x-lvariant-JP"));

    assertEquals(new Locale("en"), localeConverter.convert(new Locale("en")));

  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public enum Person { I, YOU, HE, SHE, WE, THEY, IT, UNKNOWN}

相关文章