Search code examples
pythonamazon-web-servicesalexa-skills-kit

Is there a way to translate API Information (alexa developer console, -pip install dont work)


I currently have an API for my weather intent, but I need to translate the main Information like Snow, sun, rain and so on. Has anyone an Idea how I can translate them before it goes to the 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
        )

Solution

  • You can simply specify the language in your request URL like so:

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

    So to get the weather in German you would make a request to:

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