OCI: ignore invalid replacement token

This commit is contained in:
mmd-osm 2024-09-21 09:17:03 +02:00
parent 2fd2317bc6
commit ac42afefcd
2 changed files with 15 additions and 1 deletions

View file

@ -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.
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

View file

@ -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