]> git.openstreetmap.org Git - rails.git/commitdiff
Localise local chapters
authorAdam Hoyle <atomoil@gmail.com>
Tue, 24 Aug 2021 19:55:09 +0000 (20:55 +0100)
committerAdam Hoyle <atomoil@gmail.com>
Tue, 24 Aug 2021 20:38:05 +0000 (21:38 +0100)
app/controllers/site_controller.rb
app/models/communities.rb

index 73e4ef51d5ced1dca199062575554ac35308fa80..8323ae5e7324b07bfd6870c7154cd93a76003bad 100644 (file)
@@ -108,7 +108,7 @@ class SiteController < ApplicationController
 
   def communities
     @locale = params[:communities_locale] || I18n.locale
-    @local_chapters = Communities.local_chapters
+    @local_chapters = Communities.local_chapters(@locale)
   end
 
   def export; end
index f318157ce4e1fc3eaef36908aaeae1c0437be834..cd8d6c0fe1e4209bd0914f4b95bff6e536778980 100644 (file)
@@ -1,29 +1,70 @@
 
 class Communities
 
-  def self.local_chapters
-    @local_chapters ||= self.load_local_chapters
+  require 'yaml'
+
+  @local_chapters = {}
+
+  def self.local_chapters(locale)
+    puts "locale is "+ locale.to_s
+    @local_chapters[locale] = self.local_chapter_for(locale)
   end
 
   protected
 
+  def self.local_chapter_for(locale)
+
+    @local_chapters_index = self.load_local_chapters
+
+    locale_dict = self.locale_dict_for(locale)
+
+    localised_chapters = []
+    @local_chapters_index.each do |chapter|
+      id = chapter[:id]
+      name = locale_dict.dig(id,"name") || chapter[:name]
+      url = chapter[:url]
+      localised_chapters.push({ id: id, name: name, url: url })
+    end
+    puts localised_chapters
+    localised_chapters
+  end
+
   def self.load_local_chapters
 
-    json_file = File.expand_path("node_modules/osm-community-index/dist/completeFeatureCollection.json", Dir.pwd);
+    json_file = File.expand_path("node_modules/osm-community-index/dist/resources.json", Dir.pwd);
     community_index = JSON.parse(File.read(json_file))
 
     local_chapters = []
-    community_index['features'].each do |feature|
-      feature['properties']['resources'].each do |id, data|
-        data.each do |key, value|
-          if key == "type" and value == "osm-lc" and data['strings']['community']
-            local_chapters.push({ id: id, name: data['strings']['community'], url: data['strings']['url'] });
-          end
+    community_index['resources'].each do |id, resource|
+      resource.each do |key, value|
+        if key == "type" and value == "osm-lc" and id != "OSMF"
+          strings = resource['strings']
+          chapter_name = strings['community'] ||strings['name']
+          url = strings['url']
+          local_chapters.push({ id: id, name: chapter_name, url: url})
         end
       end
     end
-
     return local_chapters
   end
 
+
+  def self.locale_dict_for(localeIn)
+    locale = localeIn.to_s.gsub("-","_")
+    full_local_path = File.expand_path("node_modules/osm-community-index/i18n/"+locale+".yaml", Dir.pwd)
+    locale_dict = {}
+    if File.exists?(full_local_path)
+      locale_dict = YAML.load(File.read(full_local_path))[locale]
+    else
+      shortened_locale = locale.split("_").first
+      if shortened_locale != locale
+        shortened_local_path = File.expand_path("node_modules/osm-community-index/i18n/"+shortened_locale+".yaml", Dir.pwd)
+        if File.exists?(shortened_local_path)
+          locale_dict = YAML.load(File.read(shortened_local_path))[shortened_locale]
+        end
+      end
+    end
+    return locale_dict
+  end
+
 end