Skip to content

Commit 202cfa3

Browse files
authored
Merge pull request #836 from fluent-vue/fix-stale-data
2 parents ff92080 + c860aa7 commit 202cfa3

File tree

4 files changed

+118
-6
lines changed

4 files changed

+118
-6
lines changed

__tests__/changeLanguage.spec.ts

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { beforeEach, describe, expect, it } from 'vitest'
22

3-
import { nextTick } from 'vue-demi'
3+
import { isVue3, nextTick } from 'vue-demi'
44
import { FluentBundle, FluentResource } from '@fluent/bundle'
55
import ftl from '@fluent/dedent'
66

77
import type { FluentVue } from '../src'
8-
import { createFluentVue } from '../src'
8+
import { createFluentVue, useFluent } from '../src'
99
import { mountWithFluent } from './utils'
1010

1111
describe('language change', () => {
@@ -104,6 +104,111 @@ describe('language change', () => {
104104
expect(mounted.html()).toEqual('<a href="/foo">link text</a>')
105105
})
106106

107+
it('updates locale even if component is unmounted github.com/orgs/fluent-vue/discussions/834', async () => {
108+
// Arrange
109+
bundleEn = new FluentBundle('en-US')
110+
bundleUk = new FluentBundle('uk-UA')
111+
112+
const fluent = createFluentVue({
113+
bundles: [bundleUk],
114+
})
115+
116+
const child = {
117+
template: '<span>{{ $t("child") }}</span>',
118+
fluent: {
119+
'en-US': new FluentResource(ftl`
120+
child = Child message
121+
`),
122+
'uk-UA': new FluentResource(ftl`
123+
child = Повідомлення
124+
`),
125+
},
126+
}
127+
128+
const component = {
129+
components: {
130+
child,
131+
},
132+
data: () => ({
133+
show: true,
134+
}),
135+
template: '<child v-if="show" />',
136+
}
137+
138+
// Act
139+
const mounted = mountWithFluent(fluent, component)
140+
141+
expect(mounted.html()).toEqual('<span>Повідомлення</span>')
142+
143+
await mounted.setData({ show: false })
144+
145+
if (isVue3)
146+
expect(mounted.html()).toEqual('<!--v-if-->')
147+
else
148+
expect(mounted.html()).toEqual('')
149+
150+
fluent.bundles = [bundleEn]
151+
152+
await mounted.setData({ show: true })
153+
154+
// Assert
155+
expect(mounted.html()).toEqual('<span>Child message</span>')
156+
})
157+
158+
it('useFluent updates locale even if component is unmounted github.com/orgs/fluent-vue/discussions/834', async () => {
159+
// Arrange
160+
bundleEn = new FluentBundle('en-US')
161+
bundleUk = new FluentBundle('uk-UA')
162+
163+
const fluent = createFluentVue({
164+
bundles: [bundleUk],
165+
})
166+
167+
const child = {
168+
setup() {
169+
useFluent()
170+
},
171+
template: '<span>{{ $t("child") }}</span>',
172+
fluent: {
173+
'en-US': new FluentResource(ftl`
174+
child = Child message
175+
`),
176+
'uk-UA': new FluentResource(ftl`
177+
child = Повідомлення
178+
`),
179+
},
180+
}
181+
182+
const component = {
183+
components: {
184+
child,
185+
},
186+
data: () => ({
187+
show: true,
188+
}),
189+
template: '<child v-if="show" />',
190+
}
191+
192+
// Act
193+
const mounted = mountWithFluent(fluent, component)
194+
195+
expect(mounted.html()).toEqual('<span>Повідомлення</span>')
196+
197+
await mounted.setData({ show: false })
198+
199+
if (isVue3)
200+
expect(mounted.html()).toEqual('<!--v-if-->')
201+
else
202+
expect(mounted.html()).toEqual('')
203+
204+
fluent.bundles = [bundleEn]
205+
206+
await mounted.setData({ show: true })
207+
208+
// Assert
209+
expect(mounted.html()).toEqual('<span>Child message</span>')
210+
})
211+
107212
it('works when dynamically adding bundles', async () => {
108213
// Arrange
109214
bundleEn = new FluentBundle('en-US')

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy