Skip to content

Commit 59f784d

Browse files
committed
update readme
1 parent 45b4495 commit 59f784d

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

readme.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,62 @@ var app = new Vue({
9797
You can specify a custom module name for vuex (default is 'i18n') or a callback that is triggered
9898
when a key has no translation for the current locale. Please note, that the function
9999
supplied for onTranslationNotFound will be called if the key is not in the actual
100-
locale, however, the key might still be available in the fallback locale or a
101-
localized locale version. This might need some further work in the future.
100+
locale or a parent locale (ie. en for en-us), however, the key might still be available
101+
in the fallback locale.
102+
103+
If a return value is given, this will be used as translation text for the key
104+
that was not found. It is also possible to return a promise. This will allow you
105+
to dynamically fetch the data from an api. Be aware, that the key will only
106+
be resolved once and then written like any other key into the store. Therefore
107+
subsequent calls of the same key will not trigger the onTranslationNotFound method.
102108

103109
```javascript
110+
111+
// without return value (will use fallback translation, default translation or key)
104112
Vue.use(vuexI18n.plugin, store, {
105113
moduleName: 'i18n',
106114
onTranslationNotFound (locale, key) {
107115
console.warn(`i18n :: Key '${key}' not found for locale '${locale}'`);
108116
}}
109117
);
118+
119+
// with string as return value. this will write the new value as translation
120+
// into the store
121+
// note: synchronous resolving of keys is not recommended as this functionality
122+
// should be implemented in a different way
123+
Vue.use(vuexI18n.plugin, store, {
124+
moduleName: 'i18n',
125+
onTranslationNotFound (locale, key) {
126+
switch(key) {
127+
case: '200':
128+
return 'Everything went fine';
129+
break;
130+
default:
131+
return 'There was a problem';
132+
}
133+
}}
134+
);
135+
136+
// with promise as return value. this will write the new value into the store,
137+
// after the promise is resolved
138+
Vue.use(vuexI18n.plugin, store, {
139+
moduleName: 'i18n',
140+
onTranslationNotFound (locale, key) {
141+
142+
return new Promise((resolve, reject) => {
143+
axios.get('/api/translations/async', {locale: locale, key:key})
144+
.then((result) => {
145+
resolve(result.data);
146+
147+
}).catch() {
148+
reject();
149+
}
150+
151+
})
152+
153+
}}
154+
);
155+
110156
```
111157

112158
## Usage

0 commit comments

Comments
 (0)