Welcome Guest, Not a member yet? Register   Sign In
English Equivalents - url_helper
#1

[eluser]terminate[/eluser]
First I'd like to thank the guys at EllisLab for the great work they have put into CodeIgniter.

I've recently run into a problem where I needed to translate a non English sentence into a url and, even though the sentence had non-English chars (quem és tu miúda), they all had an English alternative (quem-es-tu-miuda).

I've written this "english_equiv" function that should be run right before calling the url_title one.

Code:
<?php $url = "http://www.example.com/".url_title(english_equiv($title_str)) ?>

You can put it inside a file called MY_url_helper.php in application/helpers and it will automatically be available as soon as you load your url_helper.

If EllisLabs wants to include it in any future version of CI, please do.

Sincerely,
Francisco

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Changes chars to their english counter-part
*
* Takes a string as input and outputs it all in English chars
* by Francisco Leite de Castro ([email protected])
*
* @access    public
* @param    string    the string
* @return    string
*/

if (! function_exists('english_equiv'))
{
    function english_equiv($str)
    {
        // Keep on adding characters you want to translate to english locale
        $trans = array (
            "ä" => "a",
            "á" => "a",
            "à" => "a",
            "ã" => "a",
            "ç" => "c",
            "é" => "e",
            "è" => "e",
            "í" => "i",
            "ì" => "i",
            "ó" => "o",
            "ò" => "o",
            "ú" => "u",
            "ù" => "u"
        );
        
        $str = strtr($str, $trans);

        return $str;
    }
}


?>
#2

[eluser]woopsicle[/eluser]
hey thats great - has come in handy for me already!

thanks alot.
#3

[eluser]xwero[/eluser]
Maybe it should be extended into a transliterate function. Other languages have other characters that need to be changed to ascii characters Wink

But i wonder how much the meaning of a word changes if you remove the accents and things like that. Doesn't it enhance the possibility an url gets routed to the wrong entry?
#4

[eluser]terminate[/eluser]
xwero,

Changing accents, in Portuguese, doesn't alter much of the meaning and keeps the sentence much more pleasant to the reader than if you just cut out those odd characters.

"canção" (song) could be written as "cancao" without loosing much of it's meaning where it would loose a lot were you to write it as "cano" (pipe). As for the wrong routing you refer that could be a main concern but in my implementation isn't. The DB has a unique field that stores the "article" route. That insures the uniqueness of the route and allows the user to alter it on the backoffice to anything he likes.

Allowing him to have a title and a different route if it pleases him (very useful for long titles)

That said the function I use above is only run backoffice time suggesting a title based route.




Theme © iAndrew 2016 - Forum software by © MyBB