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