Add Transliteration
sudhirj opened this issue · 1 comments
sudhirj commented
Rails' paramterization method also performs transliteration of unicode characters, can do the same in Go using https://github.com/rainycape/unidecode
External libs aren't required, there a normalization feature in the stdlib https://godoc.org/golang.org/x/text/runes#Remove
sudhirj commented
var nonWord = regexp.MustCompile(`\W+`)
var whitespace = regexp.MustCompile(`\s+`)
var doubleDash = regexp.MustCompile(`-+`)
func normalize(u string) string {
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
s, _, _ := transform.String(t, u)
return s
}
func parameterize(u string) string {
u = normalize(u)
u = strings.ToLower(u)
u = strings.TrimSpace(u)
// protect dashes from replacement by making them underscores
u = strings.ReplaceAll(u, "-", "_")
u = whitespace.ReplaceAllString(u, "_")
u = nonWord.ReplaceAllString(u, "")
// restore dashes
u = strings.ReplaceAll(u, "_", "-")
// collapse multiple dashes to one
u = doubleDash.ReplaceAllString(u, "-")
// get the dashes off the ends
u = strings.Trim(u, "-")
return u
}