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

Make locale use more consistent with the Java Locale API #266

Closed
abeshenov opened this issue Jul 30, 2022 · 1 comment
Closed

Make locale use more consistent with the Java Locale API #266

abeshenov opened this issue Jul 30, 2022 · 1 comment

Comments

@abeshenov
Copy link

abeshenov commented Jul 30, 2022

The use of locales as suggested by README.md seems to be inconsistent with the Java Locale API. Namely, it gives as an example

new Faker(new Locale("en-us")).address().zipCodeByState("CA"));

Here Locale("en-us") is an incorrect locale with language en-us. This is not the same as Locale.US, which is Locale("en", "US"), i.e. language en and country US.

It seems like Faker normalizes the incorrect locales:

var locale = new Locale("en-us");  // Incorrect locale
var faker = new Faker(locale);

faker.getLocale();  // The correct Locale("en", "US")

This behavior is not reflected in the javadoc. Probably the documentation in README.md could give as an example one of these two:

new Faker(Locale.US).address().zipCodeByState("CA");
// OR
new Faker(new Locale("en", "US")).address().zipCodeByState("CA");

and the list of supported locales could follow the same format as used by Locale::toString, with underscores _ instead of hyphens -:

- ar
- bg
- ca
- ca_CAT
- cs
- da_DK
. . . . .

Another inconsistency with the Java Locale API that I noticed is that java.util.Locale uses ISO 3166 alpha-2 for country codes, and datafaker doesn't. Not everything supported by datafaker can be mapped to ISO 3166 alpha-2, but probably datafaker could recognize ISO 3166 alpha-2 codes whenever possible:

var fakerNp = new Faker(new Locale("en", "NP"));
var fakerNep = new Faker(new Locale("en", "NEP"));

fakerNp.getLocale().getDisplayCountry();  // "Nepal"
fakerNep.getLocale().getDisplayCountry();  // "NEP"

fakerNp.name().fullName();  // "Clora Douglas"
fakerNep.name().fullName();  // "Laxmi Basynat"

As you see, the Java API is not aware of NEP while datafaker is not aware of NE. The same issue arises for IN and IND.

bodiam added a commit that referenced this issue Aug 2, 2022
Better usage of java.util.Locale. See #266
@bodiam
Copy link
Contributor

bodiam commented Aug 24, 2022

@abeshenov I think this is fixed with the above issue, not sure how to fix the second issue, but if you have some suggestions, please let us know.

@bodiam bodiam closed this as completed Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants