Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FisherTsau's avatar

How to let Chinese characters show in URI when using eloquent-sluggable?

I was using the package "https://github.com/cviebrock/eloquent-sluggable" in my application. The slug was set to be same as the title of a certain model. The slug, however, could only be shown when the title is in English. If the title is in Chinese characters, nothing is shown in slug attribute, e.g. $model->slug.

Does any one know the trick so that Chinese Characters could be shown in the 'slug' attribute? Namely, {{$model->slug}} can show the title string in Chinese characters when making view with *.blade.

0 likes
5 replies
gopackup's avatar

I am facing the same problem...Looking for solution...

d3xt3r's avatar

Since most of us are not familiar with Chinese or the eloquent-sluggable package, Can you show what you have done so far, what is it that is not working and what is that you expect, local(e)isation can be tricky.

1 like
zachleigh's avatar

The problem with that package, and many other packages, is that they expect single-byte characters. Chinese characters, however, are multi-byte and normal php string manipulation functions do not work with them. I just had a glance at that package and its filled with multi-byte incompatible functions. You have two options I guess: build your own slug logic, or fork that repo and see if you can get it to work with multi-byte characters.

1 like
FisherTsau's avatar

I tried a walk around approach, and it could work well so far. In "sluggable.php", set the "method" as follows:


function ($string, $separator) {

        //if the input string is in Chinese characters, return the original string
        $slug = (mb_strlen($string, "Big5") == strlen($string)) ?
            $slug = strtolower(preg_replace('/[^A-Za-z0-9]+/i', $separator, $string)) :
            $string;

        return $slug;
    }

If the input string, which should be slugged, is in Chinese characters, just return the original string. Otherwise, use the default conversion mechanism.

Thanks to zachleigh for a good piece of advice. The hint helps a lot.

RayS's avatar

I realize this is an old question, but maybe some still need help with it.

In multi-lingual projects, separate rulesets are often necessary for each supported language in order to avoid conflicts, but that also means that any non-default languages will need to be 'enabled' before they will work.

The (cviebrock/eloquent-sluggable) package relies on another package (cocur/slugify) for its localization rulesets. So, you must ensure that Chinese is included in the 'active' rulesets to make it work the way you need it to work. Below is a link to the core Slugify class, which includes the default ruleset array near the top (before the constructor). It does not include 'Chinese'.

https://github.com/cocur/slugify/blob/main/src/Slugify.php

However, the package does include a Chinese language resource, which you can examine here:

https://github.com/cocur/slugify/tree/main/Resources/rules

So, in your model, add the following just below the sluggable method (and be sure to import both Sluggable & Slugify):

public function customizeSlugEngine(Slugify $engine, $attribute)
{
    $engine->activateRuleset('chinese');
    return $engine;
}

In 2024, that should be enough to get it working.

Please or to participate in this conversation.