How to localize a Faker database seeder in Laravel 5

Database migrations and database seeding is supported out of the box in Laravel 5.x. A great feature. In a wink you get a database full of `faked` but structured content. You can even create random image-urls (lorempixel).

Strangely even if you set your Application Locale Configuration in `config\app.php` on “nl” you still get faked English names instead of Dutch. That is not what you want.

The fix is easy, suppose you have this ModelFactory:


$factory->define(App\Person::class, function ($faker) {
return [
'name' => $faker->name,
'company' => $faker->company,
'pic' => $faker->imageUrl(600, 400, 'business'), // 'http://lorempixel.com/600/400/business/'
];
});

Simply add another provider and `overwrite` the old English provider


$factory->define(App\Person::class, function ($faker) {

$faker->addProvider(new Faker\Provider\nl_NL\Person($faker));
$faker->addProvider(new Faker\Provider\nl_NL\Company($faker));

return [
...

That way the object will be extended, the new provider will be unshifted to the beginning of the providers array


Faker\Generator {#560
#providers: array:19 [
0 => Faker\Provider\nl_NL\Company {#583
#generator: Faker\Generator {#560}
#unique: null
}
1 => Faker\Provider\nl_NL\Person {#582
#generator: Faker\Generator {#560}
#unique: null
}
2 => Faker\Provider\Uuid {#580
#generator: Faker\Generator {#560}
#unique: null
}
3 => Faker\Provider\UserAgent {#579
#generator: Faker\Generator {#560}
#unique: null
}
4 => Faker\Provider\en_US\Text {#578
#explodedText: null
#consecutiveWords: []
#generator: Faker\Generator {#560}
#unique: null
}
5 => Faker\Provider\en_US\PhoneNumber {#577
#generator: Faker\Generator {#560}
#unique: null
}
6 => Faker\Provider\en_US\Person {#576
#generator: Faker\Generator {#560}
#unique: null
}
7 => Faker\Provider\Payment {#575
#generator: Faker\Generator {#560}
#unique: null
}
8 => Faker\Provider\Miscellaneous {#574
#generator: Faker\Generator {#560}
#unique: null
}
9 => Faker\Provider\Lorem {#573
#generator: Faker\Generator {#560}
#unique: null
}
10 => Faker\Provider\Internet {#572
#generator: Faker\Generator {#560}
#unique: null
}
11 => Faker\Provider\Image {#571
#generator: Faker\Generator {#560}
#unique: null
}
12 => Faker\Provider\File {#565
#generator: Faker\Generator {#560}
#unique: null
}
13 => Faker\Provider\DateTime {#562
#generator: Faker\Generator {#560}
#unique: null
}
14 => Faker\Provider\en_US\Company {#567
#generator: Faker\Generator {#560}
#unique: null
}
15 => Faker\Provider\Color {#566
#generator: Faker\Generator {#560}
#unique: null
}
16 => Faker\Provider\Biased {#568
#generator: Faker\Generator {#560}
#unique: null
}
17 => Faker\Provider\Barcode {#569
#generator: Faker\Generator {#560}
#unique: null
}
18 => Faker\Provider\en_US\Address {#570
#generator: Faker\Generator {#560}
#unique: null
}
]
#formatters: []
}

To be honest, Faker creates fake Dutch Person that have hilarious and not so common names:

  1. Leon Gansneb genaamd Tengnagel tot Bonkenhave Msc
  2. Dubbeldemuts van der Sluys
  3. Thijmen Elsjan de Wipper
  4. drs Pien Gellemeyer
  5. Paspoort van Grijpskerke en Poppendamme

My god, those boys and girls most have had a hard time on school, ruthlessly ragged on those weird names.

Thank goodness it’s Laravel in a test-environment.

 

Comments are closed.

Categories
Archives
Links