bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.85k stars 502 forks source link

Support of ISO 639x locale standards/early locale validation? #132

Closed Max-Riley closed 6 years ago

Max-Riley commented 6 years ago

Seems a few of these locales dont follow the iso 639x standard. Is there any road-map or want to support a standardized list of locales accepted that can be universal? as per ISO 639x Values support in C# is limited due to the locale codes not following the ISO standard for language culture names and the 639X values.

var lorem = new Lorem(CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
var lorem = new Lorem(CultureInfo.CurrentCulture.Name); // Will NOT work as it returns back as en-us for US

a few being:

Bogus Locale Code Standard Language Culture Name Language 639x Value
en_AU en-AU English - Australia ENA
pl pl-PL Polish PLK
en_US en-US English -United States ENU

Also if you enter an invalid string for the Locale, you don't know the locale is invalid until you try to invoke a method.

var lorem = new Lorem("Its a test!"); // works
var stringSample = lorem.Sentence(10); // throws the exception:
                                       // System.NullReferenceException: 'Object reference not set to an instance of an object.'
bchavez commented 6 years ago

Hi Maxfield,

Thank you for your suggestion. I really appreciate it.

I agree it would be a better developer experience to throw an exception when the user passes in an invalid locale instead of running into a NullReferenceException at method invocation. This would be an overall improvement we can take a closer look at.

As for changing over Bogus' locale codes wholesale to ISO 639x locale codes, I feel this is a significant breaking change. Generally, I'm okay with breaking changes if the benefits of the changes outweigh the costs of breaking people's code. However, I'm not sure a change like this would meet that threshold.

Maybe a better approach to interoperability with CultureInfo.CurrentCulture is to offer an extension method that maps culture names in the .NET Framework to Bogus locale codes like the following:

var bogusLocale = CultureInfo.CurrentCulture.ToBogusLocale();
var lorem = new Lorem(bogusLocale);

Or maybe accept a CultureInfo in the constructor:

var lorem = new Lorem(CultureInfo.CurrentCulture);

Also, I think it's important for Bogus to maintain its own locale codes (in line with faker.js) because there are some .NET Framework locales that won't have a direct mapping like en_au_ocker.

Thanks, Brian

:crown: :gem: "I know everything that shine ain't always gold..."

Max-Riley commented 6 years ago

Either way seems quite doable even a mapping between the supported languages of ISO 639x and supported locales of faker.js so it can keep in line with faker.js.

Either way seems quite awesome! Thank you for the wonderful code!

bchavez commented 6 years ago

Hi Maxfield,

Bogus v22.0.5 has been released.

Changes include:

Hope that helps!

Thanks, Brian

:car: :blue_car: "Let the good times roll..."