|
| 1 | +<!-- |
| 2 | +// Old style new version in utils |
| 3 | +// https://vue-i18n.intlify.dev/guide/advanced/composition.html#global-scope |
| 4 | +// Add in main i18n options main.js |
| 5 | +
|
| 6 | +import { createI18n } from 'vue-i18n' |
| 7 | +
|
| 8 | +const lang = { |
| 9 | + // Allow compositions api (required) |
| 10 | + allowComposition: true, |
| 11 | + globalInjection: true, |
| 12 | + legacy: false, |
| 13 | +
|
| 14 | + // Locales |
| 15 | + locale: 'en', |
| 16 | + fallbackLocale: 'en', |
| 17 | + availableLocales: ['en', 'pl'], |
| 18 | + messages: { |
| 19 | + en: { en: "English", pl: "Polish" }, |
| 20 | + pl: { en: "Angielski", pl: "Polski" }, |
| 21 | + }, |
| 22 | +} |
| 23 | +
|
| 24 | +const i18n = createI18n(lang) |
| 25 | +app.use(i18n) |
| 26 | +--> |
| 27 | + |
1 | 28 | <template> |
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> |
| 29 | + <div class="locale-changer"> |
| 30 | + <select v-model="locale" class="locale-changer-select"> |
| 31 | + <option v-for="lang in availableLocales" :key="`locale-${lang}`" :value="lang">{{ t(lang) }}</option> |
7 | 32 | </select> |
8 | 33 | </div> |
9 | 34 | </template> |
10 | 35 |
|
11 | 36 | <script setup> |
12 | | -import axios from 'axios' |
| 37 | +import { watch, computed, onMounted } from 'vue' |
| 38 | +import { useAuthStore } from '@/stores/auth.js' |
13 | 39 | import { useI18n } from 'vue-i18n' |
14 | | -import { watch, onMounted } from 'vue' |
15 | 40 |
|
16 | 41 | const { t, locale, availableLocales } = useI18n({ useScope: 'global' }) |
17 | | -
|
18 | | -const props = defineProps({ |
19 | | - locale_url: { default: '/web/api/locale/' }, |
20 | | -}) |
| 42 | +const store = useAuthStore() |
21 | 43 |
|
22 | 44 | onMounted(() => { |
23 | | - console.log('Change locale', locale.value, availableLocales) |
| 45 | + console.log('Current locale', locale.value, availableLocales) |
24 | 46 | }) |
25 | 47 |
|
26 | 48 | watch( |
27 | 49 | () => locale.value, |
28 | 50 | (lang) => { |
29 | | - changeLocale(lang) |
| 51 | + console.log('Changed locale', lang) |
| 52 | + store.changeLocale(lang) |
30 | 53 | } |
31 | 54 | ) |
32 | 55 |
|
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 | | -} |
| 56 | +const msg = computed(() => t('example.msg')) |
43 | 57 | </script> |
44 | 58 |
|
45 | 59 | <style scoped> |
46 | | -.change-locale { |
| 60 | +.locale-changer { |
47 | 61 | float: right; |
48 | 62 | width: auto; |
49 | | - height: 44px; |
50 | | - margin: 5px; |
51 | | - background: var(--bg-primary); |
52 | | - border-radius: var(--border-radius); |
53 | | - border: 0px solid var(--divider-primary); |
| 63 | + height: auto; |
| 64 | + padding: 5px; |
54 | 65 | } |
55 | | -.change-locale .locale-select { |
56 | | - box-sizing: border-box; |
57 | | - min-width: 60px; |
58 | | - height: 42px; |
59 | | - float: left; |
| 66 | +.locale-changer-select { |
| 67 | + float: right; |
| 68 | + display: inline; |
| 69 | + padding: 2px; |
| 70 | + text-align: center; |
60 | 71 | border: 0px; |
| 72 | + font-size: 1rem; |
61 | 73 | 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); |
67 | 74 | } |
68 | | -.change-locale .locale-select option, |
69 | | -.change-locale .locale-select > * { |
70 | | - background: var(--bg-primary); |
71 | | - color: var(--text-primary); |
| 75 | +.locale-changer-select > * { |
| 76 | + background: #fff; |
| 77 | + color: #222; |
72 | 78 | } |
73 | | -.change-locale .locale-select:focus { |
| 79 | +.locale-changer-select:focus { |
74 | 80 | border: none; |
75 | 81 | box-shadow: none; |
76 | 82 | } |
77 | 83 | </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