[eluser]xwero[/eluser]
It's a longtime brainteaser for me to have multilanguage urls instead of adding a language segment/query string to the url. The solutions i came up with required too much maintenance.
Today i was working on some other translation problem and i found out the google translate api's curl urls. And that is when the lightbulb began to shine.
Code:
$ch = curl_init();
$uri_string = substr($_SERVER['PATH_INFO'],1); // remove begin slash
$org_lang = 'en';
curl_setopt($ch, CURLOPT_URL, "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$uri_string."&langpair;=|".$org_lang);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
// no go if the url couldn't be translated
if(strpos($response,'400}') === false)
{
preg_match('/translatedText":"(.+?)","detectedSourceLanguage":"([a-z]{2})/',$response,$matches);
// no go if it's the detected language is the original url language
if($matches[2] != $org_lang)
{
$route[$uri_string] = str_replace(' ','',$matches[1]);
define('DETECTED_LANG',$matches[2]); // for further use on the page
}
}
If you add this to the config/routes.php file the urls can be translated into the language pairs google translate supports.
As this is a proof of concept the code shouldn't be used by production code.