Skip to content
Merged
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
41 changes: 30 additions & 11 deletions src/main/java/net/datafaker/providers/base/PhoneNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,48 @@ private static String countryCodeIso2(Locale locale) {
/**
* A hack to detect country when only a language is given.
* <p>
* It's not correct because most languages are used in multiple countries.
* If users need to generate random phone number, they should create locale with country,
* e.g. {@code new Locale("ta_IN")}, and not just {@code new Locale("ta")}.
* It's not correct because most languages are used in multiple countries.
* If users need to generate random phone number, they should create locale with country,
* e.g. {@code new Locale("ta_IN")}, and not just {@code new Locale("ta")}.
* </p>
* <p>
* We keep this mapping here just for backward compatibility.
* We keep this mapping here just for backward compatibility.
* </p>
*/
private static String detectCountryByLanguage(String language) {
return switch (language) {
case "af" -> "ZA"; // Afrikaans language -> South Africa
case "ar" -> "SA"; // Arabic language -> Saudi Arabia (SA)
case "am" -> "ET"; // Amharic language -> Ethiopia (ET)
case "be" -> "BY"; // Belarus
case "bn" -> "BD"; // Bengali language -> Bangladesh (BD)
case "bs" -> "BA"; // Bosnian language -> Bosnia & Herzegovina (BA)
case "ca" -> "ES"; // Catalan language -> Spain (ES)
case "cy" -> "GB"; // Welsh language -> United Kingdom (GB)
case "cs" -> "CZ"; // Czech Republic
case "el" -> "GR"; // Greece
case "et" -> "EE"; // Estonian language -> Estonia (EE)
case "en" -> "US"; // it has been used by default for English
case "test" -> "US"; // What the hell is "test" language?
case "eu" -> "ES"; // Basque (Basque Country | Spain)
case "fa" -> "IR"; // Persian language (Farsi) -> Iran (IR)
case "ga" -> "IE"; // Irish/Gaelic language -> Ireland (IE)
case "gl" -> "ES"; // Galician (Spain)
case "he" -> "IL"; // Israel
case "hi" -> "IN"; // Hindi language -> India
case "hy" -> "AM"; // Armenia
case "uk" -> "UA"; // Ukraine
case "ja" -> "JP"; // Japan
case "fa" -> "IR"; // Iran
case "ka" -> "GE"; // Georgia
case "sq" -> "AL"; // Albania
case "cs" -> "CZ"; // Czech Republic
case "be" -> "BY"; // Belarus
case "km" -> "KH"; // Khmer language -> Cambodia (KH)
case "ko" -> "KR"; // Korea
case "he" -> "IL"; // Israel
case "mo" -> "MD"; // Moldavian language -> Moldova
case "sq" -> "AL"; // Albania
case "sw" -> "TZ"; // Swahili language -> Tanzania (TZ)
case "ug" -> "CN"; // Uyghur language -> China (CN)
case "ur" -> "PK"; // Urdu language -> Pakistan (PK)
case "ta" -> "IN"; // Tamil language -> India (though, Tamil is used in multiple countries)
case "test" -> "US"; // What the hell is "test" language?
case "uk" -> "UA"; // Ukraine
case "zh" -> "CN"; // Chinese language -> China (CN)
default -> language.toUpperCase(ROOT);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Locale;
Expand Down Expand Up @@ -53,6 +54,42 @@ void testLanguageOnlyPersianLocaleUsesIran() {
assertThat(localFaker.phoneNumber().countryCodeIso2()).isEqualTo("IR");
}

@ParameterizedTest
@CsvSource({
"en, US, English",
"hy, AM, Armenian → Armenia",
"uk, UA, Ukranian → Ukraine",
"ja, JP, Japanese → Japan",
"fa, IR, Persian (Farsi) → Iran",
"ka, GE, Georgia",
"sq, AL, Albanian → Albania",
"cs, CZ, Czech",
"be, BY, Belarusian",
"he, IL, Hebrew → Israel",
"ta, IN, Tamil → India",
"et, EE, Estonian → Estonia",
"el, GR, Greek → Greece",
"eu, ES, Basque → Spain",
"ca, ES, Catalan → Spain",
"cy, GB, Welsh → United Kingdom",
"ga, IE, Irish/Gaelic → Ireland",
"is, IS, Icelandic → Iceland",
"bs, BA, Bosnian → Bosnia & Herzegovina",
"ar, SA, Arabic → Saudi Arabia",
"hi, IN, Hindi → India",
"zh, CN, Chinese → China",
"am, ET, Amharic → Ethiopia",
"sw, TZ, Swahili → Tanzania",
"af, ZA, Afrikaans → South Africa",
})
void detectsCountryByLanguage(String language, String expectedCountry, String description) {
BaseFaker localFaker = new BaseFaker(new Locale(language));

assertThat(localFaker.phoneNumber().countryCodeIso2())
.as(description)
.isEqualTo(expectedCountry);
}

@ParameterizedTest
@MethodSource("allSupportedLocales")
void testAllPhoneNumbers(Locale supportedLocale) throws NumberParseException {
Expand Down
Loading