From cd58b96fad79e15d23a0bbb63977ae13b8ffffef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 20 Dec 2021 16:22:14 +0100 Subject: [PATCH] Support geozone segments with non-Latin characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `parameterize` method uses the `I18n.transliterate` method, whose documentation says: ``` I18n.transliterate("Ærøskøbing") => "AEroskobing" I18n.transliterate("日本語") => "???" ``` That means we can't use it for dictionaries where characters don't have a transliteration to the latin alphabet. So we're changing the code in order to only transliterate characters with a transliteration to the latin alphabet. Note the first example ("Česká republika") already worked with the previous code; the test has been added to make sure accented characters are handled properly. --- lib/user_segments.rb | 8 +++++++- spec/lib/user_segments_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/user_segments.rb b/lib/user_segments.rb index 5ee5bfe49..7e4e8a154 100644 --- a/lib/user_segments.rb +++ b/lib/user_segments.rb @@ -84,6 +84,12 @@ class UserSegments end def self.geozones - Geozone.order(:name).map { |geozone| [geozone.name.parameterize.underscore, geozone] }.to_h + Geozone.order(:name).map do |geozone| + [geozone.name.gsub(/./) { |char| character_approximation(char) }.underscore.tr(" ", "_"), geozone] + end.to_h + end + + def self.character_approximation(char) + I18n::Backend::Transliterator::HashTransliterator::DEFAULT_APPROXIMATIONS[char] || char end end diff --git a/spec/lib/user_segments_spec.rb b/spec/lib/user_segments_spec.rb index 4aa454cbe..79b6d176f 100644 --- a/spec/lib/user_segments_spec.rb +++ b/spec/lib/user_segments_spec.rb @@ -31,6 +31,16 @@ describe UserSegments do expect(UserSegments.segment_name("lowlands_and_highlands")).to eq "Lowlands and Highlands" end + it "supports international alphabets" do + create(:geozone, name: "Česká republika") + create(:geozone, name: "България") + create(:geozone, name: "日本") + + expect(UserSegments.segment_name("ceska_republika")).to eq "Česká republika" + expect(UserSegments.segment_name("България")).to eq "България" + expect(UserSegments.segment_name("日本")).to eq "日本" + end + it "returns regular segments when the geozone doesn't exist" do expect(UserSegments.segment_name("all_users")).to eq "All users" end