Skip to content

Commit b14b4ac

Browse files
committed
Translate custom text
1 parent f1288fc commit b14b4ac

File tree

8 files changed

+264
-105
lines changed

8 files changed

+264
-105
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,66 @@ Vue.use(loading, {
6868
})
6969
```
7070

71+
### Translate your custom text
72+
In your `main.js` file
73+
74+
```js
75+
import Vue from 'vue'
76+
import VueI18n from 'vue-i18n'
77+
import loading from 'vuejs-loading-screen'
78+
79+
// your i18n setup
80+
Vue.use(VueI18n)
81+
82+
const messages = {
83+
en: {
84+
message: {
85+
loading: 'Loading...'
86+
}
87+
},
88+
km: {
89+
message: {
90+
loading: 'កំពុងដំណើរការ...'
91+
}
92+
}
93+
}
94+
95+
const i18n = new VueI18n({
96+
locale: 'en', // set locale
97+
messages, // set locale messages
98+
})
99+
100+
// config loading plugins
101+
Vue.use(loading, {
102+
bg: '#41b883ad',
103+
slot: `
104+
<div class="px-5 py-3 bg-gray-800 rounded">
105+
<h3 class="text-3xl text-white"><i class="fas fa-spinner fa-spin"></i> ${ i18n.t('message.loading') }</h3>
106+
</div>
107+
`,
108+
})
109+
110+
new Vue({
111+
i18n,
112+
...
113+
}).$mount('#app');
114+
115+
```
116+
117+
To update your custom text, in `App.vue`
118+
119+
```js
120+
watch: {
121+
'$i18n.locale' () {
122+
this.$changeIsLoadingOptions({slot: `
123+
<div class="px-5 py-3 bg-gray-800 rounded">
124+
<h3 class="text-3xl text-white"><i class="fas fa-spinner fa-spin"></i> ${ this.$t('message.loading') }</h3>
125+
</div>
126+
`})
127+
}
128+
}
129+
```
130+
71131
## Configurations
72132

73133
| Option | Value | Description |

dev/i18n.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Vue from 'vue';
2+
import VueI18n from 'vue-i18n';
3+
4+
Vue.use(VueI18n)
5+
6+
const messages = {
7+
en: {
8+
message: {
9+
loading: 'Loading...'
10+
}
11+
},
12+
km: {
13+
message: {
14+
loading: 'កំពុងដំណើរការ...'
15+
}
16+
}
17+
}
18+
19+
const i18n = new VueI18n({
20+
locale: 'en', // set locale
21+
messages, // set locale messages
22+
})
23+
24+
25+
export default i18n

dev/serve.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import Vue from 'vue';
22
import Dev from './serve.vue';
3-
// To register individual components where they are used (serve.vue) instead of using the
4-
// library as a whole, comment/remove this import and it's corresponding "Vue.use" call
5-
import VuejsLoadingScreen from '@/entry.esm';
6-
Vue.use(VuejsLoadingScreen, {
3+
import i18n from './i18n';
4+
import IsLoading from '@/entry.esm';
5+
6+
const isLoadingOptions = {
77
bg: '#41b883ad',
88
slot: `
99
<div class="px-5 py-3 bg-gray-800 rounded">
10-
<h3 class="text-3xl text-white"><i class="fas fa-spinner fa-spin"></i> Loading...</h3>
10+
<h3 class="text-3xl text-white"><i class="fas fa-spinner fa-spin"></i> ${ i18n.t('message.loading') }</h3>
1111
</div>
12-
`
13-
});
12+
`,
13+
i18n
14+
}
15+
16+
Vue.use(IsLoading, isLoadingOptions);
1417

1518
Vue.config.productionTip = false;
19+
Vue.prototype.isLoadingOptions = isLoadingOptions
1620

1721
new Vue({
22+
i18n,
1823
render: (h) => h(Dev),
1924
}).$mount('#app');

dev/serve.vue

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ export default Vue.extend({
1313
return new Promise((resolve) => setTimeout(resolve, milliseconds));
1414
},
1515
},
16+
watch: {
17+
'$i18n.locale' () {
18+
this.$changeIsLoadingOptions({slot: `
19+
<div class="px-5 py-3 bg-gray-800 rounded">
20+
<h3 class="text-3xl text-white"><i class="fas fa-spinner fa-spin"></i> ${ this.$t('message.loading') }</h3>
21+
</div>
22+
`})
23+
}
24+
}
1625
});
1726
</script>
1827

@@ -21,12 +30,20 @@ export default Vue.extend({
2130
<div class="flex justify-center items-center h-screen w-screen">
2231
<div class="text-center">
2332
<h3 class="mb-5 text-2xl">Vuejs Loading Screen</h3>
24-
<button
25-
class="px-4 py-2 bg-blue-800 rounded text-white"
26-
@click="showLoading"
27-
>
28-
Send HTTP Request
29-
</button>
33+
<div class="space-x-2">
34+
<button
35+
class="px-4 py-2 bg-blue-800 rounded text-white"
36+
@click="showLoading"
37+
>
38+
Send HTTP Request {{ $t('message.loading') }}
39+
</button>
40+
<button
41+
class="px-4 py-2 bg-blue-800 rounded text-white"
42+
@click="() => {$i18n.locale === 'en' ? $i18n.locale = 'km' : $i18n.locale = 'en'}"
43+
>
44+
Now Locale is: {{ $i18n.locale }}
45+
</button>
46+
</div>
3047
</div>
3148
</div>
3249
</div>

0 commit comments

Comments
 (0)