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+
27+ <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 >
31+ </select >
32+ </div >
33+ </template >
34+
35+ <script setup>
36+ import { watch , computed , onMounted } from ' vue'
37+ import { useAuthStore } from ' @/stores/auth.js'
38+ import { useI18n } from ' vue-i18n'
39+
40+ const { t , locale , availableLocales } = useI18n ({ useScope: ' global' })
41+ const store = useAuthStore ()
42+
43+ onMounted (() => {
44+ console .log (' Current locale' , locale .value , availableLocales)
45+ })
46+
47+ watch (
48+ () => locale .value ,
49+ (lang ) => {
50+ console .log (' Changed locale' , lang)
51+ store .changeLocale (lang)
52+ }
53+ )
54+
55+ const msg = computed (() => t (' example.msg' ))
56+ </script >
57+
58+ <style scoped>
59+ .locale-changer {
60+ float : right ;
61+ width : auto ;
62+ height : auto ;
63+ padding : 5px ;
64+ }
65+ .locale-changer-select {
66+ float : right ;
67+ display : inline ;
68+ padding : 2px ;
69+ text-align : center ;
70+ border : 0px ;
71+ font-size : 1rem ;
72+ cursor : pointer ;
73+ }
74+ .locale-changer-select > * {
75+ background : #fff ;
76+ color : #222 ;
77+ }
78+ .locale-changer-select :focus {
79+ border : none ;
80+ box-shadow : none ;
81+ }
82+ </style >
0 commit comments