Skip to content

Instantly share code, notes, and snippets.

@baikov
Created July 30, 2021 13:27
Show Gist options
  • Save baikov/22c4b94edbd21ac5be4759a4f473a949 to your computer and use it in GitHub Desktop.
Save baikov/22c4b94edbd21ac5be4759a4f473a949 to your computer and use it in GitHub Desktop.
Custom slugify func for Russian cyrilic strings in Django
from django.utils.text import slugify
def custom_slugify(string: str) -> str:
letters = {
"а": "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",
"щ": "shch",
"ъ": "",
"ы": "i",
"ь": "",
"э": "e",
"ю": "yu",
"я": "ya",
}
string = string.lower()
for char in set(string):
if char in letters.keys():
string = string.replace(char, letters[char])
return slugify(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment