Skip to content

Commit

Permalink
fix(component): fix i18n component not rerendering on args change (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan authored Apr 1, 2021
1 parent bf44bf3 commit 57a2c12
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
34 changes: 34 additions & 0 deletions packages/fluent-vue/__tests__/vue/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,38 @@ describe('component', () => {
// Assert
expect(mounted.html()).toEqual(`<span>Hello ⁨John⁩ ⁨<b>Inner text</b>⁩ test</span>`)
})

it('updates on parameter change', async () => {
// Arrange
bundle.addResource(
new FluentResource(ftl`
key = Hello {$name} {$child} test
`)
)

const component = {
data() {
return {
name: 'John',
}
},
template: `
<i18n path="key" :args="{ name }">
<template #child>
<b>Inner text</b>
</template>
</i18n>`,
}

const mounted = mount(component, options)
expect(mounted.html()).toEqual(`<span>Hello ⁨John⁩ ⁨<b>Inner text</b>⁩ test</span>`)

// Act
const vm = mounted.vm as any
vm.name = 'Alice'
await Vue.nextTick()

// Assert
expect(mounted.html()).toEqual(`<span>Hello ⁨Alice⁩ ⁨<b>Inner text</b>⁩ test</span>`)
})
})
23 changes: 12 additions & 11 deletions packages/fluent-vue/src/vue/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ export default defineComponent({
setup(props, context) {
const childSlots = context.slots

const params = Object.assign(
{},
props.args,
...Object.keys(childSlots).map((key) => ({
[key]: `\uFFFF\uFFFE${key}\uFFFF`,
}))
)
const rootContext = inject(RootContextSymbol)!
const instance = getCurrentInstance()
const parent = getParentWithFluent(instance)
const fluent = getContext(rootContext, parent)

const translation = computed(() => {
const rootContext = inject(RootContextSymbol)!
const instance = getCurrentInstance()
const parent = getParentWithFluent(instance)
const fluent = getContext(rootContext, parent)
const params = Object.assign(
{},
props.args,
...Object.keys(childSlots).map((key) => ({
[key]: `\uFFFF\uFFFE${key}\uFFFF`,
}))
)

return fluent.format(props.path, params)
})

Expand Down

0 comments on commit 57a2c12

Please sign in to comment.