]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/local_chapter.rb
Skip path rebuilding
[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         locale = File.basename(file,".yaml")
34         community_index_yaml = YAML.safe_load(File.read(file))[locale]
35         # rails wants en-GB but osm-community-index has en_GB
36         locale_rails = locale.split("_").join("-")
37
38         community_index["resources"].each do |id, resource|
39           resource.each do |key, value|
40             next unless key == "type" && value == "osm-lc" && id != "OSMF"
41
42             strings = community_index_yaml[id] || {}
43             # if the name isn't defined then fall back on community,
44             # as per discussion here: https://github.com/osmlab/osm-community-index/issues/483
45             strings['name'] = strings['name'] || resource["strings"]["name"] || resource["strings"]["community"]
46
47             data = {}
48             data["osm_community_index"] = {}
49             data["osm_community_index"]["local_chapter"] = {}
50             data["osm_community_index"]["local_chapter"][id] = strings
51             I18n.backend.store_translations locale_rails, data
52
53           end
54         end
55       end
56     end
57   end
58 end