-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added useful method to get LocaleCode by country #2
Open
cardil
wants to merge
3
commits into
TakahikoKawasaki:master
Choose a base branch
from
wavesoftware:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1184,8 +1184,7 @@ public static LocaleCode getByCode(String language, String country, boolean case | |
|
||
if (language == null) | ||
{ | ||
// There is no LocaleCode whose language is not given. | ||
return null; | ||
return getByCountry(country, caseSensitive); | ||
} | ||
|
||
// Canonicalize the given country code. | ||
|
@@ -1232,6 +1231,38 @@ public static LocaleCode getByLocale(Locale locale) | |
// so true can be given as the third argument. | ||
return getByCode(language, country, true); | ||
} | ||
|
||
/** | ||
* Get a LocaleCode instance that corresponds to the given | ||
* country code | ||
* | ||
* @param country | ||
* <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2" | ||
* >ISO 3166-1 alpha-2</a> country code. | ||
* | ||
* @param caseSensitive | ||
* If true, the given language code must be lower-case and | ||
* the given country code, if not null, must be upper-case. | ||
* If false, this method internally canonicalizes the given | ||
* codes and then performs search. | ||
* @return | ||
* A LocaleCode instance, or null if not found. | ||
*/ | ||
public static LocaleCode getByCountry(String country, boolean caseSensitive) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also create a
which delegates to
Same as what is done with the getByCode methods. |
||
String search; | ||
if (caseSensitive) { | ||
search = country; | ||
} else { | ||
search = country.toUpperCase(); | ||
} | ||
for (LocaleCode localeCode : values()) { | ||
// finds first pair | ||
if (localeCode.getCountry() != null && localeCode.getCountry().name().equals(search)) { | ||
return localeCode; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
private static LocaleCode getByCode5(String code, boolean caseSensitive) | ||
|
272 changes: 272 additions & 0 deletions
272
src/test/java/com/neovisionaries/i18n/LocaleCodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
/* | ||
* Copyright 2013 Krzysztof Suszyński <[email protected]>. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.neovisionaries.i18n; | ||
|
||
import java.util.Locale; | ||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author Krzysztof Suszyński <[email protected]> | ||
*/ | ||
public class LocaleCodeTest { | ||
|
||
/** | ||
* Test of values method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testValues() { | ||
int expResult = 150; | ||
LocaleCode[] result = LocaleCode.values(); | ||
assertEquals(expResult, result.length); | ||
} | ||
|
||
/** | ||
* Test of valueOf method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testValueOf() { | ||
String name = "pl_PL"; | ||
LocaleCode expResult = LocaleCode.pl_PL; | ||
LocaleCode result = LocaleCode.valueOf(name); | ||
assertEquals(expResult, result); | ||
name = "pl"; | ||
expResult = LocaleCode.pl; | ||
result = LocaleCode.valueOf(name); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getLanguage method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetLanguage() { | ||
LocaleCode instance = LocaleCode.pl_PL; | ||
LanguageCode expResult = LanguageCode.pl; | ||
LanguageCode result = instance.getLanguage(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.pl; | ||
expResult = LanguageCode.pl; | ||
result = instance.getLanguage(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.ja; | ||
expResult = LanguageCode.ja; | ||
result = instance.getLanguage(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.ja_JP; | ||
expResult = LanguageCode.ja; | ||
result = instance.getLanguage(); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getCountry method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetCountry() { | ||
LocaleCode instance = LocaleCode.pl_PL; | ||
CountryCode expResult = CountryCode.PL; | ||
CountryCode result = instance.getCountry(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.ja_JP; | ||
expResult = CountryCode.JP; | ||
result = instance.getCountry(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.ja; | ||
expResult = null; | ||
result = instance.getCountry(); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of toString method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testToString() { | ||
LocaleCode instance = LocaleCode.pl_PL; | ||
String expResult = "pl-PL"; | ||
String result = instance.toString(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.pl; | ||
expResult = "pl"; | ||
result = instance.toString(); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of toLocale method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testToLocale() { | ||
LocaleCode instance = LocaleCode.pl_PL; | ||
Locale expResult = new Locale("pl", "PL"); | ||
Locale result = instance.toLocale(); | ||
assertEquals(expResult, result); | ||
instance = LocaleCode.pl; | ||
expResult = new Locale("pl"); | ||
result = instance.toLocale(); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getByCode method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetByCode_String() { | ||
String code = ""; | ||
LocaleCode expResult = null; | ||
LocaleCode result = LocaleCode.getByCode(code); | ||
assertEquals(expResult, result); | ||
code = "ja_JP"; | ||
expResult = LocaleCode.ja_JP; | ||
result = LocaleCode.getByCode(code); | ||
assertEquals(expResult, result); | ||
code = "ja"; | ||
expResult = LocaleCode.ja; | ||
result = LocaleCode.getByCode(code); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getByCode method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetByCode_String_boolean() { | ||
String code = ""; | ||
boolean caseSensitive = false; | ||
LocaleCode expResult = null; | ||
LocaleCode result = LocaleCode.getByCode(code, caseSensitive); | ||
assertEquals(expResult, result); | ||
code = "ja_JP"; | ||
caseSensitive = false; | ||
expResult = LocaleCode.ja_JP; | ||
result = LocaleCode.getByCode(code, caseSensitive); | ||
assertEquals(expResult, result); | ||
code = "ja"; | ||
caseSensitive = false; | ||
expResult = LocaleCode.ja; | ||
result = LocaleCode.getByCode(code, caseSensitive); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getByCode method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetByCode_String_String() { | ||
String language = ""; | ||
String country = ""; | ||
LocaleCode expResult = null; | ||
LocaleCode result = LocaleCode.getByCode(language, country); | ||
assertEquals(expResult, result); | ||
language = "zh"; | ||
country = "TW"; | ||
expResult = LocaleCode.zh_TW; | ||
result = LocaleCode.getByCode(language, country); | ||
assertEquals(expResult, result); | ||
language = ""; | ||
country = "TW"; | ||
expResult = LocaleCode.zh_TW; | ||
result = LocaleCode.getByCode(language, country); | ||
assertEquals(expResult, result); | ||
language = ""; | ||
country = "pl"; | ||
expResult = LocaleCode.pl_PL; | ||
result = LocaleCode.getByCode(language, country); | ||
assertEquals(expResult, result); | ||
language = "ja"; | ||
country = ""; | ||
expResult = LocaleCode.ja; | ||
result = LocaleCode.getByCode(language, country); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getByCode method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetByCode_3args() { | ||
String language = ""; | ||
String country = ""; | ||
boolean caseSensitive = false; | ||
LocaleCode expResult = null; | ||
LocaleCode result = LocaleCode.getByCode(language, country, caseSensitive); | ||
assertEquals(expResult, result); | ||
language = "zh"; | ||
country = "TW"; | ||
caseSensitive = true; | ||
expResult = LocaleCode.zh_TW; | ||
result = LocaleCode.getByCode(language, country, caseSensitive); | ||
assertEquals(expResult, result); | ||
language = ""; | ||
country = "TW"; | ||
caseSensitive = true; | ||
expResult = LocaleCode.zh_TW; | ||
result = LocaleCode.getByCode(language, country, caseSensitive); | ||
assertEquals(expResult, result); | ||
language = ""; | ||
country = "pl"; | ||
caseSensitive = false; | ||
expResult = LocaleCode.pl_PL; | ||
result = LocaleCode.getByCode(language, country, caseSensitive); | ||
assertEquals(expResult, result); | ||
language = "ja"; | ||
country = ""; | ||
caseSensitive = false; | ||
expResult = LocaleCode.ja; | ||
result = LocaleCode.getByCode(language, country, caseSensitive); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getByLocale method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetByLocale() { | ||
Locale locale = Locale.CANADA_FRENCH; | ||
LocaleCode expResult = LocaleCode.fr_CA; | ||
LocaleCode result = LocaleCode.getByLocale(locale); | ||
assertEquals(expResult, result); | ||
locale = Locale.CHINA; | ||
expResult = LocaleCode.zh_CN; | ||
result = LocaleCode.getByLocale(locale); | ||
assertEquals(expResult, result); | ||
locale = Locale.JAPAN; | ||
expResult = LocaleCode.ja_JP; | ||
result = LocaleCode.getByLocale(locale); | ||
assertEquals(expResult, result); | ||
locale = Locale.JAPANESE; | ||
expResult = LocaleCode.ja; | ||
result = LocaleCode.getByLocale(locale); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
/** | ||
* Test of getByCountry method, of class LocaleCode. | ||
*/ | ||
@Test | ||
public void testGetByCountry() { | ||
String country = "pl"; | ||
LocaleCode expResult = LocaleCode.pl_PL; | ||
LocaleCode result = LocaleCode.getByCountry(country, false); | ||
assertEquals(expResult, result); | ||
result = LocaleCode.getByCountry(country, true); | ||
assertNull(result); | ||
result = LocaleCode.getByCountry("JP", true); | ||
assertSame(LocaleCode.ja_JP, result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The javadoc is not correct.
In the param country and caseSensitive you say its a country code, whereas it should be country name.