Skip to content
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

Chinese support #47

Closed
dmyers opened this issue Oct 30, 2014 · 14 comments
Closed

Chinese support #47

dmyers opened this issue Oct 30, 2014 · 14 comments

Comments

@dmyers
Copy link

dmyers commented Oct 30, 2014

Is there a way to get Chinese characters to work?

@florianeckerstorfer
Copy link
Member

You just need to add the corresponding rules.

@danielantelo
Copy link

would be interested in this too, how would you go about it? @dmyers did you manage it?

@florianeckerstorfer
Copy link
Member

I have little to no knowledge about Chinese, but as far as I understand that Chinese doesn't really have what we call letters. That is, you cannot really transliterate a Chinese symbol into an ASCII character (or a series of characters), because Chinese characters are basically words (or at least part of words). Again I'm not 100% sure.

Take a look at FaSlugify (which seems to be based on this library for Farsi/Persian. This project includes another project called finglify which translates a Persian/Farsi word into latin characters. It includes a massive list of words. I guess that a Slugify for Chinese would also need such a list.

@zoransa
Copy link

zoransa commented Aug 30, 2015

I have solved it kind of...

if (!function_exists('slugify')) {

/**
 * Slugify string for urls
 * @param $str
 * @param string $separator
 * @param string $locale
 * @param bool $lowercase
 * @return string
 */
function slugify($str, $separator = "-", $locale = 'en', $lowercase = false)
{
    if (in_array($locale, ['zh'])) {
        $str = transliterator_transliterate('Any-Latin', $str);
    }

    $slugify = new Slugify(null, array('lowercase' => $lowercase));

    $slugify->addRules(
        [
            'א' => '',
            'ב' => 'v',
            'ב' => 'b',
            'ג' => 'g',
            'ג׳' => 'j',
            'ד' => 'd',
            'ד׳' => 'dh',
            'ה' => 'h',
            'ו' => 'v',
            'ז' => 'z',
            'ז׳' => 'zh',
            'ח' => 'h',
            'ט' => 't',
            'י' => 'y',
            'כ' => 'k',
            'ך' => 'kh',
            'ל' => 'l',
            'מ' => 'm',
            'ם' => 'm',
            'נ' => 'n',
            'ן' => 'n',
            'ס' => 's',
            'ע' => '',
            'פ' => 'f',
            'ף' => 'f',
            'צ' => 'ts',
            'ץ' => 'ts',
            'ץ׳' => 'tsh',
            'ק' => 'k',
            'ר' => 'r',
            'ש' => 's',
            'ת' => 't'
        ]
    );
    return $slugify->slugify($str, $separator);
}
}

This adds rules for Hebrew and in case of Chinese it does transliterate but you have to send 'zh' $locale flag to the function.

Also update your composer and add:

     "require": {
           "ext-intl": "*",

@0xuhe
Copy link

0xuhe commented Jan 13, 2016

Check it on php manul

<?php 
preg_match("/\p{Han}+/u", 'test中文', $result); 
var_dump($result);

@Dimimo
Copy link

Dimimo commented Jul 16, 2016

Interesting question for I will sooner or later need this. After some research I have found this solution: http://pastebin.com/zZfci0Dz. The URL won't look nice but it is re-translatable from UTF-8 to Chinese without a loss.

There are easier solutions but they are not reliable and even dangerous. Read this first reply to understand why: http://stackoverflow.com/questions/5998607/conversion-from-simplified-to-traditional-chinese. You could run into problems. Something better to avoid as a web-designer. Use numbered url's instead.

@rny
Copy link

rny commented Aug 28, 2016

        $slugify = new \Cocur\Slugify\Slugify();
        echo $slugify->slugify('中文');

echo empty, should be unchanged string '中文'.

@picks44
Copy link

picks44 commented Sep 20, 2016

+1 for the addition of the Chinese/pinyin

@zoransa
Copy link

zoransa commented Sep 23, 2016

In order to make it Chinese/pinyin just modify Chinese string with

$str = transliterator_transliterate('Any-Latin', $str);

don't forget php requires "ext-intl": "*",

@SuN-80
Copy link
Contributor

SuN-80 commented Nov 17, 2016

I suppose it could be easy to implement using a class like this as an addon: https://github.com/jifei/Pinyin/blob/master/Pinyin.php

@SuN-80
Copy link
Contributor

SuN-80 commented Feb 21, 2017

Added Chinese support (pinyin): #158

@hifall
Copy link

hifall commented Jun 2, 2017

#158 PR still not approved -- any progress? Thanks!

@SuN-80
Copy link
Contributor

SuN-80 commented Jun 6, 2017

I don't know why checks are not successful, but it works: https://getsongbpm.com/artist/zhoujie/3vJn
Unless someone can tell me what's wrong with conflicting files, you'll have to add Chinese rules manually and generate your own set of rules (like i did in the example above) using #158 .

@florianeckerstorfer
Copy link
Member

Fixed by #195

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

No branches or pull requests

10 participants