在 Laravel 5.2 中更改 Faker 语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34496720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Faker Locale in Laravel 5.2
提问by hapi
Is there a way to specify the Faker locale in the database/factories/ModelFactory.php file ? Here is my non functioning attempt at doing so >,<
有没有办法在 database/factories/ModelFactory.php 文件中指定 Faker 语言环境?这是我这样做的无效尝试>,<
$factory->define(App\Flyer::class, function (Faker\Generator $faker) {
// What is the correct way of doing this?
$faker->locale('en_GB');
return [
'zip' => $faker->postcode,
'state' => $faker->state,
];
});
Thanks for reading!
谢谢阅读!
回答by Vincent Mimoun-Prat
Faker locale can be configured in the config/app.php
configuration file. Just add the key faker_locale
.
Faker 语言环境可以在config/app.php
配置文件中进行配置。只需添加密钥faker_locale
。
e.g.: 'faker_locale' => 'fr_FR',
例如: 'faker_locale' => 'fr_FR',
See also my PR to document that previously undocumented feature: https://github.com/laravel/laravel/pull/4161
另请参阅我的 PR 以记录以前未记录的功能:https: //github.com/laravel/laravel/pull/4161
回答by David Barker
THIS ANSWER IS ONLY VALID FOR LARAVEL <=5.1 OR WHEN YOU WANT TO USE MANY DIFFERENT LOCALESsee this answer for a solution in Laravel >=5.2.
此答案仅适用于 Laravel <=5.1 或当您想使用许多不同的区域时,请参阅此答案以获取 Laravel >=5.2 中的解决方案。
To use a locale with Faker, the generator needs creating with the locale.
要在 Faker 中使用语言环境,生成器需要使用语言环境进行创建。
$faker = Faker\Factory::create('fr_FR'); // create a French faker
From the faker documentation:
从伪造者文档:
If no localized provider is found, the factory fallbacks to the default locale (en_EN).
如果未找到本地化提供程序,则工厂回退到默认语言环境 (en_EN)。
Laravel by default, binds the creation of a faker instance in the EloquentServiceProvider
. The exact code used to bind is:
Laravel 默认情况下,在EloquentServiceProvider
. 用于绑定的确切代码是:
// FakerFactory is aliased to Faker\Factory
$this->app->singleton(FakerGenerator::class, function () {
return FakerFactory::create();
});
It would appear that Laravel has no way to modify the locale of the faker instance it passes into the factory closures as it does not pass in any arguments to Faker.
看起来 Laravel 无法修改它传递给工厂闭包的 faker 实例的语言环境,因为它没有将任何参数传递给 Faker。
As such you would be better served by using your own instance of Faker in the factory method.
因此,在工厂方法中使用您自己的 Faker 实例会更好地为您服务。
$localisedFaker = Faker\Factory::create("fr_FR");
$factory->define(App\Flyer::class, function (Faker\Generator $faker) {
// Now use the localisedFaker instead of the Faker\Generator
// passed in to the closure.
return [
'zip' => $localisedFaker->postcode,
'state' => $localisedFaker->state,
];
});
回答by Juliano Petronetto
I prefer to use it:
我更喜欢使用它:
$fakerBR = Faker\Factory::create('pt_BR');
$factory->define(App\Flyer::class, function (Faker\Generator $faker) use (fakerBR) {
return [
'name' => $fakerBR->name,
'cpf' => $fakerBR->cpf,
'zip' => $faker->postcode,
'state' => $faker->state,
];
});
回答by ivanaugustobd
Late in the party, but after some research I've found this in faker documentation:
晚会晚了,但经过一些研究,我在伪造的文档中发现了这一点:
[...] since Faker starts with the last provider, you can easily override existing formatters: just add a provider containing methods named after the formatters you want to override.
That means that you can easily add your own providers to a Faker\Generator instance.
[...] 由于 Faker 从最后一个提供程序开始,您可以轻松覆盖现有的格式化程序:只需添加一个提供程序,其中包含以您要覆盖的格式化程序命名的方法。
这意味着您可以轻松地将自己的提供程序添加到 Faker\Generator 实例。
So you can do something like this
所以你可以做这样的事情
$faker->addProvider(new Faker\Provider\pt_BR\Person($faker));
Just before return []
and then use specific providers, like (in this case) $faker->cpf;
就在之前return []
然后使用特定的提供程序,例如(在这种情况下)$faker->cpf;
Tested on Laravel 5.3
在 Laravel 5.3 上测试
More info on Faker documentation
有关Faker 文档的更多信息
回答by hernandev
@IvanAugustoDB, there is a another form of doing that. When Laravel initalize faker, it can be constructed on another locale, just create a Service Provider and put the following snippet inside it.
@IvanAugustoDB,还有另一种形式。当 Laravel 初始化 faker 时,它可以在另一个语言环境中构建,只需创建一个服务提供者并将以下代码片段放入其中即可。
use Faker\Generator as FakerGenerator;
use Faker\Factory as FakerFactory;
$this->app->singleton(FakerGenerator::class, function () {
return FakerFactory::create('pt_BR');
});
回答by user2102687
$factory->define(App\User::class, function (Faker\Generator $faker) {
$faker->addProvider(new Faker\Provider\ng_NG\Person($faker));
$faker->addProvider(new Faker\Provider\ng_NG\PhoneNumber($faker));
...
in the above code, "ng_NG" is for Nigeria and can be replaced with any other locale.
在上面的代码中,“ng_NG”用于尼日利亚,可以替换为任何其他语言环境。
To my knowledge, you would have to specify Person, PhoneNumber and others depending on what you have in your vendor\fzaninotto\faker\src\Faker\Provider folder. However if the provider you intend using isn't available, then it will resolve back to using "en".
据我所知,您必须根据您的 vendor\fzaninotto\faker\src\Faker\Provider 文件夹中的内容来指定 Person、PhoneNumber 和其他人。但是,如果您打算使用的提供程序不可用,则它将解析为使用“en”。
This works like charm for me, and it should work for you too.
这对我来说很有魅力,它也应该对你有用。