Skip to content

Commit

Permalink
feat(directive): use directive argument for translation key
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan committed Aug 8, 2019
1 parent 3fb8529 commit c0bf0c7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/directive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DirectiveBinding } from 'vue/types/options'
import { VNode } from 'vue/types/vnode'
import { warn } from './util/warn'

interface FluentDirectiveBinding {
key: string
Expand All @@ -13,8 +14,13 @@ export default {
return
}

const directiveData = binding.value as FluentDirectiveBinding
if (binding.arg === undefined) {
warn(false, 'v-t directive is missing arg with translation key')
return
}

const directiveData = (binding.value as FluentDirectiveBinding) || {}

el.textContent = vnode.context.$t(directiveData.key, directiveData.arg)
el.textContent = vnode.context.$t(binding.arg, directiveData.arg)
}
}
2 changes: 2 additions & 0 deletions test/__snapshots__/directive.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
exports[`directive can use parameters 1`] = `<a href="/foo">Hello John</a>`;

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

exports[`directive warns about missing key arg 1`] = `<a href="/foo">Fallback text</a>`;
21 changes: 19 additions & 2 deletions test/directive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('directive', () => {
data: () => ({
name: 'John'
}),
template: `<a v-t="{ key: 'link' }" href="/foo">Fallback text</a>`
template: `<a v-t:link href="/foo">Fallback text</a>`
}

// Act
Expand All @@ -49,6 +49,23 @@ describe('directive', () => {
expect(mounted).toMatchSnapshot()
})

it('warns about missing key arg', () => {
// Arrange
const component = {
template: `<a v-t href="/foo">Fallback text</a>`
}

const warn = jest.fn()
console.warn = warn

// Act
const mounted = mount(component, options)

// Assert
expect(mounted).toMatchSnapshot()
expect(warn).toHaveBeenCalledTimes(1)
})

it('can use parameters', () => {
// Arrange
bundle.addResource(
Expand All @@ -61,7 +78,7 @@ describe('directive', () => {
data: () => ({
name: 'John'
}),
template: `<a v-t="{ key: 'link', arg: { name: 'John' } }" href="/foo">Fallback text</a>`
template: `<a v-t:link="{ arg: { name: 'John' } }" href="/foo">Fallback text</a>`
}

// Act
Expand Down

0 comments on commit c0bf0c7

Please sign in to comment.