Skip to content

Commit

Permalink
Fix locale parsing error with en_en, tr_tr, etc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cbeams committed May 17, 2012
1 parent e8f8559 commit f1246a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -674,7 +674,7 @@ public static Locale parseLocaleString(String localeString) {
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();
int endIndexOfCountryCode = localeString.lastIndexOf(country) + country.length();
// Strip off any leading '_' and whitespace, what's left is the variant.
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
if (variant.startsWith("_")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -639,4 +639,11 @@ public void testParseLocaleWithInvalidCharacters() {
}
}

/**
* See SPR-9420.
*/
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
}
}

0 comments on commit f1246a4

Please sign in to comment.