Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created June 14, 2021 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpaca-tc/92181c396ba5716ce7bdf6eb370f5dea to your computer and use it in GitHub Desktop.
Save alpaca-tc/92181c396ba5716ce7bdf6eb370f5dea to your computer and use it in GitHub Desktop.
モデル名のlocaleを複数形用に変換して追加する
# frozen_string_literal: true
paths = Dir[Rails.root.join('config/locales/models/defaults/*.yml')]
all_locales = paths.map { |path| YAML.load_file(path) }.inject(&:deep_merge)
# has_many用のデフォルトのlocaleを追加する
# config/locales/models/defaults/ja.ymlにモデル名が定義されている前提。
#
# activerecord/models にある値を複数形にして attributes に変換する
#
# 例
# 変換前
# ja:
# activerecord:
# models:
# employee: 従業員
#
# 変換後
# ja:
# attributes:
# employees: 従業員
all_locales.map { |locale, values|
models = values.dig('activerecord', 'models')
attributes = models.flat_map do |key, value|
[
[key.pluralize, value],
["#{key}_id", value]
]
end
{
locale => {
'attributes' => attributes.to_h
}
}
}.inject(&:merge)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment