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