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