-
-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vue.extend does not accept a VueI18n class instance as i18n option #200
Comments
Thank you for your feedback! This issue is occurred due to |
It does have an impact on Unit Testing for this kind of implementation: https://vuejs.org/v2/guide/unit-testing.html#Writing-Testable-Components We are developing a project that now uses vue-i18n. When we implemented it, some warnings appeared on existing tests:
It correctly does any assertion, since the keypath will be compared with it own value and we are not testing the vue-i18n messages itself, but the values that the component uses for internal processing. |
@kimuraz to get around this issue you must pass the i18n object into the constructor not the extend call. const Constructor = Vue.extend(SmallComponent);
const vm = new Constructor({i18n}).$mount() |
@rikbrowning Thank you, I'll give it a try to test it, although I've already solve it mocking vue-i18n instance using vue-test-utils + jest (changed my test tools) :) |
@kimuraz do you have an example repo of your mocked vue-i18n instance via jest? |
close (avoid with #200 (comment)) |
Hmmm, actually there are some use cases other than tests that would be covered by this. const CommonApplication = Vue.extend({
router: new VueRouter({
routes: [
{ path: '/shared', component: shared },
],
}),
i18n: new VueI18n({
locale: 'en',
messages: {
en: {
shared: 'Shared Message'
},
},
}),
});
const fooApplication = new CommonApplication({ el: '#foo' });
// Works as expected and $router contains /shared and /foo routes
fooApplication.$router.addRoutes([ { path: '/foo', component: foo } ]);
// Doesn't work and $i18n doesn't contain 'shared' message, but only 'foo'
fooApplication.$i18n.locale = 'en';
fooApplication.$i18n.mergeLocaleMessage('en', {
foo: 'Foo Message',
});
const barApplication = new CommonApplication({ el: '#bar' });
// Works as expected and $router contains /shared and /bar routes
barApplication.$router.addRoutes([ { path: '/bar', component: bar } ]);
// Doesn't work and $i18n doesn't contain 'shared' message, but only 'bar'
barApplication.$i18n.locale = 'en';
barApplication.$i18n.mergeLocaleMessage('en', {
bar: 'Bar Message',
}); |
@kazupon I've created this fiddle to illustrated the concept: |
In case it helps anyone else I had to also do
|
@kazupon I've found the reason why |
vue & vue-i18n version
2.4.2 & 7.0.5 (using dist from master in fiddle)
Reproduction Link
Minimal example http://jsfiddle.net/Ly55ew8m/2/
Our use case is not represented in that fiddle, see below.
Steps to reproduce
Passing a
VueI18n
instance toVue.extend
is ignored, only a plain oldi18n
options object works.In fiddle just comment and un-comment the relevant lines
What is Expected?
If a class instance would work, we could keep (and export) a reference to the instance and import it in other parts of the application (like vuex actions) to control
vue-i18n
, i.e. locale switch.What is actually happening?
We can not use a class instance so we have to add the locale switch logic inside the
Vue.extend
options: i.e. watch avuex
getter for current language and switch language throughthis.$i18n.locale
.The text was updated successfully, but these errors were encountered: