Skip to content

Made the slugger compatible with every language #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed

Made the slugger compatible with every language #389

wants to merge 8 commits into from

Conversation

javiereguiluz
Copy link
Member

In the Symfony Installer we recently made a change to forbid any non-Latin character in the project names (see details). This is needed because Composer itself requires that for package names.

In the Symfony Demo app we had a similar problem with the slugs of the blog posts. We removed any non-Latin character ... but that caused bugs like #136. I propose to change the slugger completely and accept any character.

For example, you can now create this blog post:

backend_post

And the blog listing no longer displays an error:

blog_post

And the URL is perfectly fine although it contains non-Latin chars:

url-post

@@ -27,6 +27,6 @@ class Slugger
*/
public function slugify($string)
{
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-');
return str_replace(' ', '-', strtolower(trim(strip_tags($string))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should replace any whitespace char, not only spaces. And you should not put several dashes for consecutive spaces.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will /\s+/ regex pattern be enough here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bocharsky-bw yes, I did that at the end 👍

@@ -27,6 +27,6 @@ class Slugger
*/
public function slugify($string)
{
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-');
return str_replace(' ', '-', strtolower(trim(strip_tags($string))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, if you want to support any language, you should use mb_strtolower

@javiereguiluz
Copy link
Member Author

There are some failing tests only for PHP 5.5. I don't understand why because we use Symfony's polyfill for mb_strtolower():

There were 2 failures:
1) Tests\AppBundle\Utils\SluggerTest::testSlugify with data set #6 ('lorem ру́сский язы... ipsum', 'lorem-ру́сский-язы...-ipsum')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'lorem-ру́сский-язы́к-ipsum'
+'lorem-����������������-����������-ipsum'
/home/travis/build/symfony/symfony-demo/tests/AppBundle/Utils/SluggerTest.php:36

2) Tests\AppBundle\Utils\SluggerTest::testSlugify with data set #7 ('lorem العَرَبِيَّ�... ipsum', 'lorem-العَرَبِيَّ�...-ipsum')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'lorem-العَرَبِيَّة‎‎-ipsum'
+'lorem-������������������������‎‎-ipsum'
/home/travis/build/symfony/symfony-demo/tests/AppBundle/Utils/SluggerTest.php:36

@voronkovich
Copy link
Contributor

@javiereguiluz, Could we use the slugs without changing their cases?

@voronkovich
Copy link
Contributor

@javiereguiluz, I think it doesn't work because you use unsupported symbols like у́ (it's not Cyrillic). See https://github.com/symfony/polyfill-mbstring/blob/master/Resources/unidata/lowerCase.php

@javiereguiluz
Copy link
Member Author

@voronkovich I took that from the Wikipedia 😱 Is it wrong?

wikipedia_russian

@@ -27,6 +27,6 @@ class Slugger
*/
public function slugify($string)
{
return trim(preg_replace('/[^a-z0-9]+/', '-', strtolower(strip_tags($string))), '-');
return preg_replace('/\s+/', '-', mb_strtolower(trim(strip_tags($string))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should force the encoding to UTF-8 for mb_strtolower. The internal encoding of mbstring might be configured to a different one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this is very likely, as the default one was not UTF-8 on PHP 5 (PHP 7 changed the default encoding to UTF-8)

@voronkovich
Copy link
Contributor

@javiereguiluz, it's not wrong but those symbols are used rarely. Try to use: русский язык.

@@ -44,7 +44,7 @@ public function getSlugs()
yield ['!Lorem Ipsum!', '!lorem-ipsum!'];
yield ['lorem-ipsum', 'lorem-ipsum'];
yield ['lorem 日本語 ipsum', 'lorem-日本語-ipsum'];
yield ['lorem ру́сский язы́к ipsum', 'lorem-ру́сский-язы́к-ipsum'];
yield ['lorem русский языќк ipsum', 'lorem-русский язык-ipsum'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz, you forgot to delete odd ќ

@javiereguiluz
Copy link
Member Author

javiereguiluz commented Oct 14, 2016

And it's working 🎉 Thank you all for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants