Translation of open weather map api

Hello,

maybe someone has used the weather api ‘open weather map’, and translated it successfully depending on the chosen language of the website?

I can add “&lang=lt” inside this code:

fetch('https://api.openweathermap.org/data/2.5/weather?id=' + cityID + '&units=metric' + '&appid=' + key)

and it will successfully provide descriptions in this specified language. But then translations in that language will appear for both the ‘en’ and ‘lt’ site versions.

I want the translations for the selected language of the website to be displayed.

Any ideas?

You can use $kirby->language()->code() instead of hard-coding the language code.

But it is not possible to write php code inside javascript. If i write it like this:

fetch('https://api.openweathermap.org/data/2.5/weather?id=' + cityID + '&units=metric' + '&lang=<?php $kirby->language()->code(); ?>' + '&appid=' + key)

I get 400 bad request error. Maybe i did not understand you correctly?

Well, no, you cannot write PHP code inside a JS file. This is usually solved via a data attribute or some other attribute (e.g. the lang code in the html element) in your HTML, from which you then fetch the value.

1 Like

Thank you very much for your idea!