import { onMounted, watch } from 'vue';
import { useI18n } from 'vue-i18n';

/** 挂载时加载一次；语言切换后重新拉取服务端本地化数据 */
export function useOnLocaleChange(loader: () => void | Promise<void>) {
  const { locale } = useI18n();

  onMounted(() => {
    void loader();
  });

  watch(locale, (next, prev) => {
    if (prev && next !== prev) void loader();
  });
}
