Skip to content

StringUtils#parseLocaleString(String) does not work for empty locale [SPR-6562] #11228

Closed
@spring-projects-issues

Description

@spring-projects-issues

Cédrik LIME opened SPR-6562 and commented

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);
}

Affects: 2.5.6

Issue Links:

1 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions