Welcome Guest, Not a member yet? Register   Sign In
Proof of concept : multilanguage urls with the help of google translate api
#1

[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.
#2

[eluser]sophistry[/eluser]
very cool. always thinking outside the nine dots, xwero!

i tested the google API with this commandline curl (from their API page):
Code:
curl -e http://www.my-ajax-site.com 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello world 400&langpair=en|it

i added something to the q parameter that would break the code above. can you see it?

nice work. i look forward to getting some juice out of this.

EDIT: just thought of something... what about encoding of URLs? the first english i tried returned a spanish word with an accent...
#3

[eluser]Dam1an[/eluser]
@sopistry, You need the double quotes around the string do you not? At least they're there is xwero's code

Great idea xwero Smile
#4

[eluser]sophistry[/eluser]
[quote author="Dam1an" date="1244141520"]@sopistry, You need the double quotes around the string do you not? At least they're there is xwero's code[/quote]

nope. no need for double-quotes. anyway, it's commandline curl not PHP cURL (for demo only).
#5

[eluser]xwero[/eluser]
You need to url encode the pipes symbol : %7C. I guess the query string will also have to be url encoded, to be on the safe side.

It's not a real solution because further tests i did with other urls made me aware of the fact that the language of single segment urls quite often doesn't get recognized.
Another dealbreaker is that the actual controller and method name has to be what the translate api provides you. So you can only use it with controllers/methods you are in control of. Or you need to run the slugs through google translate as well.
#6

[eluser]Evil Wizard[/eluser]
personally I would have encoded the spaces to either "+" or "%20;" or "2", spaces tend to break URLs

EDIT: had to change the code of the space codes to display the code and not the space character it represents.
#7

[eluser]sophistry[/eluser]
actually, on my (forgiving) system the unencoded pipe and spaces work OK, but the 400 in the q parameter hoses the PHP code because it relies on a fragile strpos rather than decoding the JSON response.

here's to keeping it bubbly.

cheers.
#8

[eluser]xwero[/eluser]
sophistry the 400 in the url string isn't a problem because it's not followed by a curly bracket and i don't think an url string will have a curly bracket in it.

I didn't go for the json php functions because it's a one line response. I think decoding the json object will take longer than using strpos and preg_match.
#9

[eluser]sophistry[/eluser]
doh! didn't notice the curly brace... :red:

anyhow, suggest a tighter solution is using '"responseStatus": 400' string in the strpos.

cheers.
#10

[eluser]xwero[/eluser]
This is the message when the language isn't detected
Quote:{"responseData": null, "responseDetails": "could not reliably detect source language", "responseStatus": 400}
i could do a responseData preg_match check but i wanted to catch as many errors as possible to make the code light. If someone would actually use the code the error messages could be intercepted and routed to the appropriate error page.




Theme © iAndrew 2016 - Forum software by © MyBB