Skip to content

How to save the selected language? #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
botjkeee opened this issue Sep 1, 2022 · 3 comments
Open

How to save the selected language? #90

botjkeee opened this issue Sep 1, 2022 · 3 comments

Comments

@botjkeee
Copy link

botjkeee commented Sep 1, 2022

I used to use locale: localStorage.getItem("lang") || "ru", but what is the right way to do it here?

@dodanex
Copy link

dodanex commented Sep 13, 2022

Up! I'm trying to figure it out myself...
Inside template I can use @click="$i18n.locale = 'en'" to set the language and it works without importing anything in the setup, but don't know how to set and save it inside setup, because $i18n.locale is not available.
Anyone want to help?

@dodanex
Copy link

dodanex commented Sep 13, 2022

Up! I'm trying to figure it out myself... Inside template I can use @click="$i18n.locale = 'en'" to set the language and it works without importing anything in the setup, but don't know how to set and save it inside setup, because $i18n.locale is not available. Anyone want to help?

UPDATE - found a solution that works. Hope this will help others!

<script setup lang="ts">
import { useI18n } from 'vue-i18n'

const { locale } = useI18n()
const config = useRuntimeConfig()

locale.value = 'en' || config.DEFAULT_LANGUAGE

function setLocale(value) {
	locale.value = value
        // TODO: save the locale in local storage or cookie...
        // localStorage.set('locale', value)
}

onMounted(() =>{
    // TODO: get the locale from local storage or cookie and set it...
    // locale.value = localStorage.get('locale')
})
</script>

<template>
	<q-btn flat dense icon="flag" class="q-mr-md"
		><q-menu
			><q-list>
				<q-item
					v-close-popup
					clickable
					:active="$i18n.locale == 'en'"
					@click="setLocale('en')"
				>
					<q-item-section avatar>
						<q-icon name="flag" />
					</q-item-section>
					<q-item-section>
						<q-item-label>English</q-item-label>
					</q-item-section>
				</q-item>
				<q-item
					v-close-popup
					clickable
					:active="$i18n.locale == 'ro'"
					@click="setLocale('ro')"
				>
					<q-item-section avatar>
						<q-icon name="flag" />
					</q-item-section>
					<q-item-section>
						<q-item-label>Romanian</q-item-label>
					</q-item-section>
				</q-item>
			</q-list></q-menu
		></q-btn
	>
</template>

@JCAlways
Copy link

向上!我正在尝试自己弄清楚...在模板内部我可以@click="$i18n.locale = 'en'"用来设置语言并且它无需在设置中导入任何内容即可工作,但不知道如何在设置中设置和保存它,因为 $i18n.locale不可用。有人想帮忙吗?

更新 - 找到了一个有效的解决方案。希望这对其他人有帮助!

<script setup lang="ts">
import { useI18n } from 'vue-i18n'

const { locale } = useI18n()
const config = useRuntimeConfig()

locale.value = 'en' || config.DEFAULT_LANGUAGE

function setLocale(value) {
	locale.value = value
        // TODO: save the locale in local storage or cookie...
        // localStorage.set('locale', value)
}

onMounted(() =>{
    // TODO: get the locale from local storage or cookie and set it...
    // locale.value = localStorage.get('locale')
})
</script>

<template>
	<q-btn flat dense icon="flag" class="q-mr-md"
		><q-menu
			><q-list>
				<q-item
					v-close-popup
					clickable
					:active="$i18n.locale == 'en'"
					@click="setLocale('en')"
				>
					<q-item-section avatar>
						<q-icon name="flag" />
					</q-item-section>
					<q-item-section>
						<q-item-label>English</q-item-label>
					</q-item-section>
				</q-item>
				<q-item
					v-close-popup
					clickable
					:active="$i18n.locale == 'ro'"
					@click="setLocale('ro')"
				>
					<q-item-section avatar>
						<q-icon name="flag" />
					</q-item-section>
					<q-item-section>
						<q-item-label>Romanian</q-item-label>
					</q-item-section>
				</q-item>
			</q-list></q-menu
		></q-btn
	>
</template>

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants