Skip to content

Commit c0bf0c7

Browse files
committed
feat(directive): use directive argument for translation key
1 parent 3fb8529 commit c0bf0c7

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/directive.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DirectiveBinding } from 'vue/types/options'
22
import { VNode } from 'vue/types/vnode'
3+
import { warn } from './util/warn'
34

45
interface FluentDirectiveBinding {
56
key: string
@@ -13,8 +14,13 @@ export default {
1314
return
1415
}
1516

16-
const directiveData = binding.value as FluentDirectiveBinding
17+
if (binding.arg === undefined) {
18+
warn(false, 'v-t directive is missing arg with translation key')
19+
return
20+
}
21+
22+
const directiveData = (binding.value as FluentDirectiveBinding) || {}
1723

18-
el.textContent = vnode.context.$t(directiveData.key, directiveData.arg)
24+
el.textContent = vnode.context.$t(binding.arg, directiveData.arg)
1925
}
2026
}

test/__snapshots__/directive.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
exports[`directive can use parameters 1`] = `<a href="/foo">Hello John</a>`;
44

55
exports[`directive translates text content 1`] = `<a href="/foo">Link text</a>`;
6+
7+
exports[`directive warns about missing key arg 1`] = `<a href="/foo">Fallback text</a>`;

test/directive.test.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('directive', () => {
3939
data: () => ({
4040
name: 'John'
4141
}),
42-
template: `<a v-t="{ key: 'link' }" href="/foo">Fallback text</a>`
42+
template: `<a v-t:link href="/foo">Fallback text</a>`
4343
}
4444

4545
// Act
@@ -49,6 +49,23 @@ describe('directive', () => {
4949
expect(mounted).toMatchSnapshot()
5050
})
5151

52+
it('warns about missing key arg', () => {
53+
// Arrange
54+
const component = {
55+
template: `<a v-t href="/foo">Fallback text</a>`
56+
}
57+
58+
const warn = jest.fn()
59+
console.warn = warn
60+
61+
// Act
62+
const mounted = mount(component, options)
63+
64+
// Assert
65+
expect(mounted).toMatchSnapshot()
66+
expect(warn).toHaveBeenCalledTimes(1)
67+
})
68+
5269
it('can use parameters', () => {
5370
// Arrange
5471
bundle.addResource(
@@ -61,7 +78,7 @@ describe('directive', () => {
6178
data: () => ({
6279
name: 'John'
6380
}),
64-
template: `<a v-t="{ key: 'link', arg: { name: 'John' } }" href="/foo">Fallback text</a>`
81+
template: `<a v-t:link="{ arg: { name: 'John' } }" href="/foo">Fallback text</a>`
6582
}
6683

6784
// Act

0 commit comments

Comments
 (0)