Locales { public static Locale forName(String localeName) { Args.notEmpty(localeName,"locale name"); int len = localeName.length(); if (len != 2 && len != 5 && len < 7) { throw new IllegalArgumentException("Invalid locale format: " + localeName); } char ch0 = localeName.charAt(0); char ch1 = localeName.charAt(1); if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z') { throw new IllegalArgumentException("Invalid locale format: " + localeName); } if (len == 2) { return new Locale(localeName, ""); } else { if (localeName.charAt(2) != '_') { throw new IllegalArgumentException("Invalid locale format: " + localeName); } char ch3 = localeName.charAt(3); if (ch3 == '_') { return new Locale(localeName.substring(0, 2), "", localeName.substring(4)); } char ch4 = localeName.charAt(4); if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z') { throw new IllegalArgumentException("Invalid locale format: " + localeName); } if (len == 5) { return new Locale(localeName.substring(0, 2), localeName.substring(3, 5)); } else { if (localeName.charAt(5) != '_') { throw new IllegalArgumentException("Invalid locale format: " + localeName); } return new Locale(localeName.substring(0, 2), localeName.substring(3, 5), localeName.substring(6)); } } } protected  Locales(); static String toName(Locale locale); static Locale tryForName(String localName); static Locale forName(String localeName); static String extractFromFilename(String filename); static String[] getLocalePaths(Locale locale, String path); static String[] getLocalePaths(Locale locale, String path,String suffix); static String[] getLocaleFilePaths(Locale locale, String filePath); static final Locale DEFAULT_LOCALE; }
@Test public void testForName(){ Locale locale = Locales.forName("en"); assertNotNull(locale); assertEquals("en", locale.toString()); locale = Locales.forName("zh"); assertNotNull(locale); assertEquals("zh", locale.toString()); locale = Locales.forName("zh_CN"); assertNotNull(locale); assertEquals("zh_CN", locale.toString()); }