dkfbasel / vuex-i18n

Localization plugin for vue.js 2.0 using vuex as store
MIT License
666 stars 56 forks source link

Hook custom logic for non existent keys in lang #109

Closed Erwin32 closed 5 years ago

Erwin32 commented 5 years ago

I was looking for some way to register some callback/interceptor on failed key lookup so that I could send a request to server API we have that would notify the server of the missing translation.

I didn't find any way to do this using the library functions. I could introduce custom vue filter and some wrapper method around $t method as last resort tho I wouldn't prefer that.

tikiatua commented 5 years ago

Hi @Erwin32

This functionality is actually available. You need to pass a respective handler function as onTranslationNotFound property when initializing the store. You can find an example in the readme.

// with promise as return value. this will write the new value into the store,
// after the promise is resolved
Vue.use(vuexI18n.plugin, store, {
    moduleName: 'i18n',
    onTranslationNotFound (locale, key) {

        return new Promise((resolve, reject) => {
            axios.get('/api/translations/async', {locale: locale, key:key})
            .then((result) => {
                resolve(result.data);

            }).catch() {
                reject();
            }

        })

    }}
);
Erwin32 commented 5 years ago

oh great must have missed that, thx