Skip to content

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

Closed

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

Activity

  1. spring-projects-issues commented on Feb 15, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Ivan commented

    This function also doesn't correctly parses locales without country code.
    For example code

    Locale locale0 = new Locale("en", "", "var");
    Locale locale = org.springframework.util.StringUtils.parseLocaleString(locale0.toString()); //parsing "en__var"
    System.out.println(locale.equals(locale0));
    

    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.

  2. spring-projects-issues commented on Sep 28, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Antonio Mota commented

    Yes, same here, I just solved this by using my own parseLocaleString

    private Locale parseLocaleString(String localeString) {
    	String[] parts = localeString.split("_",3);
    	String language = (parts.length > 0 ? parts[0] : "");
    	String country = (parts.length > 1 ? parts[1] : "");
    	String variant = (parts.length > 2 ? parts[2] : "");
    	return new Locale(language, country, variant);
    }
    

    doesn't work with ' ' as separator though but is easy to change it.

  3. spring-projects-issues commented on Sep 28, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Antonio Mota commented

    Sorry about the mess...

     
    	private Locale parseLocaleString(String localeString) {
    		String[] parts = localeString.split("_",3);
    		String language = (parts.length > 0 ? parts[0] : "");
    		String country = (parts.length > 1 ? parts[1] : "");
    		String variant = (parts.length > 2 ? parts[2] : "");
    		return new Locale(language, country, variant);
    	}
    
    

    Maybe this should be enhanced to conform to the spec, that doesn't allow a variant without Country or Language.

  4. spring-projects-issues commented on Sep 28, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Antonio Mota commented

    I just opened a new Jira, #12254 about this problem with the Variant with no Language or Country.

  5. spring-projects-issues commented on Jul 17, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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.

  6. added
    status: declined
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: declinedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions