Translate value before sending information

125 views Asked by At

is there a away to translate values in php using either google api translate or any other api...

<?php
   // 1.- Query to get information
   // 2.- build array with that query
   // Example array from query
   $data = array(
          '0' => array (
                   'name' => 'Zapatos',
                   'color' => 'Verde'
                 ),

          '1' => array (
                   'name' => 'Casa',
                   'color' => 'Rosa'
                 ),
           );
       // Now that the array has been build, lets make a translation
       // Which I have no idea how to do that but the final array should be

$final = array(
          '0' => array (
                   'name' => 'Zapatos',
                   'color' => 'Verde',
                   'name_en' => 'Shoes',
                   'color_en' => 'Green'
                 ),

          '1' => array (
                   'name' => 'Casa',
                   'color' => 'Rosa',
                   'name_en' => 'House',
                   'color_en' => 'Pink'
                 ),
           );

is this process possible or am I just dreaming?
I have very little knowledge on how exactly Goolge API works since I only use the Google Translate widget and the translation is after you present the information but in this case we need to make a translation before presenting the information...

1

There are 1 answers

0
bignick On BEST ANSWER

Google translate API is a paid service. You need to get a api key from google api services : google translate API

After that, you can make a curl to google api after getting your results from query : sample url for curl : https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff

You will get the results as JSON object,do json_decode and add results to your array.