Google 将 API 评论翻译后未以原始语言显示

问题描述 投票:0回答:1

我正在使用 API 来显示评论,但由于某种原因,即使它们最初是斯洛伐克语,当我尝试获取它们时,它们也会被翻译成英语,如下所示:




   author_name
   : 
   "Tomáš Uhliarik"
   author_url
   : 
   "https://www.google.com/maps/contrib/111631361301310521467/reviews"
   language
   : 
   "en-US"
   original_language
   : 
   "sk"
   text
   : 
   "Professional approach and quality work. I recommend"
   translated
   : 
   true



我尝试以另一个人的身份更改请求链接:

“https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJt1LeJK3BFUcRgvatv67YD3c&fields=reviews&reviews_no_translations=true&tranlated=false&key=${apiKey}”

并添加“reviews_no_translation=true”,还有“language=sk”,但我所做的一切仍然在获取翻译后的内容。

这是用于获取的node.js代码:




const express = require("express");
const axios = require("axios");
const cors = require("cors");

const app = express();
const port = 3000;

app.use(cors());

app.get("/reviews", async (req, res) => {
  const placeId = "ChIJt1LeJK3BFUcRgvatv67YD3c";
  const apiKey = ""; // There is a valid one

  try {
    const response = await axios.get(
      `https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJt1LeJK3BFUcRgvatv67YD3c&fields=reviews&reviews_no_translations=true&tranlated=false&key=${apiKey}&language=sk`
    );
    res.json(response.data.result.reviews);
  } catch (error) {
    console.error("Error fetching Google reviews:", error);
    res.status(500).send("Error fetching Google reviews");
  }
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});



这里是用于显示它的 tsx 代码,以防万一:


import { useEffect, useState } from "react";
import axios from "axios";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import "./Home.css";

const Home = () => {
  const [reviews, setReviews] = useState([]);

  useEffect(() => {
    const fetchReviews = async () => {
      console.log(reviews);
      try {
        const response = await axios.get("http://localhost:3000/reviews");
        setReviews(response.data);
      } catch (error) {
        console.error("Error fetching Google reviews:", error);
      }
    };

    fetchReviews();
  }, []);

  const settings = {
    dots: true,
    infinite: true,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1,
  };

  return (
    <div className="home">
      <Slider {...settings}>
        {reviews.map((review) => (
          <div key={review.time}>
            <p>{review.rating}</p>
            <p>{review.author_name}</p>
            <p>{review.text}</p>
          </div>
        ))}
      </Slider>
    </div>
  );
};

export default Home;


reactjs node.js google-api google-places-api google-reviews
1个回答
0
投票

当我对其进行更改时,我必须重新启动 Node.js 服务器,所以最终 google api 可以正常工作。并且还修复了 api 链接中的一些拼写错误,现在如下所示: https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJt1LeJK3BFUcRgvatv67YD3c&fields=reviews&reviews_no_translations=true&translated=false&key=${apiKey}

© www.soinside.com 2019 - 2024. All rights reserved.