No translations when vue-i18n used inside library
See original GitHub issueRelated to https://github.com/kazupon/vue-i18n/issues/505 and https://github.com/kazupon/vue-i18n/issues/670
Context
I’m creating a package to share some vue components to my app. Both the package and the app are in vuejs and use vue-i18n to handle translations.
I build the package with the --target option.
Issue
When working on the package as a stand-alone (ie. local mode or building the package without the --target option), all translations displays correctly.
When building the package and using it inside the parent app, the translations does not show. The translation key is visible instead.
Code extract
The package (my-package)
I’m using the following command to build the package:
$vue-cli-service build --target lib --name my-package ./src/all-components.js
// src/main.js (used in dev mode / stand-alone version of the package)
import Vue from 'vue';
import App from './App.vue';
import { i18n, loadLanguage } from './plugin/i18n';
new Vue({
i18n,
beforeCreate() {
loadLanguage('fr');
},
render: (h) => h(App),
}).$mount('#app');
// src/all-components.js (used to build and share the package)
import MySharedComponent1 from '@/components/my-shared-component-1.vue';
import MySharedComponent2 from '@/components/my-shared-component-2.vue';
export default { MySharedComponent1, MySharedComponent2 };
// src/components/my-shared-component-1.vue
<template name="my-shared-component-1">
<main>
<h1 v-t="'article.title'" />
<p>
{{ $t('article.title') }}
<span v-text="$tc('article.comments', count, { x: count })" />
</p>
</main>
</template>
<script>
import { loadLanguage } from '@/plugin/i18n';
export default {
name: 'MySharedComponent1',
props: {
language: {
type: String,
default: 'fr',
},
},
data() {
return {
count: 4,
};
},
created() {
loadLanguage(this.language);
},
};
</script>
// src/plugins/i18n.js
import Vue from 'vue';
import VueI18n from 'vue-i18n';
import de from '@/locales/de.json';
import en from '@/locales/en.json';
import fr from '@/locales/fr.json';
const languages = { de, en, fr };
Vue.use(VueI18n);
const i18n = new VueI18n({
locale: 'fr',
fallbackLocale: 'fr',
messages: { fr: languages.fr },
silentTranslationWarn: process.env.NODE_ENV === 'production',
});
function loadLanguage(lang) {
if (i18n.locale === lang) {
return;
}
const currentLang = languages[lang] ? lang : 'fr';
i18n.setLocaleMessage(currentLang, languages[currentLang]);
i18n.locale = currentLang;
document.querySelector('html').setAttribute('lang', currentLang);
}
export {
i18n,
loadLanguage,
};
The app (my-app)
// src/pages/home.vue
<i18n>
{
"fr": {
"title": "Accueil"
},
"de": {
"title": "Startseite"
},
"en": {
"title": "Homepage"
}
}
</i18n>
<template>
<div>
<my-shared-component-1 language="en" />
...
</div>
</template>
<script>
import { mapGetters } from 'vuex';
import { MySharedComponent1 } from '@namespace/my-package';
export default {
name: 'Home',
components: {
MySharedComponent1,
},
head() {
return {
title: this.$t('title'),
};
},
};
</script>
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:10 (1 by maintainers)
Top Results From Across the Web
How to use Vue-i18n within component library - Get Help
The goal is that the components would include english and french internally, but we would have option of users providing their own translations....
Read more >No translations when vue-i18n used inside library - Issuehunt
When building the package and using it inside the parent app, the translations does not show. The translation key is visible instead. Code...
Read more >How to localize my Vue components library? - Stack Overflow
Show activity on this post. I develop my own vue components library, and this components have a lot of texts that needs to...
Read more >The Ultimate Vue Localization Guide | Phrase
A common use case, interpolating values that change at runtime in our messages is easy with Vue I18n. Our <Coords> component, which shows...
Read more >How to translate your Vue.js application with vue-i18n
The second part of the tutorial explains how you can use BabelEdit to easily manage your translations. This includes fast pre-translation using machine ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So, I found something interesting.
In the generated package bundle, it seems that there is some conflicts between the vue-i18n of the package and the vue-i18n of the parent app.
Extract of the package
@namespace/my-package/dist/my-package.common.js
:When I comment the indicated lines, the translations of my-package appear. However, the translations of my-app are lost instead.
There are many more similar cases in the generated package. It feels that the scope of the package’s vue-i18n is not properly closed.
In your Shared Library project, You need to external vue-i18n with
vue.config.js
.e.g. vue.config.js configration: