How to replace accented letters with ASCII letters?

I’m late to this party but you might find unicode_transform useful. Its a rules-based transliterator which implements currently just a few of the many CLDR transliterations. Equally it might be both too much and too little for what you need.

The transformation rules for Latin to ASCII are quite comprehensive!

@jayjun’s excellent slugify package would be my general recommendation for slugification.

Examples

iex> Unicode.Transform.LatinAscii.transform "ü ø ß"
"u o ss"

iex> Unicode.Transform.LatinAscii.transform "árboles más grandes" 
"arboles mas grandes"

iex> Unicode.Transform.LatinAscii.transform "Übel wütet der Gürtelwürger"
"Ubel wutet der Gurtelwurger"

iex> Unicode.Transform.LatinAscii.transform "Ł"                  
"L"
3 Likes

For the lazy people (like me) who don’t want to decipher the whole thread, it seems like @massimo’s answer is the way to go:

I’d be wary of using that approach since you’d have to be completely certain your text doesn’t contain non-word characters. To me that’s just going to come back and bite you at some time in the future.

iex> String.normalize("Näytẗkuv#$%^&*()[]aèüÀÁÂÃĀĂȦÄẢÅǍȀȂĄẠḀẦẤàáâä", :nfd) |> String.replace(~r/\W/u, "")
"NayttkuvaeuAAAAAAAAAAAAAAAAAAaaaa"

Ah, thanks for pointing that out. I take it you still stand by your previous answer?

Just re-read and I’ve no updated reason to change my answer. unicode_transforms is a general purpose transformation from Unicode to ASCII - that might be more than you need. And therefore slugify would be more straight forward if you’re generating slugs.

1 Like