Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Maven
<groupId>com.neovisionaries</groupId>
<artifactId>nv-i18n</artifactId>
<version>1.2</version>
</depenency>
</dependency>


See Also
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/com/neovisionaries/i18n/LocaleCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*/
Copy link

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.

public static LocaleCode getByCountry(String country, boolean caseSensitive) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also create a

getByCountry(String country) 

which delegates to

getByCountry(country, false) . 

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)
Expand Down
272 changes: 272 additions & 0 deletions src/test/java/com/neovisionaries/i18n/LocaleCodeTest.java
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);
}
}