在发送信息之前翻译价值

时间:2015-06-25 23:54:36

标签: php google-translate machine-translation

是否可以使用google api translate或任何其他api翻译php中的值...

<?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'
                 ),
           );

这个过程是可能的还是我只是在做梦? 我对Goolge API的工作原理知之甚少,因为我只使用Google Translate小部件,翻译是在您提供信息之后,但在这种情况下我们需要在提供信息之前进行翻译......

1 个答案:

答案 0 :(得分:0)

Google翻译API是一项付费服务​​。您需要从google api服务获取api密钥: google translate API

之后,您可以在从查询中获得结果后对google api进行卷曲: 卷曲样本网址: https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff

您将获得结果作为JSON对象,执行json_decode并将结果添加到您的数组。