Skip to content

Commit

Permalink
feat(component): add component for component interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan committed Sep 7, 2019
1 parent 6b98a21 commit 79bac0a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Vue } from 'vue/types/vue'
import extend from './extend'
import mixin from './mixin'
import directive from './vue/directive'
import component from './vue/component'

export default function install(vue: typeof Vue) {
extend(vue)
vue.mixin(mixin)
vue.directive('t', directive)
vue.component('i18n', component as any)
}
19 changes: 19 additions & 0 deletions src/vue/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from 'vue/types'

export default {
name: 'i18n',
functional: true,
props: {
path: { type: String, required: true },
tag: { type: String, default: 'span' },
data: { type: Object, default: () => ({}) }
},
render(h, { parent, props, data }) {
const key = props.path
const fluent = parent.$fluent

const translation = fluent.format(key)

return h(props.tag, data, translation)
}
} as Component
3 changes: 3 additions & 0 deletions test/vue/__snapshots__/component.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`component handles simple translations 1`] = `<span>Inner data</span>`;
48 changes: 48 additions & 0 deletions test/vue/component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createLocalVue, mount } from '@vue/test-utils'

import { FluentBundle, FluentResource } from '@fluent/bundle'
import ftl from '@fluent/dedent'

import FluentVue from '../../src'

describe('component', () => {
let options: any
let bundle: FluentBundle

beforeEach(() => {
const localVue = createLocalVue()
localVue.use(FluentVue)

bundle = new FluentBundle('en-US', {
useIsolating: false
})

const fluent = new FluentVue({
bundles: [bundle]
})

options = {
fluent,
localVue
}
})

it('handles simple translations', () => {
// Arrange
bundle.addResource(
new FluentResource(ftl`
key = Inner data
`)
)

const component = {
template: '<i18n path="key"></i18n>'
}

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

// Assert
expect(mounted).toMatchSnapshot()
})
})

0 comments on commit 79bac0a

Please sign in to comment.