Code Monkey home page Code Monkey logo

Comments (14)

florianeckerstorfer avatar florianeckerstorfer commented on May 27, 2024

You just need to add the corresponding rules.

from slugify.

danielantelo avatar danielantelo commented on May 27, 2024

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

from slugify.

florianeckerstorfer avatar florianeckerstorfer commented on May 27, 2024

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.

from slugify.

zoransa avatar zoransa commented on May 27, 2024

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": "*",

from slugify.

0xuhe avatar 0xuhe commented on May 27, 2024

Check it on php manul

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

from slugify.

Dimimo avatar Dimimo commented on May 27, 2024

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.

from slugify.

rny avatar rny commented on May 27, 2024
        $slugify = new \Cocur\Slugify\Slugify();
        echo $slugify->slugify('中文');

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

from slugify.

picks44 avatar picks44 commented on May 27, 2024

+1 for the addition of the Chinese/pinyin

from slugify.

zoransa avatar zoransa commented on May 27, 2024

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": "*",

from slugify.

SuN-80 avatar SuN-80 commented on May 27, 2024

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

from slugify.

SuN-80 avatar SuN-80 commented on May 27, 2024

Added Chinese support (pinyin): #158

from slugify.

hifall avatar hifall commented on May 27, 2024

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

from slugify.

SuN-80 avatar SuN-80 commented on May 27, 2024

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 .

from slugify.

florianeckerstorfer avatar florianeckerstorfer commented on May 27, 2024

Fixed by #195

from slugify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.