From: Tom Hughes Date: Sun, 22 Sep 2024 16:08:47 +0000 (+0100) Subject: Merge remote-tracking branch 'upstream/pull/5232' X-Git-Tag: live~104 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/8a685b70e89a42d9e58e72d850ab95dacfc58668?hp=72d61f29240aa54e6b1aa63912ea6d1c290209fc Merge remote-tracking branch 'upstream/pull/5232' --- diff --git a/lib/osm_community_index.rb b/lib/osm_community_index.rb index 11383dd16..c395a3768 100644 --- a/lib/osm_community_index.rb +++ b/lib/osm_community_index.rb @@ -38,7 +38,11 @@ module OsmCommunityIndex community_en_yaml.dig("_defaults", community.type, "name") community_name = community_locale_yaml.dig("_communities", community.strings["communityID"]) # Change the `{community}` placeholder to `%{community}` and use Ruby's Kernel.format to fill it in. - translated_name = format(template.gsub("{", "%{"), { :community => community_name }) if template && community_name + begin + translated_name = format(template.gsub("{", "%{"), { :community => community_name }) if template && community_name + rescue KeyError => e + Rails.logger.warn e.full_message + end return translated_name if translated_name # Otherwise fall back to the (English-language) resource name diff --git a/test/lib/osm_community_index_test.rb b/test/lib/osm_community_index_test.rb index a922d7fd9..9b10d81d9 100644 --- a/test/lib/osm_community_index_test.rb +++ b/test/lib/osm_community_index_test.rb @@ -40,4 +40,14 @@ class CountryTest < ActiveSupport::TestCase name = OsmCommunityIndex.resolve_name(community, community_locale_yaml, community_en_yaml) assert_equal("Translated Community Chapter", name) end + + def test_i18n_invalid_replacement_token + # Ignore invalid replacement tokens in OCI data provided. This might happen if translators were mistakenly translating the predefined token ids. + community = Community.new({ "id" => "foo-chapter", "type" => "osm-lc", "strings" => { "community" => "Community Name", "communityID" => "communityname" } }) + community_locale_yaml = { "_communities" => { "communityname" => "Translated Community" }, "_defaults" => { "osm-lc" => { "name" => "{comminauté} Chapter" } } } + community_en_yaml = {} + + name = OsmCommunityIndex.resolve_name(community, community_locale_yaml, community_en_yaml) + assert_equal("Community Name", name) + end end