Support geozone segments with non-Latin characters

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.
This commit is contained in:
Javi Martín
2021-12-20 16:22:14 +01:00
parent 18910d0904
commit cd58b96fad
2 changed files with 17 additions and 1 deletions

View File

@@ -84,6 +84,12 @@ class UserSegments
end end
def self.geozones 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
end end

View File

@@ -31,6 +31,16 @@ describe UserSegments do
expect(UserSegments.segment_name("lowlands_and_highlands")).to eq "Lowlands and Highlands" expect(UserSegments.segment_name("lowlands_and_highlands")).to eq "Lowlands and Highlands"
end 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 it "returns regular segments when the geozone doesn't exist" do
expect(UserSegments.segment_name("all_users")).to eq "All users" expect(UserSegments.segment_name("all_users")).to eq "All users"
end end