]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index.rb
Restrict files read from directory
[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       data = {}
11
12       communities.each do |community|
13         id = community[:id]
14
15         strings = community_locale_yaml[id] || {}
16         # if the name isn't defined then fall back on community,
17         # as per discussion here: https://github.com/osmlab/osm-community-index/issues/483
18         strings["name"] = strings["name"] || community["strings"]["name"] || community["strings"]["community"]
19
20         data.deep_merge!({ "osm_community_index" => { "communities" => { id => strings } } })
21       end
22
23       I18n.backend.store_translations locale_rails, data
24     end
25   end
26 end