cviebrock / eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel
MIT License
3.9k stars 459 forks source link

Problem with Persian and Arabic languages #427

Closed ali-mhz closed 6 years ago

ali-mhz commented 6 years ago

Hi I'm using laravel 5.4 and eloquent-sluggable 4.2.5 in one of my apps which is in 2 languages: English and Persian.

This works perfect with English but for Persian it generates strange slugs. For example if i have "تست" then result is "tst" but i expect to get "تست" or if i have this "این یک تست است" (it means 'this is a test') the result is "n-tst-st" but i expect to have this "این-یک-تست-است"

DJafari commented 6 years ago

https://github.com/morilog/eluquent-sluggable-persian

i think this config.php can help to you

پ.ن: انجین پیش فرضش می خواد تلاش کنه کلمه رو ترجمه کنه بعد تبدیل به اسلاگ کنه ولی ناتوانه، این لینکی که بهت دادم انجین رو یه متدی می گذاره که حروف اضافی رو پاک می کنه و بینش خط فاصله می گذاره واسه هر دو زبان هم کار می کنه

cviebrock commented 6 years ago

By default, the slugging engine we use does a "transliteration" of the slug into ASCII characters. The assumption was that people would use this library to generate URLs (which can use UTF characters, but tend to be ASCII-only).

If you want to keep your slugs in Persian, you could probably do something like @DJafari suggested, or just use the following in your config:

'method' => function ($string, $separator) {
    $slug = mb_strtolower(
        preg_replace('/([?]|\p{P}|\s)+/u', $separator, $string)
    );
    return trim($slug, $separator);
}
ali-mhz commented 6 years ago

@cviebrock This works but i cannot cache my config file. Any solution?

cviebrock commented 6 years ago

You can't cache configs that use closures, correct.

Putting that logic into a helper function or class/method, and then referencing that in the config should work. See the second example here.

ali-mhz commented 6 years ago

@cviebrock Thank you so much, i searched a lot for this, don't know how i didn't notice it.