Files
evening_detective_server/internal/modules/string_tools/transliterate.go
T
2026-10-03 13:22:11 +07:00

62 lines
933 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package string_tools provides string transformation helpers.
package string_tools
import (
"regexp"
"strings"
)
var (
replacer = strings.NewReplacer(
"а", "a",
"б", "b",
"в", "v",
"г", "g",
"д", "d",
"е", "e",
"ё", "yo",
"ж", "zh",
"з", "z",
"и", "i",
"й", "y",
"к", "k",
"л", "l",
"м", "m",
"н", "n",
"о", "o",
"п", "p",
"р", "r",
"с", "s",
"т", "t",
"у", "u",
"ф", "f",
"х", "h",
"ц", "ts",
"ч", "ch",
"ш", "sh",
"щ", "sch",
"ъ", "",
"ы", "y",
"ь", "",
"э", "e",
"ю", "yu",
"я", "ya",
" ", "_",
)
)
func Transliterate(text string) string {
result := replacer.Replace(strings.ToLower(text))
reg := regexp.MustCompile(`[^a-z0-9_]`)
result = reg.ReplaceAllString(result, "")
regMulti := regexp.MustCompile(`_+`)
result = regMulti.ReplaceAllString(result, "_")
result = strings.Trim(result, "_")
return result
}