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