Programator PHP | WEB | SEO

SEO: Poti ajunge pe primul loc!

Promovare SEO
Vrei sa fi pe prima pagina a motoarelor de cautare?
Vrei sa atragi mai multi clienti?
Vrei sa iti cresti Cifra de Afaceri?

SEO este solutia pentru tine si afacerea ta!
Vezi pachetele noastre

30 Apr
0 comentarii

Programator PHP – Google Translate script


Google Translate is probably one of the best online translators around. Here’s how to integrate it into your scripts.

AJAX API… What about PHP?

Google offers an AJAX API for translating, but (sadly enough) they don’t offer any options for PHP. We can get around that, though.

Since Google isn’t providing you with their whole language database and algorithms, you know that somewhere in their AJAX script they are requesting data from their server. Using the location that the script requests the data, we can request the data ourselves.

Translation Function

Here’s a function that will translate from one language to another. I’ll explain everything after.

<?php

function translate( $text, $destLang = 'es', $srcLang = 'en' ) {

$text = urlencode( $text );
$destLang = urlencode( $destLang );
$srcLang = urlencode( $srcLang );

$trans = @file_get_contents( "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={$text}&langpair={$srcLang}|{$destLang}" );
$json = json_decode( $trans, true );

if( $json['responseStatus'] != '200' ) return false; else return $json['responseData']['translatedText'];

}
?>

The function is pretty straight forward. It just downloads the translation and then parses it. It will return false if the request fails.

I’m not sure if Google has any API limits on this – I doubt it. The API is designed for websites that want to have their content available in multiple languages. I don’t think that Google would limit the API and potentially hurt anyone who depends on them for translations.

You can find a list of all of the supported languages here. To use any language, you would have to convert the language name (such as ‘English’) to the language code (such as ‘en’). You can use this list to do so. Google uses the ISO 639-1 code.Google Translate is probably one of the best online translators around. Here’s how to integrate it into your scripts.

AJAX API… What about PHP?

Google offers an AJAX API for translating, but (sadly enough) they don’t offer any options for PHP. We can get around that, though.

Since Google isn’t providing you with their whole language database and algorithms, you know that somewhere in their AJAX script they are requesting data from their server. Using the location that the script requests the data, we can request the data ourselves.

Translation Function

Here’s a function that will translate from one language to another. I’ll explain everything after.

<?php

function translate( $text, $destLang = 'es', $srcLang = 'en' ) {

$text = urlencode( $text );
$destLang = urlencode( $destLang );
$srcLang = urlencode( $srcLang );

$trans = @file_get_contents( "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q={$text}&langpair={$srcLang}|{$destLang}" );
$json = json_decode( $trans, true );

if( $json['responseStatus'] != '200' ) return false; else return $json['responseData']['translatedText'];

}
?>

The function is pretty straight forward. It just downloads the translation and then parses it. It will return false if the request fails.

I’m not sure if Google has any API limits on this – I doubt it. The API is designed for websites that want to have their content available in multiple languages. I don’t think that Google would limit the API and potentially hurt anyone who depends on them for translations.

You can find a list of all of the supported languages here. To use any language, you would have to convert the language name (such as ‘English’) to the language code (such as ‘en’). You can use this list to do so. Google uses the ISO 639-1 code.

Continuare