关于"i18n"在vue中的使用
<!-- vue2中 -->
<template><div>{{ $t("This campaign has expired.") }}}}</div>
</template>
<script>
export default {created() {this.onLoading();},methods: {onLoading () {this.$t("loading")},}
}
</script>
<!-- vue3中 -->
<template><div>{{ $t("This campaign has expired.") }}}}</div>
</template>
<script setup>
import { onMounted } from "vue";
import { useI18n } from "vue-i18n";const { t } = useI18n();
onMounted(() => {onLoading();
});
onLoading = () => {t("loading")
};
const
</script>
/* 忽略不看
./lang文件夹下有en.json, mm.json({ ”Ok“: "Ok" })
// env.uat 文件下定义
VUE_APP_I18N_LOCALE=mm
VUE_APP_I18N_FALLBACK_LOCALE=mm
*/
import { createI18n } from "vue-i18n";function languageFunc () {const locales = require.context('./lang', true, /[A-Za-z0-9-_,\s]+\.json$/i);const messages = {};locales.keys().forEach(key => {const matched = key.match(/([A-Za-z0-9-_]+)\./i);if (matched && matched.length > 1) {const locale = matched[1]messages[locale] = locales(key)}})return messages
}export default createI18n({locale: process.env.VUE_APP_I18N_LOCALE || 'en',fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || 'en',messages: languageFunc(),legacy: false
})