Skip to content

Commit c8b7dde

Browse files
committed
Fix stale translations on locale change
1 parent ff92080 commit c8b7dde

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/composition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export function useFluent(): TranslationContext {
1212
const rootContext = inject(RootContextSymbol)
1313
assert(rootContext != null, 'useFluent called without installing plugin')
1414

15-
return getContext(rootContext, instance.proxy)
15+
return getContext(rootContext, instance.proxy, true)
1616
}

src/getContext.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function * flatMap<T, TR>(iterable: Iterable<T>, mapper: (element: T) => TR[]):
1313
export function getContext(
1414
rootContext: TranslationContext,
1515
instance: VueComponent | null | undefined,
16+
fromSetup = false,
1617
): TranslationContext {
1718
if (instance == null)
1819
return rootContext
@@ -50,7 +51,10 @@ export function getContext(
5051

5152
const context = new TranslationContext(overriddenBundles, rootContext.options)
5253

53-
options._fluent = context
54+
// If we are in script setup, we cannot cache the context
55+
// because after component is unmounted, computed will not be updated
56+
if (!fromSetup)
57+
options._fluent = context
5458

5559
return context
5660
}

src/types/vue.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import type Vue from 'vue-2'
22
import type { FluentResource } from '@fluent/bundle'
3-
import type { FluentVue } from '../index'
3+
import type { TranslationContext } from '../TranslationContext'
44

55
declare module 'vue-2/types/options' {
66
// eslint-disable-next-line @typescript-eslint/no-unused-vars
77
interface ComponentOptions<V extends Vue> {
88
fluent?: Record<string, FluentResource>
99

1010
/** @private */
11-
_fluent?: FluentVue
11+
_fluent?: TranslationContext
12+
13+
/** @private */
14+
_fluentSetup?: TranslationContext
1215
}
1316
}
1417

0 commit comments

Comments
 (0)