-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directive): initial directive implementation
- Loading branch information
Showing
5 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DirectiveBinding } from 'vue/types/options' | ||
import { VNode } from 'vue/types/vnode' | ||
|
||
interface FluentDirectiveBinding { | ||
key: string | ||
arg: Object | ||
attrs?: [string] | ||
} | ||
|
||
export default { | ||
bind(el: HTMLElement, binding: DirectiveBinding, vnode: VNode) { | ||
if (vnode.context === undefined) { | ||
return | ||
} | ||
|
||
const directiveData = binding.value as FluentDirectiveBinding | ||
|
||
const message = vnode.context.$t(directiveData.key, directiveData.arg) | ||
|
||
if (message != null) { | ||
el.textContent = message | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Vue } from 'vue/types/vue' | ||
import extend from './extend' | ||
import mixin from './mixin' | ||
import directive from './directive' | ||
|
||
export default function install(vue: typeof Vue) { | ||
extend(vue) | ||
vue.mixin(mixin) | ||
vue.directive('t', directive) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`directive can use parameters 1`] = `<a href="/foo">Hello John</a>`; | ||
|
||
exports[`directive translates text content 1`] = `<a href="/foo">Link text</a>`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { createLocalVue, mount } from '@vue/test-utils' | ||
|
||
import { FluentBundle, FluentResource } from '@fluent/bundle' | ||
import ftl from '@fluent/dedent' | ||
|
||
import FluentVue from '../src' | ||
|
||
describe('directive', () => { | ||
let options: any | ||
let bundle: any | ||
|
||
beforeEach(() => { | ||
const localVue = createLocalVue() | ||
localVue.use(FluentVue) | ||
|
||
bundle = new FluentBundle('en-US', { | ||
useIsolating: false | ||
}) | ||
|
||
const fluent = new FluentVue({ | ||
bundle | ||
}) | ||
|
||
options = { | ||
fluent, | ||
localVue | ||
} | ||
}) | ||
|
||
it('translates text content', () => { | ||
// Arrange | ||
bundle.addResource( | ||
new FluentResource(ftl` | ||
link = Link text | ||
`) | ||
) | ||
|
||
const component = { | ||
data: () => ({ | ||
name: 'John' | ||
}), | ||
template: `<a v-t="{ key: 'link' }" href="/foo">Fallback text</a>` | ||
} | ||
|
||
// Act | ||
const mounted = mount(component, options) | ||
|
||
// Assert | ||
expect(mounted).toMatchSnapshot() | ||
}) | ||
|
||
it('can use parameters', () => { | ||
// Arrange | ||
bundle.addResource( | ||
new FluentResource(ftl` | ||
link = Hello {$name} | ||
`) | ||
) | ||
|
||
const component = { | ||
data: () => ({ | ||
name: 'John' | ||
}), | ||
template: `<a v-t="{ key: 'link', arg: { name: 'John' } }" href="/foo">Fallback text</a>` | ||
} | ||
|
||
// Act | ||
const mounted = mount(component, options) | ||
|
||
// Assert | ||
expect(mounted).toMatchSnapshot() | ||
}) | ||
}) |