]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index.rb
Use each_with_object
[rails.git] / lib / osm_community_index.rb
1 module OsmCommunityIndex
2   def self.add_to_i18n
3     communities = Community.all
4     files = Dir.glob(Rails.root.join("node_modules/osm-community-index/i18n/*.yaml"))
5     files.each do |file|
6       locale = File.basename(file, ".yaml")
7       community_locale_yaml = YAML.safe_load(File.read(file))[locale]
8       # rails wants en-GB but osm-community-index has en_GB
9       locale_rails = locale.tr("_", "-")
10
11       data = communities.each_with_object({}) do |community, obj|
12         id = community.id
13
14         strings = community_locale_yaml[id] || {}
15         # if the name isn't defined then fall back on community,
16         # as per discussion here: https://github.com/osmlab/osm-community-index/issues/483
17         strings["name"] = strings["name"] || community.strings["name"] || community.strings["community"]
18
19         obj.deep_merge!("osm_community_index" => { "communities" => { id => strings } })
20       end
21
22       I18n.backend.store_translations locale_rails, data
23     end
24   end
25 end