1 module OsmCommunityIndex
2 class OsmCommunityIndex
5 @localised_strings = {}
7 def self.community_index
8 @community_index ||= community_index_from_json
11 def self.localised_strings(locale)
12 @localised_strings[locale] ||= locale_hash_from_json(locale)
15 def self.community_index_from_json
16 json_file = Rails.root.join("node_modules/osm-community-index/dist/resources.json")
17 JSON.parse(File.read(json_file))
20 def self.locale_hash_from_json(locale_in)
21 locale = locale_in.to_s.tr("-", "_")
22 # try the passed in locale
23 json = load_locale_json(locale)
24 return json unless json.nil?
26 # now try it without it's country part (eg 'en' instead of 'en_GB')
27 shortened_locale = locale.split("_").first
28 unless shortened_locale == locale
29 json = load_locale_json(shortened_locale)
30 return json unless json.nil?
33 # if nothing else works, then return "en"
34 load_locale_json("en")
37 def self.load_locale_json(locale)
38 json_path = Rails.root.join("node_modules/osm-community-index/i18n/#{locale}.yaml")
39 return YAML.safe_load(File.read(json_path))[locale] if File.exist?(json_path)