php 如何翻译JSON API特定字段并将翻译存储在DB中

wr98u20j  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(46)

我使用Laravel和React模板构建了一个实时分数网站,我使用API来获取json格式的实时数据,如下所示:

[
{
    "country_id": "6",
    "country_name": "Spain",
    "league_id": "300",
    "league_name": "Copa del Rey",
    "league_season": "2020/2021",
    "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/300_copa-del-rey.png",
    "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
},
{
    "country_id": "6",
    "country_name": "Spain",
    "league_id": "302",
    "league_name": "La Liga",
    "league_season": "2020/2021",
    "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/302_la-liga.png",
    "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
},

字符串
我喜欢这样来获得价值观:

$APIkey='xxxxxxxxxxxxxx';
$country_id = 6;

$curl_options = array(
CURLOPT_URL => "https://apiv3.apifootball.com/? 
action=get_leagues&country_id=$country_id&APIkey=$APIkey",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_CONNECTTIMEOUT => 5
);

$curl = curl_init();
curl_setopt_array( $curl, $curl_options );
$result = curl_exec( $curl );

$result = (array) json_decode($result);

var_dump($result);


我所寻找的是在前端显示之前对一些特定的json字段进行翻译,我这样阐明我的想法:

------------------------------------------------------------------------
JSON file from API ======> make translations ===>displaying in view
                                             ||
                                             ||
                           getting translations words from database

的字符串
例如Copa del Rey翻译它到Coupe du Roi谁这个词存储在数据库中的法语语言?
任何想法?

3npbholx

3npbholx1#

方法

这里我并没有假设您有不同的翻译文件,如french_translations.jsonspanish_translation.json等,因为您的请求不是从English翻译成其他语言,而是相反。因此,理想情况下,您的数据库模型(文件存储或任何其他存储)希望拥有所有键(所有语言)及其English
下面是这样一个模型的例子(假设文件存储在这里,文件名为translations.json):

{
  "Copa del Rey": "Coupe du Roi",
   ... // your rest of the `key-value` pairs
}

字符串
您从数据库收到的实时数据如下:

live_data = [{
    "country_id": "6",
    "country_name": "Spain",
    "league_id": "300",
    "league_name": "Copa del Rey",
    "league_season": "2020/2021",
    "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/300_copa-del-rey.png",
    "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
},
{
    "country_id": "6",
    "country_name": "Spain",
    "league_id": "302",
    "league_name": "La Liga",
    "league_season": "2020/2021",
    "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/302_la-liga.png",
    "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
}]


在您的API中,您可以执行以下操作:

import translation_list from 'translations.json'; // import the translation file

// Using the `map` operation, translate the desired key(s) 
// and return the result (`translatedLiveData`) to the client
translatedLiveData = live_data.map(item => {
  return {
   ...item,
   'league_name': translation_list[item['league_name']]
  }
});


希望我能给予你一个更好的图片。让我知道如果你需要更多的了解这里。

相关问题