【问题标题】:Is there a way to translate API Information (alexa developer console, -pip install dont work)有没有办法翻译 API 信息(alexa 开发者控制台,-pip install 不起作用)
【发布时间】:2021-12-02 17:21:03
【问题描述】:

我目前有一个用于天气意图的 API,但我需要翻译主要信息,如雪、太阳、雨等。有没有人知道如何在将它们发送到 speak_output 之前翻译它们?

class wetterHandler(AbstractRequestHandler):
    """Handler for Get Weather Intent."""
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("wetter")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        api_url= "http://api.openweathermap.org/data/2.5/weather?q=München&appid=b9ad6b1c8e80f623ff23a53cdd8832a9&units=metric"
        json_data = requests.get(api_url).json()
        weather_json = json_data['weather'][0]['main']
        temp_json = round(json_data['main']['temp'] )
        city = "München"
        speak_output = "The weather is {} and temperature is {} °C in {}. " .format(weather_json, temp_json, city)
        reprompt = "Want to try again? Or you can say cancel to quit."
        handler_input.response_builder
        
        return (
            handler_input.response_builder
                .speak(speak_output)
                .ask(reprompt)
                .response
        )

【问题讨论】:

    标签: python amazon-web-services alexa-skills-kit


    【解决方案1】:

    您可以像这样在请求 URL 中简单地 specify the language

    http://api.openweathermap.org/data/2.5/weather?id=524901&appid={API key}&lang={lang}
    

    因此,要获得德语天气,您需要向以下地址提出请求:

    http://api.openweathermap.org/data/2.5/weather?q=München&appid=b9ad6b1c8e80f623ff23a53cdd8832a9&units=metric&lang=de
    

    【讨论】:

    • 非常感谢,对我帮助很大。
    【解决方案2】:

    已解决: 获得了 API 的所有可能返回,并在代码打印前放了一个简单的 if:

    def handle(self, handler_input):
            # type: (HandlerInput) -> Response
            api_url= "http://api.openweathermap.org/data/2.5/weather?q=München&appid=b9ad6b1c8e80f623ff23a53cdd8832a9&units=metric"
            json_data = requests.get(api_url).json()
            weather_json = json_data['weather'][0]['main']
            if weather_json == "Snow":
                weather_json = "am Schneien"
            if weather_json == "Rain":
                weather_json = "Regnerisch"
            if weather_json == "Clouds":
                weather_json = "Bewölkt"
            if weather_json == "Sun":
                weather_json = "Sonnig"
            if weather_json == "Wind":
                weather_json = "Windig"
            temp_json = round(json_data['main']['temp'] )
            city = "München"
            speak_output = "Aktuell ist es {} und die Temperatur beträgt {} °C in {}. Wie sollen wir fortfahren? " .format(weather_json, temp_json, city)
            reprompt = "Möchtest du nochmal das Wetter abfragen?"
            handler_input.response_builder
            
            return (
                handler_input.response_builder
                    .speak(speak_output)
                    .ask(reprompt)
                    .response
            )
    

    【讨论】:

    • 你把这个弄得太复杂了,看我的回答
    猜你喜欢
    • 2018-03-09
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2019-08-07
    • 2015-06-21
    • 2020-01-25
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多