]> git.openstreetmap.org Git - rails.git/blob - app/models/communities.rb
Improve method privacy
[rails.git] / app / models / communities.rb
1 class Communities
2   require "yaml"
3
4   @local_chapters = {}
5
6   def self.local_chapters(locale)
7     @local_chapters[locale] = local_chapter_for(locale)
8   end
9
10   class << self
11     protected
12
13     def local_chapter_for(locale)
14       @local_chapters_index = load_local_chapters
15       locale_dict = locale_dict_for(locale)
16       localised_chapters = []
17       @local_chapters_index.each do |chapter|
18         id = chapter[:id]
19         name = locale_dict.dig(id, "name") || chapter[:name]
20         url = chapter[:url]
21         localised_chapters.push({ :id => id, :name => name, :url => url })
22       end
23       localised_chapters
24     end
25
26     def load_local_chapters
27       json_file = File.expand_path("node_modules/osm-community-index/dist/resources.json", Dir.pwd)
28       community_index = JSON.parse(File.read(json_file))
29       local_chapters = []
30       community_index["resources"].each do |id, resource|
31         resource.each do |key, value|
32           next unless key == "type" && value == "osm-lc" && id != "OSMF"
33
34           strings = resource["strings"]
35           chapter_name = strings["community"] || strings["name"]
36           url = strings["url"]
37           local_chapters.push({ :id => id, :name => chapter_name, :url => url })
38         end
39       end
40       local_chapters
41     end
42
43     def locale_dict_for(locale_in)
44       locale = locale_in.to_s.tr("-", "_")
45       full_local_path = File.expand_path("node_modules/osm-community-index/i18n/#{locale}.yaml", Dir.pwd)
46       locale_dict = {}
47       if File.exist?(full_local_path)
48         locale_dict = YAML.safe_load(File.read(full_local_path))[locale]
49       else
50         shortened_locale = locale.split("_").first
51         if shortened_locale != locale
52           shortened_local_path = File.expand_path("node_modules/osm-community-index/i18n/#{shortened_locale}.yaml", Dir.pwd)
53           locale_dict = YAML.safe_load(File.read(shortened_local_path))[shortened_locale] if File.exist?(shortened_local_path)
54         end
55       end
56       locale_dict
57     end
58   end
59 end