| Code with Finding: |
class LocaleUtil {
/**
* Convert a locale string from the RFC 3066 standard format
* to the Java locale format. You can call this on
* any locale string obtained from an external source
* (cookie, URL parameter, header, etc.). This method accepts
* more than just the standard format and will also tolerate
* capitalization discrepancies and the use of an underscore
* in place of a hyphen.
*
* @param s String representation for locale.
* @return Parsed locale.
*/
static Locale getLocaleFromStandardLocaleString(String s) {
if (s == null) {
return null;
}
Matcher matcher = LOCALE_PATTERN.matcher(s);
// LOCALE_PATTERN will match any string, though it may not match the whole
// string. Specifically, it will not match a third _ or - or any
// subsequent text.
matcher.find();
String language = Strings.makeSafe(matcher.group(1));
String country = Strings.makeSafe(matcher.group(2));
String variant = Strings.makeSafe(matcher.group(3));
return new Locale(language, country, variant);
}
}
|