Removing Accented (Diacritic) Characters From Strings in Rails

Sreedev Kodichath
TarkaLabs TIL
Published in
1 min readMay 28, 2020

--

If you have a huge list of text records with accented characters and want to get rid of the accented (diacritic) characters because you want to keep the data ASCII-only, Rails offers a simple method to get rid of accented characters without having to go through the pain of writing complex Regex matchers.

I18n.transliterate "São Paulo"=> "Sao Paulo"

As per the official documentation, the `transliterate` method replaces non-ASCII characters with an ASCII approximation.

NOTE: In case, an ASCII Approximation for a non-ASCII character is not found, the character is replaced with a `?`.

--

--

Sreedev Kodichath
TarkaLabs TIL

Senior Software Engineer @ Tunecore. Ruby, Elixir, Javascript C & Rust. Amateur Radio Operator — KD2ZWB/VU2JWE. www.sree.dev. Brooklyn, NYC.