A site in Vietnamese, it is virtually no different to English. However, there is a problem that is slug. When I type characters such as “ư”, “ơ”, “á”,… Django is not identified. Solution here is to replace characters that do not sign into.
Eg:
ư -> u
ơ -> o
á -> a
One from “những-viên-kẹo” will become “nhung-vien-keo”.
However, I do not know how to do this. Someone help me. Thank you very much!
[edit]
I take it back, django’s
django.template.defaultfilters.slugify()does what you want, usingunicodedata.normalizeand.encode('ascii', 'ignore'). Just feeding your string into slugify will work:To do this automatically, add this to the
.save()method in your models:The slolution I wrote ealier (below) would still be useful for languages that require additional characters in their translation, eg German’s ü->ue, ß->ss etc.
[original post]
Python allows you to use a translation dict to map characters to a replacement string.
A simple version for you case would be:
And then you can call:
To get:
For more advanced use (ie a dynamic dict), see eg http://effbot.org/zone/unicode-convert.htm
Note that the above is just to show you what the map needs to look like, it’s not a particularly convenient way of entering the data. A more convenient way to do the exact same thing is something like: