]> git.openstreetmap.org Git - rails.git/blob - app/models/communities.rb
Localise local chapters
[rails.git] / app / models / communities.rb
1
2 class Communities
3
4   require 'yaml'
5
6   @local_chapters = {}
7
8   def self.local_chapters(locale)
9     puts "locale is "+ locale.to_s
10     @local_chapters[locale] = self.local_chapter_for(locale)
11   end
12
13   protected
14
15   def self.local_chapter_for(locale)
16
17     @local_chapters_index = self.load_local_chapters
18
19     locale_dict = self.locale_dict_for(locale)
20
21     localised_chapters = []
22     @local_chapters_index.each do |chapter|
23       id = chapter[:id]
24       name = locale_dict.dig(id,"name") || chapter[:name]
25       url = chapter[:url]
26       localised_chapters.push({ id: id, name: name, url: url })
27     end
28     puts localised_chapters
29     localised_chapters
30   end
31
32   def self.load_local_chapters
33
34     json_file = File.expand_path("node_modules/osm-community-index/dist/resources.json", Dir.pwd);
35     community_index = JSON.parse(File.read(json_file))
36
37     local_chapters = []
38     community_index['resources'].each do |id, resource|
39       resource.each do |key, value|
40         if key == "type" and value == "osm-lc" and id != "OSMF"
41           strings = resource['strings']
42           chapter_name = strings['community'] ||strings['name']
43           url = strings['url']
44           local_chapters.push({ id: id, name: chapter_name, url: url})
45         end
46       end
47     end
48     return local_chapters
49   end
50
51
52   def self.locale_dict_for(localeIn)
53     locale = localeIn.to_s.gsub("-","_")
54     full_local_path = File.expand_path("node_modules/osm-community-index/i18n/"+locale+".yaml", Dir.pwd)
55     locale_dict = {}
56     if File.exists?(full_local_path)
57       locale_dict = YAML.load(File.read(full_local_path))[locale]
58     else
59       shortened_locale = locale.split("_").first
60       if shortened_locale != locale
61         shortened_local_path = File.expand_path("node_modules/osm-community-index/i18n/"+shortened_locale+".yaml", Dir.pwd)
62         if File.exists?(shortened_local_path)
63           locale_dict = YAML.load(File.read(shortened_local_path))[shortened_locale]
64         end
65       end
66     end
67     return locale_dict
68   end
69
70 end