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

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

本文整理了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

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

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

  1. public static String[] getLocaleList() {
  2. Locale[] locales = Locale.getAvailableLocales();
  3. String[] strings = new String[locales.length];
  4. for ( int i = 0; i < strings.length; i++ ) {
  5. strings[i] = locales[i].toString();
  6. }
  7. Arrays.sort( strings );
  8. return strings;
  9. }
  10. }

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

  1. /**
  2. * Checks whether user specified language code is correct (is contained in available locales).
  3. * @param userSpecifiedLanguageCode user specified language code.
  4. * @return true if user specified language code is correct.
  5. */
  6. private static boolean isValidLanguageCode(final String userSpecifiedLanguageCode) {
  7. boolean valid = false;
  8. final Locale[] locales = Locale.getAvailableLocales();
  9. for (Locale locale : locales) {
  10. if (userSpecifiedLanguageCode.equals(locale.toString())) {
  11. valid = true;
  12. break;
  13. }
  14. }
  15. return valid;
  16. }

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

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

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

  1. protected void setLocales() {
  2. Locale[] locale = Locale.getAvailableLocales();
  3. dateLocale = new String[locale.length];
  4. for ( int i = 0; i < locale.length; i++ ) {
  5. dateLocale[i] = locale[i].toString();
  6. }
  7. if ( dateLocale != null ) {
  8. wDateLocale.setItems( dateLocale );
  9. }
  10. }

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

  1. protected void setLocales() {
  2. Locale[] locale = Locale.getAvailableLocales();
  3. dateLocale = new String[locale.length];
  4. for ( int i = 0; i < locale.length; i++ ) {
  5. dateLocale[i] = locale[i].toString();
  6. }
  7. if ( dateLocale != null ) {
  8. wDateLocale.setItems( dateLocale );
  9. }
  10. }

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

  1. private static Locale getFixedLocale() {
  2. final String locale = Parameter.LOCALE.getValue();
  3. if (locale != null) {
  4. for (final Locale l : Locale.getAvailableLocales()) {
  5. if (l.toString().equals(locale)) {
  6. return l;
  7. }
  8. }
  9. }
  10. return null;
  11. }
  12. }

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

  1. @GET
  2. @ApiOperation(value = "Get supported locales")
  3. @Path("/locales")
  4. @Timed
  5. public LocalesResponse locales() {
  6. return LocalesResponse.create(Locale.getAvailableLocales());
  7. }

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

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

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

  1. @Test // SPR-16651
  2. public void testAvailableLocalesWithLocaleString() {
  3. for (Locale locale : Locale.getAvailableLocales()) {
  4. Locale parsedLocale = StringUtils.parseLocaleString(locale.toString());
  5. if (parsedLocale == null) {
  6. assertEquals("", locale.getLanguage());
  7. }
  8. else {
  9. assertEquals(parsedLocale.toString(), locale.toString());
  10. }
  11. }
  12. }

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

  1. @Test // SPR-16651
  2. public void testAvailableLocalesWithLanguageTag() {
  3. for (Locale locale : Locale.getAvailableLocales()) {
  4. Locale parsedLocale = StringUtils.parseLocale(locale.toLanguageTag());
  5. if (parsedLocale == null) {
  6. assertEquals("", locale.getLanguage());
  7. }
  8. else {
  9. assertEquals(parsedLocale.toLanguageTag(), locale.toLanguageTag());
  10. }
  11. }
  12. }

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

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

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

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

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

  1. @Test
  2. public void testLocaleIndependence() {
  3. final Locale orig = Locale.getDefault();
  4. final Locale[] locales = Locale.getAvailableLocales();
  5. final String[][] data = {
  6. { "I", "i"},
  7. { "i", "I"},
  8. { "i", "\u0130"},
  9. { "i", "\u0131"},
  10. { "\u03A3", "\u03C2"},
  11. { "\u03A3", "\u03C3"},
  12. { "\u03C2", "\u03C3"},
  13. };
  14. try {
  15. for (int i = 0; i < data.length; i++) {
  16. for (final Locale locale : locales) {
  17. Locale.setDefault(locale);
  18. assertTrue("Test data corrupt: " + i, data[i][0].equalsIgnoreCase(data[i][1]));
  19. final boolean match = FilenameUtils.wildcardMatch(data[i][0], data[i][1], IOCase.INSENSITIVE);
  20. assertTrue(Locale.getDefault().toString() + ": " + i, match);
  21. }
  22. }
  23. } finally {
  24. Locale.setDefault(orig);
  25. }
  26. }

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

  1. @Test
  2. // Check that all Locales can parse the formats we use
  3. public void testParses() throws Exception {
  4. for(final String format : new String[]{LONG_FORMAT, SHORT_FORMAT}) {
  5. for(final Locale locale : Locale.getAvailableLocales()) {
  6. for(final TimeZone tz : new TimeZone[]{NEW_YORK, REYKJAVIK, GMT}) {
  7. for(final int year : new int[]{2003, 1940, 1868, 1867, 1, -1, -1940}) {
  8. final Calendar cal= getEraStart(year, tz, locale);
  9. final Date centuryStart= cal.getTime();
  10. cal.set(Calendar.MONTH, 1);
  11. cal.set(Calendar.DAY_OF_MONTH, 10);
  12. final Date in= cal.getTime();
  13. final FastDateParser fdp= new FastDateParser(format, tz, locale, centuryStart);
  14. validateSdfFormatFdpParseEquality(format, locale, tz, fdp, in, year, centuryStart);
  15. }
  16. }
  17. }
  18. }
  19. }

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

  1. private void testLocales(final String format, final boolean eraBC) throws Exception {
  2. final Calendar cal= Calendar.getInstance(GMT);
  3. cal.clear();
  4. cal.set(2003, Calendar.FEBRUARY, 10);
  5. if (eraBC) {
  6. cal.set(Calendar.ERA, GregorianCalendar.BC);
  7. }
  8. for(final Locale locale : Locale.getAvailableLocales() ) {
  9. // ja_JP_JP cannot handle dates before 1868 properly
  10. if (eraBC && locale.equals(FastDateParser.JAPANESE_IMPERIAL)) {
  11. continue;
  12. }
  13. final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
  14. final DateParser fdf = getInstance(format, locale);
  15. try {
  16. checkParse(locale, cal, sdf, fdf);
  17. } catch(final ParseException ex) {
  18. fail("Locale "+locale+ " failed with "+format+" era "+(eraBC?"BC":"AD")+"\n" + trimMessage(ex.toString()));
  19. }
  20. }
  21. }

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

  1. @Signature
  2. public static Memory getAvailableLocales(Environment env, Memory... args) {
  3. ArrayMemory r = new ArrayMemory();
  4. for(Locale el : Locale.getAvailableLocales()) {
  5. r.add(new WrapLocale(env, el));
  6. }
  7. return r.toConstant();
  8. }

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

  1. @Test
  2. public void testTimeZoneStrategyPattern() {
  3. for(final Locale locale : Locale.getAvailableLocales()) {
  4. final FastDateParser parser = new FastDateParser("z", TimeZone.getDefault(), locale);
  5. final String[][] zones = DateFormatSymbols.getInstance(locale).getZoneStrings();
  6. for(final String[] zone : zones) {
  7. for(int t = 1; t<zone.length; ++t) {
  8. final String tzDisplay = zone[t];
  9. if (tzDisplay == null) {
  10. break;
  11. }
  12. try {
  13. parser.parse(tzDisplay);
  14. } catch(final Exception ex) {
  15. fail("'" + tzDisplay + "'"
  16. + " Locale: '" + locale.getDisplayName() + "'"
  17. + " TimeZone: " + zone[0]
  18. + " offset: " + t
  19. + " defaultLocale: " + Locale.getDefault()
  20. + " defaultTimeZone: " + TimeZone.getDefault().getDisplayName()
  21. );
  22. }
  23. }
  24. }
  25. }
  26. }

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

  1. /**
  2. * Test availableLocaleSet() method.
  3. */
  4. @Test
  5. public void testAvailableLocaleSet() {
  6. final Set<Locale> set = LocaleUtils.availableLocaleSet();
  7. final Set<Locale> set2 = LocaleUtils.availableLocaleSet();
  8. assertNotNull(set);
  9. assertSame(set, set2);
  10. assertUnmodifiableCollection(set);
  11. final Locale[] jdkLocaleArray = Locale.getAvailableLocales();
  12. final List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
  13. final Set<Locale> jdkLocaleSet = new HashSet<>(jdkLocaleList);
  14. assertEquals(jdkLocaleSet, set);
  15. }

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

  1. /**
  2. * Test availableLocaleList() method.
  3. */
  4. @Test
  5. public void testAvailableLocaleList() {
  6. final List<Locale> list = LocaleUtils.availableLocaleList();
  7. final List<Locale> list2 = LocaleUtils.availableLocaleList();
  8. assertNotNull(list);
  9. assertSame(list, list2);
  10. assertUnmodifiableCollection(list);
  11. final Locale[] jdkLocaleArray = Locale.getAvailableLocales();
  12. final List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
  13. assertEquals(jdkLocaleList, list);
  14. }

相关文章