Skip to content

Commit cf18077

Browse files
authored
Update ChangeLocale.vue
1 parent 465685c commit cf18077

File tree

1 file changed

+72
-50
lines changed

1 file changed

+72
-50
lines changed
Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,104 @@
1-
<!--
2-
// https://vue-i18n.intlify.dev/guide/advanced/composition.html#global-scope
3-
// Add in main i18n options main.js
4-
5-
import { createI18n } from 'vue-i18n'
6-
7-
const lang = {
8-
// Allow compositions api (required)
9-
allowComposition: true,
10-
globalInjection: true,
11-
legacy: false,
12-
13-
// Locales
14-
locale: 'en',
15-
fallbackLocale: 'en',
16-
availableLocales: ['en', 'pl'],
17-
messages: {
18-
en: { en: "English", pl: "Polish" },
19-
pl: { en: "Angielski", pl: "Polski" },
20-
},
21-
}
22-
23-
const i18n = createI18n(lang)
24-
app.use(i18n)
25-
-->
26-
271
<template>
28-
<div class="locale-changer">
29-
<select v-model="locale" class="locale-changer-select">
30-
<option v-for="lang in availableLocales" :key="`locale-${lang}`" :value="lang">{{ t(lang) }}</option>
2+
<div class="change-locale">
3+
<select v-model="locale" class="locale-select">
4+
<option v-for="lang in availableLocales" :key="`locale-${lang}`" :value="lang">
5+
{{ t(lang) }}
6+
</option>
317
</select>
328
</div>
339
</template>
3410

3511
<script setup>
36-
import { watch, computed, onMounted } from 'vue'
37-
import { useAuthStore } from '@/stores/auth.js'
12+
import axios from 'axios'
3813
import { useI18n } from 'vue-i18n'
14+
import { watch, onMounted } from 'vue'
3915
4016
const { t, locale, availableLocales } = useI18n({ useScope: 'global' })
41-
const store = useAuthStore()
17+
18+
const props = defineProps({
19+
locale_url: { default: '/web/api/locale/' },
20+
})
4221
4322
onMounted(() => {
44-
console.log('Current locale', locale.value, availableLocales)
23+
console.log('Change locale', locale.value, availableLocales)
4524
})
4625
4726
watch(
4827
() => locale.value,
4928
(lang) => {
50-
console.log('Changed locale', lang)
51-
store.changeLocale(lang)
29+
changeLocale(lang)
5230
}
5331
)
5432
55-
const msg = computed(() => t('example.msg'))
33+
async function changeLocale(locale) {
34+
if (props.locale_url) {
35+
try {
36+
// Server update
37+
// await axios.get(props.locale_url + locale)
38+
} catch (err) {
39+
console.log('Change locale error', err)
40+
}
41+
}
42+
}
5643
</script>
5744

5845
<style scoped>
59-
.locale-changer {
46+
.change-locale {
6047
float: right;
6148
width: auto;
62-
height: auto;
63-
padding: 5px;
49+
height: 44px;
50+
margin: 5px;
51+
background: var(--bg-primary);
52+
border-radius: var(--border-radius);
53+
border: 0px solid var(--divider-primary);
6454
}
65-
.locale-changer-select {
66-
float: right;
67-
display: inline;
68-
padding: 2px;
69-
text-align: center;
55+
.change-locale .locale-select {
56+
box-sizing: border-box;
57+
min-width: 60px;
58+
height: 42px;
59+
float: left;
7060
border: 0px;
71-
font-size: 1rem;
7261
cursor: pointer;
62+
text-align: center;
63+
padding-left: 5px;
64+
font-size: 14px;
65+
background: var(--bg-primary);
66+
border-radius: var(--border-radius);
7367
}
74-
.locale-changer-select > * {
75-
background: #fff;
76-
color: #222;
68+
.change-locale .locale-select option,
69+
.change-locale .locale-select > * {
70+
background: var(--bg-primary);
71+
color: var(--text-primary);
7772
}
78-
.locale-changer-select:focus {
73+
.change-locale .locale-select:focus {
7974
border: none;
8075
box-shadow: none;
8176
}
82-
</style>
77+
</style>
78+
79+
<!--
80+
// Old mini version
81+
// https://vue-i18n.intlify.dev/guide/advanced/composition.html#global-scope
82+
// Add in main i18n options main.js
83+
84+
import { createI18n } from 'vue-i18n'
85+
86+
const lang = {
87+
// Allow compositions api (required)
88+
allowComposition: true,
89+
globalInjection: true,
90+
legacy: false,
91+
92+
// Locales
93+
locale: 'en',
94+
fallbackLocale: 'en',
95+
availableLocales: ['en', 'pl'],
96+
messages: {
97+
en: { en: "English", pl: "Polish" },
98+
pl: { en: "Angielski", pl: "Polski" },
99+
},
100+
}
101+
102+
const i18n = createI18n(lang)
103+
app.use(i18n)
104+
-->

0 commit comments

Comments
 (0)