Files
evening_detective_server/internal/modules/string_tools/transliterate.go
T
2026-10-01 16:42:39 +07:00

63 lines
1.1 KiB
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
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",
" ", "_",
)
)
// Transliterate выполняет транслитерацию русского текста в латиницу,
// заменяет пробелы на _, удаляет все остальные символы, кроме цифр (знаки препинания и т.д.)
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
}