laravel 如何在 fzaninotto/Faker 中更改生成文本的语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27675846/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:36:23  来源:igfitidea点击:

How to change generated text's language in fzaninotto/Faker?

phplaravellocalizationfaker

提问by YanDatsiuk

In Laravel I use Faker. (fzaninotto/Faker)

在 Laravel 中,我使用 Faker。( fzaninotto/Faker)

Can't change locale(language) of generated texts.

无法更改生成文本的区域设置(语言)。

My code:

我的代码:

use Faker\Factory as Faker;

class MySeeder extends Seeder {    

    public function run() {
        $faker = Faker::create('ru_RU');

        $randomSentence = $faker->sentence();
        ...
    }
}

But, as result $randomSentencecontains generated text from default locale ('en_EN').

但是,结果$randomSentence包含从默认语言环境('en_EN')生成的文本。

P.S. Faker is updated. Folder '\vendor\fzaninotto\faker\src\Faker\Provider\ru_RU' contains Text.php

PS Faker 已更新。文件夹 '\vendor\fzaninotto\faker\src\Faker\Provider\ru_RU' 包含 Text.php

回答by Ulrik

The reason you're not getting Russian text from the sentence()method is that it's not using the text from Text.php.

您没有从该sentence()方法中获取俄语文本的原因是它没有使用来自Text.php.

The sentence()method is defined in Lorem.phpand uses the wordlist in that file. You either need to use the realText()method, or implement a Russian version of the wordlist (which the Faker author has already said no to)

sentence()方法在Lorem.php 中定义并使用该文件中的词表。您要么需要使用该realText()方法,要么实施俄语版的词表(Faker 作者已经拒绝

In short, you need to use this line to get russian text:

简而言之,您需要使用此行来获取俄语文本:

$faker = Faker::create('ru_RU');
$randomSentence = $faker->realText();