Skip to content

Commit 3c7dfe4

Browse files
committed
feat(directive): update translations when parameters change
1 parent a3581fd commit 3c7dfe4

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

src/directive.ts

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
import { DirectiveBinding } from 'vue/types/options'
22
import { VNode } from 'vue/types/vnode'
33
import { warn } from './util/warn'
4+
import { FluentVueObject } from '../types'
5+
6+
function translate(el: HTMLElement, fluent: FluentVueObject, binding: DirectiveBinding) {
7+
if (binding.arg === undefined) {
8+
warn(false, 'v-t directive is missing arg with translation key')
9+
return
10+
}
11+
12+
const msg = fluent.getMessage(binding.arg)
13+
14+
if (msg === undefined) {
15+
return
16+
}
17+
18+
el.textContent = fluent.formatPattern(msg.value, binding.value)
19+
20+
for (const [attr] of Object.entries(binding.modifiers)) {
21+
el.setAttribute(attr, fluent.formatPattern(msg.attributes[attr], binding.value))
22+
}
23+
}
424

525
export default {
626
bind(el: HTMLElement, binding: DirectiveBinding, vnode: VNode) {
727
if (vnode.context === undefined) {
828
return
929
}
1030

11-
if (binding.arg === undefined) {
12-
warn(false, 'v-t directive is missing arg with translation key')
13-
return
14-
}
15-
16-
const msg = vnode.context.$fluent.getMessage(binding.arg)
31+
translate(el, vnode.context.$fluent, binding)
32+
},
1733

18-
if (msg === undefined) {
34+
update(el: HTMLElement, binding: DirectiveBinding, vnode: VNode) {
35+
if (vnode.context === undefined) {
1936
return
2037
}
2138

22-
el.textContent = vnode.context.$fluent.formatPattern(msg.value, binding.value)
23-
24-
for (const [attr] of Object.entries(binding.modifiers)) {
25-
el.setAttribute(
26-
attr,
27-
vnode.context.$fluent.formatPattern(msg.attributes[attr], binding.value)
28-
)
29-
}
39+
translate(el, vnode.context.$fluent, binding)
3040
}
3141
}

test/__snapshots__/directive.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ exports[`directive can use parameters 1`] = `<a href="/foo">Hello John</a>`;
66

77
exports[`directive translates text content 1`] = `<a href="/foo">Link text</a>`;
88

9+
exports[`directive updates translations on component update 1`] = `<a aria-label="Localized aria">Hello John</a>`;
10+
11+
exports[`directive updates translations on component update 2`] = `<a aria-label="Localized aria">Hello Anna</a>`;
12+
913
exports[`directive warns about missing key arg 1`] = `<a href="/foo">Fallback text</a>`;
1014

1115
exports[`directive works without fallbacks 1`] = `<a aria-label="Localized aria">Hello John</a>`;

test/directive.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,31 @@ describe('directive', () => {
133133
// Assert
134134
expect(mounted).toMatchSnapshot()
135135
})
136+
137+
it('updates translations on component update', () => {
138+
// Arrange
139+
bundle.addResource(
140+
new FluentResource(ftl`
141+
link = Hello {$name}
142+
.aria-label = Localized aria
143+
`)
144+
)
145+
146+
const component = {
147+
data: () => ({
148+
name: 'John'
149+
}),
150+
template: `<a v-t:link.aria-label="{ name }"></a>`
151+
}
152+
153+
const mounted = mount(component, options)
154+
155+
expect(mounted).toMatchSnapshot()
156+
157+
// Act
158+
mounted.setData({ name: 'Anna' })
159+
160+
// Assert
161+
expect(mounted).toMatchSnapshot()
162+
})
136163
})

0 commit comments

Comments
 (0)