1 # frozen_string_literal: true
3 module OsmCommunityIndex
5 # Filter the communities here to avoid loading excessive numbers of translations
6 communities = Community.where(:type => "osm-lc").where.not(:id => "OSMF")
7 community_en_yaml = if File.exist?("node_modules/osm-community-index/i18n/en.yaml")
8 YAML.safe_load_file("node_modules/osm-community-index/i18n/en.yaml")["en"]
13 files = Rails.root.glob("node_modules/osm-community-index/i18n/*.yaml")
15 locale = File.basename(file, ".yaml")
16 community_locale_yaml = YAML.safe_load_file(file)[locale]
17 # rails wants language-COUNTRY but osm-community-index uses underscores
18 locale_rails = locale.tr("_", "-")
20 data = communities.each_with_object({}) do |community, obj|
23 strings = community_locale_yaml[id] || {}
24 strings["name"] = resolve_name(community, community_locale_yaml, community_en_yaml)
26 obj.deep_merge!("osm_community_index" => { "communities" => { id => strings } })
29 I18n.backend.store_translations locale_rails, data
33 def self.resolve_name(community, community_locale_yaml, community_en_yaml)
34 # If theres an explicitly translated name then use that
35 translated_name = community_locale_yaml.dig(community.id, "name")
36 return translated_name if translated_name
38 # If not, then look up the default translated name for this type of community, and interpolate the template
39 template = community_locale_yaml.dig("_defaults", community.type, "name") ||
40 community_en_yaml.dig("_defaults", community.type, "name")
41 community_name = community_locale_yaml.dig("_communities", community.strings["communityID"])
42 # Change the `{community}` placeholder to `%{community}` and use Ruby's Kernel.format to fill it in.
44 translated_name = format(template.gsub("{", "%{"), { :community => community_name }) if template && community_name
46 Rails.logger.warn e.full_message
48 return translated_name if translated_name
50 # Otherwise fall back to the (English-language) resource name
51 return community.strings["name"] if community.strings["name"]
53 # Finally use the (English-language) community name
54 community.strings["community"]