]> git.openstreetmap.org Git - rails.git/blob - lib/osm_community_index/local_chapter.rb
Add relevant osm-community-index data into I18n
[rails.git] / lib / osm_community_index / local_chapter.rb
1 module OsmCommunityIndex
2   class LocalChapter
3     attr_reader :id, :name, :url
4
5     @localised_chapters = {}
6
7     def initialize(id, name, url)
8       @id = id
9       @name = name
10       @url = url
11     end
12
13     def self.local_chapters_with_locale(locale)
14       load_local_chapter_localisation
15       @localised_chapters[locale] ||= load_local_chapters(locale)
16     end
17
18     def self.load_local_chapter_localisation
19       community_index = OsmCommunityIndex.community_index
20       localisation_files = Dir.children(Rails.root.join("node_modules/osm-community-index/i18n/"))
21       localisation_files.each do |file|
22         locale = File.basename(file,".yaml")
23         # rails wants en-GB but osm-community-index has en_GB
24         locale_rails = locale.split("_").join("-")
25         full_path = Rails.root.join("node_modules/osm-community-index/i18n/#{file}")
26         locale_data = YAML.safe_load(File.read(full_path))[locale]
27
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
32             strings = locale_data[id] || {}
33             strings['name'] = locale_data['name'] || resource["strings"]["name"] || resource["strings"]["community"]
34
35             data = {}
36             data["osm_community_index"] = {}
37             data["osm_community_index"]["local_chapter"] = {}
38             data["osm_community_index"]["local_chapter"][id] = strings
39             # data["osm_community_index.local_chapter." + id] = localisation
40             I18n.backend.store_translations locale_rails, data
41
42             if locale == "en"
43               puts locale_rails + " " + id + " " + data.to_s
44             end
45           end
46         end
47       end
48
49     end
50
51     def self.load_local_chapters(locale)
52       community_index = OsmCommunityIndex.community_index
53       localised_strings = OsmCommunityIndex.localised_strings(locale)
54       local_chapters = []
55       community_index["resources"].each do |id, resource|
56         resource.each do |key, value|
57           next unless key == "type" && value == "osm-lc" && id != "OSMF"
58
59           strings = resource["strings"]
60           name = localised_strings.dig(id, "name") || strings["name"] || strings["community"]
61           url = strings["url"]
62           local_chapters.push(LocalChapter.new(id, name, url))
63         end
64       end
65       local_chapters
66     end
67   end
68 end