PHP code example of yak0d3 / mirza_yandex_translator
1. Go to this page and download the library: Download yak0d3/mirza_yandex_translator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$es_translation = Mirza::translate('Hello World!', 'es); //The first param is the text, the second one is the ISO code of the language
echo $es_translation; //This will output "Hola Mundo!"
$textArray = [
'header' => "Welcome to the Mirza Documentation Page",
'body' => "The body is too long to be put in this item",
'footer' => "Thank you for reading this!"
]; //Our associative text array
$jsonString = Marzi::translate($textArray,'es', true); //Notice that i have set $assoc (third param) to `true`
$translationsArray = json_decode($jsonString, true);
//Now you can access the translations by their old index names
$header = $translationsArray['header']['translatedText'];
$body = $translationsArray['body']['translatedText'];
$footer = $translationsArray['footer']['translatedText'];
//Leave the $name param empty or set it to `false`
//To return the language ISO code
$lang = Mirza::detectLanguage('Hello World!');
echo $lang; //Outputs "en"
//Setthe $name param to `true`
//To return the language ISO code
$lang = Mirza::detectLanguage('Hello World!', true);
echo $lang; //Outputs "English"
//Save the json encoded string to the `$supportedLanguages` variable
$supportedLanguages = Mirza::getSupportedLanguages();
echo $supportedLanguages;
/* Outputs the json string in the following format:
[
{ 'lang_code' => 'lang_name' },
{ 'lang_code' => 'lang_name' },
]
*/
//Decode json string and wrap it into a PHP array
$langsArray = json_decode($supportedLanguages, true);
echo $langsArray['tr']; //Outputs "Turkish"
$flippedArray = array_flip($langsArray);
/* The values are now keys! Cool right? */
$languageCode = $flippedArray['Sinhalese'];
echo $languageCode; //Outputs "si"
//Save the json string to a variable
$myStringInAllLanguages = Mirza::translateToAll('My string');
echo $myStringInAllLanguages;
/*Outputs a similar string to the `translateTo` method but
with all supported languages*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.