]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/local_chapter.rb
178d3437d334e3ac9a93a0dfd71b132179a0d750
[rails.git] / lib / osm_community_index / local_chapter.rb
1 module OsmCommunityIndex
2   class LocalChapter
3     attr_reader :id, :url
4
5     def initialize(id, url)
6       @id = id
7       @url = url
8     end
9
10     def self.local_chapters
11       @chapters = init_local_chapters
12     end
13
14     def self.init_local_chapters
15       raw_local_chapters = load_raw_local_chapters
16       local_chapters = []
17       raw_local_chapters.each do |chapter|
18         id = chapter[:id]
19         url = chapter[:resource]["strings"]["url"]
20         local_chapters.push(LocalChapter.new(id, url))
21       end
22       local_chapters
23     end
24
25     def self.load_raw_local_chapters
26       community_index = OsmCommunityIndex.community_index
27       raw_local_chapters = []
28       community_index["resources"].each do |id, resource|
29         resource.each do |key, value|
30           next unless key == "type" && value == "osm-lc" && id != "OSMF"
31           raw_local_chapters.push({ :id => id, :resource => resource })
32         end
33       end
34       raw_local_chapters
35     end
36
37     def self.add_to_i18n
38       raw_local_chapters = load_raw_local_chapters
39       files = Dir.glob(Rails.root.join("node_modules/osm-community-index/i18n/*"))
40       files.each do |file|
41         locale = File.basename(file,".yaml")
42         community_index_yaml = YAML.safe_load(File.read(file))[locale]
43         # rails wants en-GB but osm-community-index has en_GB
44         locale_rails = locale.split("_").join("-")
45         data = {}
46
47         raw_local_chapters.each do |chapter|
48           id = chapter[:id]
49           resource = chapter[:resource]
50
51           strings = community_index_yaml[id] || {}
52           # if the name isn't defined then fall back on community,
53           # as per discussion here: https://github.com/osmlab/osm-community-index/issues/483
54           strings['name'] = strings['name'] || resource["strings"]["name"] || resource["strings"]["community"]
55
56           data.deep_merge!({"osm_community_index" => {"local_chapter" => {id => strings}}})
57         end
58
59         I18n.backend.store_translations locale_rails, data
60       end
61     end
62   end
63 end