Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Convert hashes to hash with indifferent access for rails #644

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,29 @@ def parse(key)
}.join
end

def with_indifferent_access(values)
return values unless values.is_a?(Array)

values.map do |v|
v.respond_to?(:with_indifferent_access) ? v.with_indifferent_access : v
end
end

# Call I18n.translate with our configured locale if no
# locale is specified
def translate(*args)
opts = args.last.is_a?(Hash) ? args.pop : {}
opts[:locale] ||= Faker::Config.locale
opts[:raise] = true
I18n.translate(*(args.push(opts)))
with_indifferent_access(I18n.translate(*(args.push(opts))))
rescue I18n::MissingTranslationData
opts = args.last.is_a?(Hash) ? args.pop : {}
opts[:locale] = :en

# Super-simple fallback -- fallback to en if the
# translation was missing. If the translation isn't
# in en either, then it will raise again.
I18n.translate(*(args.push(opts)))
with_indifferent_access(I18n.translate(*(args.push(opts))))
end

# Executes block with given locale set.
Expand Down