Skip to content

Commit 8d8adbf

Browse files
Reactive Current Locale (#200)
* Reactive current locale - Related to this #192 * make reactive instance of current language to be readonly
1 parent 407aed6 commit 8d8adbf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { reactive, Plugin, computed, ComputedRef, watchEffect } from 'vue'
1+
import { reactive, Plugin, computed, ComputedRef, watchEffect, ref, ComputedRef } from 'vue'
22
import { OptionsInterface } from './interfaces/options'
33
import { PluginOptionsInterface } from './interfaces/plugin-options'
44
import { LanguageInterface } from './interfaces/language'
@@ -26,6 +26,17 @@ const DEFAULT_OPTIONS: OptionsInterface = {
2626
onLoad: (lang: string) => {}
2727
}
2828

29+
/**
30+
* A computed property that reflects the current reactive language.
31+
*
32+
* This value is derived from the shared I18n instance. When the language
33+
* is updated via the I18n API, this computed value will automatically reflect
34+
* the new language.
35+
*/
36+
export const currentLocale: ComputedRef<string> = computed(() => {
37+
return I18n.getSharedInstance().getCurrentLanguage().value
38+
})
39+
2940
/**
3041
* The default options, for the plugin.
3142
*/
@@ -137,6 +148,9 @@ export class I18n {
137148
// Stores options for the current instance
138149
private options: OptionsInterface
139150

151+
// Stores a reactive reference to the current active language
152+
private currentLanguage = ref<string>(DEFAULT_OPTIONS.lang || DEFAULT_OPTIONS.fallbackLang)
153+
140154
// Stores messages for the currently active language
141155
private activeMessages: object = reactive({})
142156

@@ -358,6 +372,7 @@ export class I18n {
358372
}
359373

360374
this.options.lang = lang
375+
this.currentLanguage.value = lang
361376

362377
for (const [key, value] of Object.entries(messages)) {
363378
this.activeMessages[key] = value
@@ -386,6 +401,13 @@ export class I18n {
386401
return this.options.lang || this.options.fallbackLang
387402
}
388403

404+
/**
405+
* Returns the reactive current active language
406+
*/
407+
getCurrentLanguage(): ComputedRef<string> {
408+
return computed(() => this.currentLanguage.value)
409+
}
410+
389411
/**
390412
* Checks if the language is loaded.
391413
*/

test/translate.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mount } from '@vue/test-utils'
2-
import { i18nVue, trans, trans_choice, loadLanguageAsync, reset, getActiveLanguage, isLoaded, wTrans, I18n } from '../src'
2+
import { i18nVue, trans, trans_choice, loadLanguageAsync, reset, getActiveLanguage, isLoaded, wTrans, I18n, currentLocale } from '../src'
33
import { reset as resetLoader } from '../src/loader'
44

55
beforeEach(() => reset());
@@ -278,3 +278,15 @@ it('checks if watching wTrans works if key does not exist', async () => {
278278

279279
expect(translated.value).toBe('Not existing translation');
280280
})
281+
282+
it('get current reactive locale with "currentLocale"', async () => {
283+
await global.mountPlugin();
284+
285+
expect(currentLocale.value)
286+
.toBe('pt')
287+
288+
await loadLanguageAsync('en');
289+
290+
expect(currentLocale.value)
291+
.toBe('en')
292+
})

0 commit comments

Comments
 (0)