Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
6 posts Page 1 of 1
Hello Couch users and admins.
I suppose many people faced the problem of auto-transforming article's title into article's name (this is possible for English titles only by default). I've created a simple solution for that like follow (for Couch 1.4):

1. Open "functions.php" in Couch folder. You see on line 236:
Code: Select all
        function get_clean_url( $title ){
            $title = strip_tags( $title ); // remove html tags
            $title = preg_replace('/%([0-9a-fA-F][0-9a-fA-F])/', '', $title); // remove encoded octets
            $title = preg_replace('/&.+?;/', '', $title); // remove html_entities
            $title = $this->remove_accents( $title );
            $title = str_replace( '/', '-', $title );
            $title = preg_replace( '/[^0-9a-zA-Z-_ \.]/', '', $title );
            $title = strtolower( $title );
            $title = str_replace( ' ', '-', $title );
            $title = str_replace( '.', '-', $title );
            $title = preg_replace( '/-+/', '-', $title);
            $title = trim( $title, '-' );
           
            return $title;
        }


2. Change it to:
Code: Select all
        function get_clean_url( $title ){
            $title = strip_tags( $title ); // remove html tags
            $title = preg_replace('/%([0-9a-fA-F][0-9a-fA-F])/', '', $title); // remove encoded octets
            $title = preg_replace('/&.+?;/', '', $title); // remove html_entities
            $title = $this->translit( $title ); // performing transliteration
            $title = $this->remove_accents( $title );
            $title = str_replace( '/', '-', $title );
            $title = preg_replace( '/[^0-9a-zA-Z-_ \.]/', '', $title );
            $title = strtolower( $title );
            $title = str_replace( ' ', '-', $title );
            $title = str_replace( '.', '-', $title );
            $title = preg_replace( '/-+/', '-', $title);
            $title = trim( $title, '-' );
            return $title;
        }
       
        function translit($string) {
            $charlist = array(
               "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
               "Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I",
               "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
               "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
               "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
               "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"","Ы"=>"YI","Ь"=>"",
               "Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b",
               "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
               "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
               "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
               "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
               "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
               "ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"
            );

            return strtr($string,$charlist);
        }


3. Save the file into UTF-8 (without BOM) encoding.

So, that's it for Russian transliteration. I suppose you can easily find a chart for trasliting your language.
This is simple enough but if admins could suggest a way without modifying the core file, it will be great.

Thanks

Attachments

Thank you, Musman :)

To be honest, I was totally unaware of the problem.
There are numerous sites I know of that are in Russian, Chinese, Kanji script etc. but nobody ever complained. Wonder if all input the titles manually?

Anyways, I'd like fellow Couchers working with similar languages to share their experiences regarding this issue.
It is certainly something that needs finding a graceful solution.

Thanks again.
Hello KK,
before this solution non-english users had to write the names (aliases) of articles manually in English. When I first downloaded the Russian translation for Couch the translator clearly wrote:

Article name (automatically decode only English titles)

If not English, the name was Null. It's for French, Arabic, etc. languages, too. Here are some translit tables for many languages: http://www.loc.gov/catdir/cpso/roman.html , but I used in my solution Russian Guidelines for International passport.

Anyway, I think that including this table in Language file would be the best solution, so will be glad to this patch.

UPD: by the way, transliteration is good for SEO, too. Today's searching engines easily highlight translit, for example when you search in Russian "имя" they highlight mysite.com/name.html or mysite.com/imya.html gracefully.
Thank you very much again.

I think none of the default lang files that ship with Couch (i.e. EN, DE, FR, ES and NL) need transliteration.
However, for other language families, placing an optional transliteration table in the lang file is an excellent idea.

I've added this to the TODO list and will get it done soon.

Thanks.
@Musman,

v1.4.5 now tries to address this issue by using PHP 5.4.0 'transliterator_transliterate' function that does the transliteration for you.

However, in case the function is not available on your setup we have added a hook that can be used to insert your own transliterating code into Couch. For example, we can place the code you shared in this thread within 'couch/addons/kfunctions.php' as follows -
Code: Select all
$FUNCS->add_event_listener( 'transliterate_clean_url', 'translit' );

function translit( &$title ){
    $charlist = array(
       "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
       "Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I",
       "Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
       "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
       "У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
       "Ш"=>"SH","Щ"=>"SCH","Ъ"=>"","Ы"=>"YI","Ь"=>"",
       "Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b",
       "в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j",
       "з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
       "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
       "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
       "ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"y",
       "ы"=>"yi","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"
    );

    // modify title
    $title = strtr( $title, $charlist );
}

This way we don't have to modify any core source file of Couch.

Hope it helps.
Thanks! Great.
6 posts Page 1 of 1