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

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

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

Locale.getAvailableLocales介绍

[英]Returns the system's installed locales. This array always includes Locale.US, and usually several others. Most locale-sensitive classes offer their own getAvailableLocales method, which should be preferred over this general purpose method.
[中]返回系统已安装的区域设置。此数组始终包含区域设置。我们,通常还有其他几个人。大多数对区域设置敏感的类都提供了自己的getAvailableCales方法,与此通用方法相比,这应该是首选方法。

代码示例

代码示例来源:origin: commons-lang/commons-lang

/**
 * Initializes the availableLocaleList. It is separate from availableLocaleList() 
 * to avoid the synchronized block affecting normal use, yet synchronized and 
 * lazy loading to avoid a static block affecting other methods in this class. 
 */
private static synchronized void initAvailableLocaleList() {
  if(cAvailableLocaleList == null) {
    List list = Arrays.asList(Locale.getAvailableLocales());
    cAvailableLocaleList = Collections.unmodifiableList(list);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static String[] getLocaleList() {
  Locale[] locales = Locale.getAvailableLocales();
  String[] strings = new String[locales.length];
  for ( int i = 0; i < strings.length; i++ ) {
   strings[i] = locales[i].toString();
  }
  Arrays.sort( strings );
  return strings;
 }
}

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

/**
 * Checks whether user specified language code is correct (is contained in available locales).
 * @param userSpecifiedLanguageCode user specified language code.
 * @return true if user specified language code is correct.
 */
private static boolean isValidLanguageCode(final String userSpecifiedLanguageCode) {
  boolean valid = false;
  final Locale[] locales = Locale.getAvailableLocales();
  for (Locale locale : locales) {
    if (userSpecifiedLanguageCode.equals(locale.toString())) {
      valid = true;
      break;
    }
  }
  return valid;
}

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

@Generates
private Locale generateLocale() {
 return pickInstance(Locale.getAvailableLocales(), Locale.US);
}

代码示例来源:origin: pentaho/pentaho-kettle

protected void setLocales() {
 Locale[] locale = Locale.getAvailableLocales();
 dateLocale = new String[locale.length];
 for ( int i = 0; i < locale.length; i++ ) {
  dateLocale[i] = locale[i].toString();
 }
 if ( dateLocale != null ) {
  wDateLocale.setItems( dateLocale );
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

protected void setLocales() {
 Locale[] locale = Locale.getAvailableLocales();
 dateLocale = new String[locale.length];
 for ( int i = 0; i < locale.length; i++ ) {
  dateLocale[i] = locale[i].toString();
 }
 if ( dateLocale != null ) {
  wDateLocale.setItems( dateLocale );
 }
}

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

private static Locale getFixedLocale() {
    final String locale = Parameter.LOCALE.getValue();
    if (locale != null) {
      for (final Locale l : Locale.getAvailableLocales()) {
        if (l.toString().equals(locale)) {
          return l;
        }
      }
    }
    return null;
  }
}

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

@GET
@ApiOperation(value = "Get supported locales")
@Path("/locales")
@Timed
public LocalesResponse locales() {
  return LocalesResponse.create(Locale.getAvailableLocales());
}

代码示例来源:origin: jenkinsci/jenkins

if (pv instanceof AllView && AllView.DEFAULT_VIEW_NAME.equals(pv.name)) {
  for (Locale l : Locale.getAvailableLocales()) {
    if (name.equals(Messages._Hudson_ViewName().toString(l))) {

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

@Test  // SPR-16651
public void testAvailableLocalesWithLocaleString() {
  for (Locale locale : Locale.getAvailableLocales()) {
    Locale parsedLocale = StringUtils.parseLocaleString(locale.toString());
    if (parsedLocale == null) {
      assertEquals("", locale.getLanguage());
    }
    else {
      assertEquals(parsedLocale.toString(), locale.toString());
    }
  }
}

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

@Test  // SPR-16651
public void testAvailableLocalesWithLanguageTag() {
  for (Locale locale : Locale.getAvailableLocales()) {
    Locale parsedLocale = StringUtils.parseLocale(locale.toLanguageTag());
    if (parsedLocale == null) {
      assertEquals("", locale.getLanguage());
    }
    else {
      assertEquals(parsedLocale.toLanguageTag(), locale.toLanguageTag());
    }
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

@Override public Locale getTestObject() {
 Locale[] availableLocales = Locale.getAvailableLocales();
 Locale random = availableLocales[ new Random().nextInt( availableLocales.length ) ];
 if ( Utils.isEmpty( random.toString() ) || random.toString().matches( "(\\w)*#.*" ) ) {
  // locales with '#', like 'sr_rs_#latn', are not restored properly
  return Locale.US;
 } else {
  return random;
 }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testTzParses() throws Exception {
  // Check that all Locales can parse the time formats we use
  for(final Locale locale : Locale.getAvailableLocales()) {
    final FastDateParser fdp= new FastDateParser("yyyy/MM/dd z", TimeZone.getDefault(), locale);
    for(final TimeZone tz :  new TimeZone[]{NEW_YORK, REYKJAVIK, GMT}) {
      final Calendar cal= Calendar.getInstance(tz, locale);
      cal.clear();
      cal.set(Calendar.YEAR, 2000);
      cal.set(Calendar.MONTH, 1);
      cal.set(Calendar.DAY_OF_MONTH, 10);
      final Date expected= cal.getTime();
      final Date actual = fdp.parse("2000/02/10 "+tz.getDisplayName(locale));
      assertEquals("tz:"+tz.getID()+" locale:"+locale.getDisplayName(), expected, actual);
    }
  }
}

代码示例来源:origin: commons-io/commons-io

@Test
public void testLocaleIndependence() {
  final Locale orig = Locale.getDefault();
  final Locale[] locales = Locale.getAvailableLocales();
  final String[][] data = {
    { "I", "i"},
    { "i", "I"},
    { "i", "\u0130"},
    { "i", "\u0131"},
    { "\u03A3", "\u03C2"},
    { "\u03A3", "\u03C3"},
    { "\u03C2", "\u03C3"},
  };
  try {
    for (int i = 0; i < data.length; i++) {
      for (final Locale locale : locales) {
        Locale.setDefault(locale);
        assertTrue("Test data corrupt: " + i, data[i][0].equalsIgnoreCase(data[i][1]));
        final boolean match = FilenameUtils.wildcardMatch(data[i][0], data[i][1], IOCase.INSENSITIVE);
        assertTrue(Locale.getDefault().toString() + ": " + i, match);
      }
    }
  } finally {
    Locale.setDefault(orig);
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
// Check that all Locales can parse the formats we use
public void testParses() throws Exception {
  for(final String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) {
    for(final Locale locale : Locale.getAvailableLocales()) {
      for(final TimeZone tz :  new TimeZone[]{NEW_YORK, REYKJAVIK, GMT}) {
         for(final int year : new int[]{2003, 1940, 1868, 1867, 1, -1, -1940}) {
          final Calendar cal= getEraStart(year, tz, locale);
          final Date centuryStart= cal.getTime();
          cal.set(Calendar.MONTH, 1);
          cal.set(Calendar.DAY_OF_MONTH, 10);
          final Date in= cal.getTime();
          final FastDateParser fdp= new FastDateParser(format, tz, locale, centuryStart);
          validateSdfFormatFdpParseEquality(format, locale, tz, fdp, in, year, centuryStart);
        }
      }
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

private void testLocales(final String format, final boolean eraBC) throws Exception {
  final Calendar cal= Calendar.getInstance(GMT);
  cal.clear();
  cal.set(2003, Calendar.FEBRUARY, 10);
  if (eraBC) {
    cal.set(Calendar.ERA, GregorianCalendar.BC);
  }
  for(final Locale locale : Locale.getAvailableLocales() ) {
    // ja_JP_JP cannot handle dates before 1868 properly
    if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
      continue;
    }
    final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
    final DateParser fdf = getInstance(format, locale);
    try {
      checkParse(locale, cal, sdf, fdf);
    } catch(final ParseException ex) {
      fail("Locale "+locale+ " failed with "+format+" era "+(eraBC?"BC":"AD")+"\n" + trimMessage(ex.toString()));
    }
  }
}

代码示例来源:origin: jphp-group/jphp

@Signature
public static Memory getAvailableLocales(Environment env, Memory... args) {
  ArrayMemory r = new ArrayMemory();
  for(Locale el : Locale.getAvailableLocales()) {
    r.add(new WrapLocale(env, el));
  }
  return r.toConstant();
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testTimeZoneStrategyPattern() {
  for(final Locale locale : Locale.getAvailableLocales()) {
    final FastDateParser parser = new FastDateParser("z", TimeZone.getDefault(), locale);
    final String[][] zones = DateFormatSymbols.getInstance(locale).getZoneStrings();
    for(final String[] zone :  zones) {
      for(int t = 1; t<zone.length; ++t) {
        final String tzDisplay = zone[t];
        if (tzDisplay == null) {
          break;
        }
        try {
          parser.parse(tzDisplay);
        } catch(final Exception ex) {
          fail("'" + tzDisplay + "'"
              + " Locale: '" + locale.getDisplayName() + "'"
              + " TimeZone: " + zone[0]
              + " offset: " + t
              + " defaultLocale: " + Locale.getDefault()
              + " defaultTimeZone: " + TimeZone.getDefault().getDisplayName()
              );
        }
      }
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Test availableLocaleSet() method.
 */
@Test
public void testAvailableLocaleSet() {
  final Set<Locale> set = LocaleUtils.availableLocaleSet();
  final Set<Locale> set2 = LocaleUtils.availableLocaleSet();
  assertNotNull(set);
  assertSame(set, set2);
  assertUnmodifiableCollection(set);
  final Locale[] jdkLocaleArray = Locale.getAvailableLocales();
  final List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
  final Set<Locale> jdkLocaleSet = new HashSet<>(jdkLocaleList);
  assertEquals(jdkLocaleSet, set);
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Test availableLocaleList() method.
 */
@Test
public void testAvailableLocaleList() {
  final List<Locale> list = LocaleUtils.availableLocaleList();
  final List<Locale> list2 = LocaleUtils.availableLocaleList();
  assertNotNull(list);
  assertSame(list, list2);
  assertUnmodifiableCollection(list);
  final Locale[] jdkLocaleArray = Locale.getAvailableLocales();
  final List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
  assertEquals(jdkLocaleList, list);
}

相关文章