Created
June 14, 2021 01:13
-
-
Save alpaca-tc/92181c396ba5716ce7bdf6eb370f5dea to your computer and use it in GitHub Desktop.
モデル名のlocaleを複数形用に変換して追加する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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