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

wr98u20j  于 2024-01-05  发布在  PHP
关注(0)|答案(1)|浏览(177)

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

  1. [
  2. {
  3. "country_id": "6",
  4. "country_name": "Spain",
  5. "league_id": "300",
  6. "league_name": "Copa del Rey",
  7. "league_season": "2020/2021",
  8. "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/300_copa-del-rey.png",
  9. "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
  10. },
  11. {
  12. "country_id": "6",
  13. "country_name": "Spain",
  14. "league_id": "302",
  15. "league_name": "La Liga",
  16. "league_season": "2020/2021",
  17. "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/302_la-liga.png",
  18. "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
  19. },

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

  1. $APIkey='xxxxxxxxxxxxxx';
  2. $country_id = 6;
  3. $curl_options = array(
  4. CURLOPT_URL => "https://apiv3.apifootball.com/?
  5. action=get_leagues&country_id=$country_id&APIkey=$APIkey",
  6. CURLOPT_RETURNTRANSFER => true,
  7. CURLOPT_HEADER => false,
  8. CURLOPT_TIMEOUT => 30,
  9. CURLOPT_CONNECTTIMEOUT => 5
  10. );
  11. $curl = curl_init();
  12. curl_setopt_array( $curl, $curl_options );
  13. $result = curl_exec( $curl );
  14. $result = (array) json_decode($result);
  15. var_dump($result);


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

  1. ------------------------------------------------------------------------
  1. JSON file from API ======> make translations ===>displaying in view
  2. ||
  3. ||
  4. getting translations words from database

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

3npbholx

3npbholx1#

方法

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

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

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

  1. live_data = [{
  2. "country_id": "6",
  3. "country_name": "Spain",
  4. "league_id": "300",
  5. "league_name": "Copa del Rey",
  6. "league_season": "2020/2021",
  7. "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/300_copa-del-rey.png",
  8. "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
  9. },
  10. {
  11. "country_id": "6",
  12. "country_name": "Spain",
  13. "league_id": "302",
  14. "league_name": "La Liga",
  15. "league_season": "2020/2021",
  16. "league_logo": "https://apiv3.apifootball.com/badges/logo_leagues/302_la-liga.png",
  17. "country_logo": "https://apiv3.apifootball.com/badges/logo_country/6_spain.png"
  18. }]


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

  1. import translation_list from 'translations.json'; // import the translation file
  2. // Using the `map` operation, translate the desired key(s)
  3. // and return the result (`translatedLiveData`) to the client
  4. translatedLiveData = live_data.map(item => {
  5. return {
  6. ...item,
  7. 'league_name': translation_list[item['league_name']]
  8. }
  9. });


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

展开查看全部

相关问题