Navigation Menu

Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Make it easier to change the locale of Faker #398

Closed
garygreen opened this issue Jan 31, 2017 · 9 comments
Closed

Make it easier to change the locale of Faker #398

garygreen opened this issue Jan 31, 2017 · 9 comments

Comments

@garygreen
Copy link

garygreen commented Jan 31, 2017

At the moment Faker always defaults the locale to US. It would be nice if there was some kind of way of changing it through a config/env option.

I know you can change it by completely overriding the way the faker generator singleton is registered with something like the below, but it's a bit long-winded and would be easier to change through a simple env('FAKER_LOCALE') option.

<?php

namespace App\Providers;

use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(FakerGenerator::class, function () {
          return FakerFactory::create('en_GB');
        });
    }
}
@RemiCollin
Copy link

I agree, plus it's very easy to implement with a config key.

More and more we're using Faker & Factories as an easy way to quickly throw an API for prototyping frontend, and I'm pretty sure this is a pretty common use case, and we need to set the localization for this.

@drbyte
Copy link

drbyte commented Jan 31, 2017

I've long wished this was built in already too.

I've been doing it this way, which pulls in the app.locale config setting.

       // tell Faker to use the app's primary locale
        $this->app->singleton(\Faker\Generator::class, function () {
            return \Faker\Factory::create(config('app.locale'));
        });

@themsaid
Copy link
Member

Next patch you'll be able to set a faker locale in config/app.php using a faker_locale config key.

https://github.com/laravel/framework/pull/17895/files

@garygreen
Copy link
Author

Thanks for update @themsaid much appreciated. 😄

@themsaid
Copy link
Member

❤️

@jbrooksuk
Copy link
Member

@themsaid I feel like that should go into a new testing.php or something. Seems weird to be in the main app config?

@garygreen
Copy link
Author

Not really specifically related to testing though - I use faker in model factories and seeders to seed fake data into application for demo purposes, etc. It would be nice if it could just use app.locale setting but faker wants it in a slightly different format. Dunno, doesn't make much difference to me. Easy enough to configure with faker_locale, if you don't want it in your config then don't worry, it defaults to US.

@fendis0709
Copy link

Really helpful.

I only need to add variable
'faker_locale' = 'id_ID'
inside of my config/app.php

Or, if I want to make them more customization, just add a new varible FAKER_LOCALE in env.php and call it in the config 'fakse_locale' = env('FAKER_LOCALE', 'id_ID).

PS : The second parameter is default value.

@bennyatc
Copy link

@fendiseptiawan0709 GREAT.
Thanks.

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

No branches or pull requests

7 participants