Non-ascii slug transliteration

110 views
Skip to first unread message

dbori...@gmail.com

unread,
Jan 10, 2016, 8:19:43 AM1/10/16
to Mezzanine Users
Hi guys,

I have a site with russian locale and want to convert non-ascii slugs into ascii one to avoid ugly urlencoded urls.
I've replaced Mezzanine's slugify with Django's default django.template.defaultfilters.slugify but it just strips non-ascii characters but i want to transliterate them.
Is there any way to do it in Mezzanine without making own slugify?

Thnx for any answer.

Stephen McDonald

unread,
Jan 10, 2016, 5:48:31 PM1/10/16
to Mezzanine Users
The first dozen or so results in Google when searching for "slugify unicode" reveal exact what you want.

--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Stephen McDonald
http://jupo.org

dbori...@gmail.com

unread,
Jan 10, 2016, 9:25:48 PM1/10/16
to Mezzanine Users, st...@jupo.org
Yes it is but it is not that i want =)
Sorry for my poor english. May be you did not correctly understand my previous message.
I want to convert any unicode symbols to english ascii symbols.
slugify_unicode makes url with russian characters if page title is in russian. It is not that i want.
And django's slugify simply strips non-ascii characters. It not that I want too.
Is there any way to convert (transliterate) unicode to ascii on the fly without additional programming? Any config option or something like this?

понедельник, 11 января 2016 г., 1:48:31 UTC+3 пользователь Stephen McDonald написал:

Stephen McDonald

unread,
Jan 10, 2016, 10:04:36 PM1/10/16
to Mezzanine Users
I've interpreted your second email exactly the same as your first, so I can only reply the same, perhaps a bit more clearly.

Since you mentioned trying Django's slugify function instead of Mezzanine's, presumably you've found the setting for configuring the slugify function used:


Powered with the ability to swap out the function, all you need now is the function. Like I said, Googling finds one right away. I have to confess, I actually searched for "unicode slugify", not "slugify unicode" as I originally mentioned (and which I think you thought of as a reference to the function name in Mezzanine, but I don't know). Anyway, here's the first result:


Here's an example from it doing exactly what you described:

# Replace non ascii chars with their "best" representation
slugify(u'北京 (capital of China)', only_ascii=True)
# u'bei-jing-capital-of-china'

Good luck.



Mike Dewhirst

unread,
Jan 10, 2016, 10:15:29 PM1/10/16
to mezzani...@googlegroups.com
On 11/01/2016 1:25 PM, dbori...@gmail.com wrote:
> Yes it is but it is not that i want =)
> Sorry for my poor english. May be you did not correctly understand my
> previous message.
> I want to convert any unicode symbols to english ascii symbols.
> slugify_unicode makes url with russian characters if page title is in
> russian. It is not that i want.
> And django's slugify simply strips non-ascii characters. It not that I
> want too.
> Is there any way to convert (transliterate) unicode to ascii on the fly
> without additional programming? Any config option or something like this?

I haven't seen one but I'm interested so I googled and found ...

http://stackoverflow.com/questions/816285/where-is-pythons-best-ascii-for-this-unicode-database

... which might let you get some of the transliteration done in your own
slugify() based on Django's slugify() in django/utils/text.py. You might
also need to maintain a list of unicode chars you don't like and replace
them with your preferred ascii chars before using slugify.

Good luck and please post your solution

Thanks

Mike

>
> понедельник, 11 Ñ Ð½Ð²Ð°Ñ€Ñ 2016 г., 1:48:31 UTC+3
> пользователь Stephen McDonald Ð½Ð°Ð¿Ð¸Ñ Ð°Ð»:
>
> The first dozen or so results in Google when searching for
> "slugify unicode" reveal exact what you want.
>
> On Mon, Jan 11, 2016 at 12:01 AM, <dbori...@gmail.com <javascript:>>
> wrote:
>
> Hi guys,
>
> I have a site with russian locale and want to convert non-ascii
> slugs into ascii one to avoid ugly urlencoded urls.
> I've replaced Mezzanine's slugify with Django's
> default django.template.defaultfilters.slugify but it just
> strips non-ascii characters but i want to transliterate them.
> Is there any way to do it in Mezzanine without making own slugify?
>
> Thnx for any answer.
>
> --
> You received this message because you are subscribed to the
> Google Groups "Mezzanine Users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to mezzanine-use...@googlegroups.com
> <javascript:>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> Stephen McDonald
> http://jupo.org
>
> --
> You received this message because you are subscribed to the Google
> Groups "Mezzanine Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mezzanine-use...@googlegroups.com
> <mailto:mezzanine-use...@googlegroups.com>.

Денис Борисов

unread,
Jan 11, 2016, 12:35:25 AM1/11/16
to mezzani...@googlegroups.com
I'm new in Django. Sorry for my stupid question. Now I see the right way with help of your comments.
Big thanks! I'll post my solution soon

To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.

Денис Борисов

unread,
Jan 11, 2016, 11:53:10 AM1/11/16
to mezzani...@googlegroups.com
The solution is extremely simple:
pip install awesome-slugify # For alternate russian transliteration support
SLUGIFY = 'slugify.slugify_ru' # In local_settings.py. Or slugify.slugify which is more generic

Andrey Andreev

unread,
May 16, 2019, 8:39:06 AM5/16/19
to Mezzanine Users
Denis Thanks! Very helpful!

понедельник, 11 января 2016 г., 19:53:10 UTC+3 пользователь Денис Борисов написал:

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzani...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages