Skip to content

Commit f1246a4

Browse files
committed
Fix locale parsing error with en_en, tr_tr, etc
Previously, StringUtils#parseLocaleString would parse locale strings having the same lowercase token for both language and country incorrectly, e.g. 'tr_tr' would parse to 'tr_TR_tr' as opposed to the expected 'tr_TR'. This commit fixes this behavior by using using String#lastIndexOf instead of String#indexOf when determining the location of the country code token. Issue: SPR-9420
1 parent e8f8559 commit f1246a4

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -674,7 +674,7 @@ public static Locale parseLocaleString(String localeString) {
674674
if (parts.length >= 2) {
675675
// There is definitely a variant, and it is everything after the country
676676
// code sans the separator between the country code and the variant.
677-
int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
677+
int endIndexOfCountryCode = localeString.lastIndexOf(country) + country.length();
678678
// Strip off any leading '_' and whitespace, what's left is the variant.
679679
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
680680
if (variant.startsWith("_")) {

spring-core/src/test/java/org/springframework/util/StringUtilsTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -639,4 +639,11 @@ public void testParseLocaleWithInvalidCharacters() {
639639
}
640640
}
641641

642+
/**
643+
* See SPR-9420.
644+
*/
645+
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
646+
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
647+
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
648+
}
642649
}

0 commit comments

Comments
 (0)