LoginSignup
62
62

More than 5 years have passed since last update.

Faker を i18n locale :ja で使うときは Faker::Config.locale を :en にセットしなおすべし

Posted at

Faker は各種 locale に対応しているので Rails アプリなどで default locale を :ja にセットすると人名のダミーデータが日本語化されたりして便利なのだけど Faker::Internet.email がぶっ壊れてテストデータの email バリデーションエラーが出まくってハマった。

default locale :en で使う場合

問題無し。普通はこういう状態を期待する。

require 'i18n'
I18n.locale # => :en
require 'faker'
Faker::Config.locale # => :en
Faker::Internet.email # => "sanford@jacobsonjohns.com"

I18n.locale = :ja にセットして使う場合

Faker の locale も :ja になり、 email アドレスの local part / domain part が壊れる (内部的に日本語で "美咲.清水@美咲.com" みたいなデータが作られた上で ascii 範囲外の文字列が削除されていると予想)

require 'i18n'
I18n.locale # => :en
I18n.locale = :ja
I18n.locale # => :ja
require 'faker'
Faker::Config.locale # => :ja
Faker::Internet.email # => ".@.com"

i18n.locale = :ja した上で Faker::Config.locale = :en にセットしなおす

期待した振る舞いに戻る。

require 'i18n'
I18n.locale # => :en
I18n.locale = :ja
I18n.locale # => :ja
require 'faker'
Faker::Config.locale # => :ja
Faker::Config.locale = :en
Faker::Config.locale # => :en
Faker::Internet.email # => "ransom_blanda@auer.name"
62
62
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
62