]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/local_chapter.rb
d83f134719f5ea692da80b23eeac056a1d96c5db
[rails.git] / lib / osm_community_index / local_chapter.rb
1 module OsmCommunityIndex
2   class LocalChapter
3     def self.add_to_i18n
4       local_chapters = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
5       files = Dir.glob(Rails.root.join("node_modules/osm-community-index/i18n/*"))
6       files.each do |file|
7         locale = File.basename(file, ".yaml")
8         community_index_yaml = YAML.safe_load(File.read(file))[locale]
9         # rails wants en-GB but osm-community-index has en_GB
10         locale_rails = locale.tr("_", "-")
11         data = {}
12
13         local_chapters.each do |chapter|
14           id = chapter[:id]
15
16           strings = community_index_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"] || chapter["strings"]["name"] || chapter["strings"]["community"]
20
21           data.deep_merge!({ "osm_community_index" => { "local_chapter" => { id => strings } } })
22         end
23
24         I18n.backend.store_translations locale_rails, data
25       end
26     end
27   end
28 end