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

How to generate a middleName? #381

Closed
bloodgang94 opened this issue Jul 14, 2021 · 4 comments
Closed

How to generate a middleName? #381

bloodgang94 opened this issue Jul 14, 2021 · 4 comments

Comments

@bloodgang94
Copy link

How will the middle name be generated?
I use the ru locale. In the class name I did not find a property

@digitalcoyote
Copy link
Contributor

digitalcoyote commented Jul 14, 2021 via email

@salixzs
Copy link
Contributor

salixzs commented Jul 14, 2021

If you do not need it to be 100% correct, you can reuse FirstName and if it is Fathers` name, you can modify rule to add "ич" or "ович" to the end, so "Иван" would become "Иванович".

.RuleFor(u => u.MiddleName, f => f.Person.FirstName() + "ович")

Then again if you want it gender safe - you need to build some more logic to add "овна" in case person gender is woman.

@bloodgang94
Copy link
Author

bloodgang94 commented Jul 14, 2021

If you do not need it to be 100% correct, you can reuse FirstName and if it is Fathers` name, you can modify rule to add "ич" or "ович" to the end, so "Иван" would become "Иванович".

.RuleFor(u => u.MiddleName, f => f.Person.FirstName() + "ович")

Then again if you want it gender safe - you need to build some more logic to add "овна" in case person gender is woman.

Thank you. I need 100% correct reporting. Solved the problem like this :
image

@bchavez
Copy link
Owner

bchavez commented Jul 18, 2021

It's super great to see the community help with these questions. Great work!

To add to the solutions in this issue; Bogus does contain an "ru" set of gendered middle names. However, the "ru" middle names are not exposed at a top level by C# APIs. The raw data can be found here:

"male_middle_name": [
"Александрович",
"Алексеевич",
"Альбертович",
"Анатольевич",

If you want to access this raw data in C#, you'll need to use lower-level APIs via the raw DB as shown below:

void Main()
{
   var f = new Faker("ru");
   Enumerable.Range(0, 5)
      .Select(_ => f.Name.GetRuMiddleName(Name.Gender.Male))
      .Dump();

   Enumerable.Range(0, 5)
      .Select(_ => f.Name.GetRuMiddleName(Name.Gender.Female))
      .Dump();
}

public static class RuNameExtensions
{
   public static string GetRuMiddleName(this Bogus.DataSets.Name name, Name.Gender gender)
   {
      BArray middleNameData;
      if( gender == Bogus.DataSets.Name.Gender.Male ){
         middleNameData = (BArray)Bogus.Database.Get("name", "male_middle_name", "ru");
      }
      else
      {
         middleNameData = (BArray)Bogus.Database.Get("name", "female_middle_name", "ru");
      }
      
      return name.Random.ArrayElement(middleNameData);
   }
}

image

I have no idea if this middle name data for the "ru" locale is correct, but it's there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants