Created
July 30, 2021 13:27
-
-
Save baikov/22c4b94edbd21ac5be4759a4f473a949 to your computer and use it in GitHub Desktop.
Custom slugify func for Russian cyrilic strings in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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