You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
StringUtils#parseLocaleString(String) does not work for empty locale [SPR-6562] #11228
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
StringUtils#parseLocaleString(String) is supposed to be the exact inverse operation to Locale.toString().
This is not the case for new Locale("", "", ""), which String representation is "", but which comes back as null from StringUtils.parseLocaleString("").
The parseLocaleString() method should be:
public static Locale parseLocaleString(String localeString) {
if (localeString == null) {
return null;
}
String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
String language = (parts.length > 0 ? parts[0] : "");
String country = (parts.length > 1 ? parts[1] : "");
String variant = "";
if (parts.length >= 2) {
// There is definitely a variant, and it is everything after the country
// code sans the separator between the country code and the variant.
int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
// Strip off any leading '_' and whitespace, what's left is the variant.
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
if (variant.startsWith("_")) {
variant = trimLeadingCharacter(variant, '_');
}
}
return new Locale(language, country, variant);
}
will print 'false' ("VAR" will become language code after parsing) and it turns out that this is not a reverse function to java.util.Locale.toString();
I think this function must be completely reviewed.
As part of the current overhaul of the related StringUtils.parseLocale in #21192, it turns out that we do want to retain the null semantics for an empty locale, in particular since "empty" may also mean "invalid" in Locale.forLanguageTag which we're using by default now.
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Activity
spring-projects-issues commentedon Feb 15, 2010
Ivan commented
This function also doesn't correctly parses locales without country code.
For example code
will print 'false' ("VAR" will become language code after parsing) and it turns out that this is not a reverse function to java.util.Locale.toString();
I think this function must be completely reviewed.
spring-projects-issues commentedon Sep 28, 2010
Antonio Mota commented
Yes, same here, I just solved this by using my own parseLocaleString
doesn't work with ' ' as separator though but is easy to change it.
spring-projects-issues commentedon Sep 28, 2010
Antonio Mota commented
Sorry about the mess...
Maybe this should be enhanced to conform to the spec, that doesn't allow a variant without Country or Language.
spring-projects-issues commentedon Sep 28, 2010
Antonio Mota commented
I just opened a new Jira, #12254 about this problem with the Variant with no Language or Country.
spring-projects-issues commentedon Feb 6, 2012
David Mas commented
Related : https://jira.springsource.org/browse/SPR-8099
spring-projects-issues commentedon Jul 17, 2018
Juergen Hoeller commented
As part of the current overhaul of the related
StringUtils.parseLocalein #21192, it turns out that we do want to retain thenullsemantics for an empty locale, in particular since "empty" may also mean "invalid" inLocale.forLanguageTagwhich we're using by default now.