Re-render a OpenWeatherMap component when switching language in Vue 3

I am a beginner using Vue 3, to learn it I am developing a simple weather webapp using OpenWeatherMap API. I have added multilanguage support using vue-i18n library and everything works fine. But there is only one issue, the weather description language that OWM API returns depends on the lang parameter passed in the request. Hence, even if the app language is fully reactive and changes when I switch the language, that weather description is not updating, that is normal because it would need to re-request the API with the proper lang value. But I can’t figure out how to do that. How can I achieve re-rendering that component in order to get the right language in the description returned by the API?

Below is my component with the relevant code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
<script setup>
import axios from "axios";
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const i18nLocale = useI18n();
const route = useRoute();
const getWeatherData = async () => {
const langapi = i18nLocale.locale.value.substring(0, 2);
try {
const weatherData = await axios.get(`https://api.openweathermap.org/data/3.0/onecall?lat=${route.query.lat}&lon=${route.query.lng}&exclude={part}&appid=${myapikey}&units=metric&lang=${langapi}`);
// cal current date & time
const localOffset = new Date().getTimezoneOffset() * 60000;
const utc = weatherData.data.current.dt * 1000 + localOffset;
weatherData.data.currentTime =
utc + 1000 * weatherData.data.timezone_offset;
// cal hourly weather offset
weatherData.data.hourly.forEach((hour) => {
const utc = hour.dt * 1000 + localOffset;
hour.currentTime =
utc + 1000 * weatherData.data.timezone_offset;
});
await new Promise((res) => setTimeout(res, 500));
return weatherData.data;
} catch (error) {
console.log(error);
}
}
const weatherData = await getWeatherData();
// ...
</script>
<template>
// ...
<p class="capitalize">
{{ weatherData.current.weather[0].description }}
</p>
// ...
</template>
</code>
<code> <script setup> import axios from "axios"; import { useRoute, useRouter } from 'vue-router'; import { useI18n } from "vue-i18n"; const { t } = useI18n(); const i18nLocale = useI18n(); const route = useRoute(); const getWeatherData = async () => { const langapi = i18nLocale.locale.value.substring(0, 2); try { const weatherData = await axios.get(`https://api.openweathermap.org/data/3.0/onecall?lat=${route.query.lat}&lon=${route.query.lng}&exclude={part}&appid=${myapikey}&units=metric&lang=${langapi}`); // cal current date & time const localOffset = new Date().getTimezoneOffset() * 60000; const utc = weatherData.data.current.dt * 1000 + localOffset; weatherData.data.currentTime = utc + 1000 * weatherData.data.timezone_offset; // cal hourly weather offset weatherData.data.hourly.forEach((hour) => { const utc = hour.dt * 1000 + localOffset; hour.currentTime = utc + 1000 * weatherData.data.timezone_offset; }); await new Promise((res) => setTimeout(res, 500)); return weatherData.data; } catch (error) { console.log(error); } } const weatherData = await getWeatherData(); // ... </script> <template> // ... <p class="capitalize"> {{ weatherData.current.weather[0].description }} </p> // ... </template> </code>

<script setup>
import axios from "axios";
import { useRoute, useRouter } from 'vue-router';
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const i18nLocale = useI18n();
const route = useRoute();

const getWeatherData = async () => {
    const langapi = i18nLocale.locale.value.substring(0, 2);

    try {
        const weatherData = await axios.get(`https://api.openweathermap.org/data/3.0/onecall?lat=${route.query.lat}&lon=${route.query.lng}&exclude={part}&appid=${myapikey}&units=metric&lang=${langapi}`);

        // cal current date & time
        const localOffset = new Date().getTimezoneOffset() * 60000;
        const utc = weatherData.data.current.dt * 1000 + localOffset;
        weatherData.data.currentTime =
            utc + 1000 * weatherData.data.timezone_offset;

        // cal hourly weather offset
        weatherData.data.hourly.forEach((hour) => {
            const utc = hour.dt * 1000 + localOffset;
            hour.currentTime =
                utc + 1000 * weatherData.data.timezone_offset;
        });

        await new Promise((res) => setTimeout(res, 500));

        return weatherData.data;
    } catch (error) {
        console.log(error);
    }
}

const weatherData = await getWeatherData();

// ...
</script>

<template>
    // ...
    
        <p class="capitalize">
            {{ weatherData.current.weather[0].description }}
        </p>
        
    // ...
</template>

I guess I should refresh the component in order to do another call to the API with the right lang parameter, or to more simply re-invoke the getWeatherData function, but I can’t figure out how to do that.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật