Skip to content

Commit 48f68bb

Browse files
committed
feat(method): add a way to get message attrs
Add $ta method that gets message attributes fix #9
1 parent 31301d7 commit 48f68bb

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

src/extend.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ export default function extend(Vue: VueConstructor<Vue>): void {
1919
Vue.prototype.$t = function(key: string, values: any): string {
2020
return this.$fluent.format(key, values)
2121
}
22+
23+
/**
24+
* Format message attributes
25+
* @param key Translation key
26+
* @param values Message parameters
27+
*/
28+
Vue.prototype.$ta = function(key: string, values: any): string {
29+
return this.$fluent.formatAttrs(key, values)
30+
}
2231
}

src/fluent-vue.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ export default class FluentVue implements FluentVueObject {
7474

7575
const errors: string[] = []
7676
const result = this.formatPattern(context, message.value, value, errors)
77+
78+
for (const error of errors) {
79+
warn(`Fluent error for key [${key}]: ${error}`)
80+
}
81+
82+
return result
83+
}
84+
85+
formatAttrs(key: string, value?: object): object {
86+
const context = this.getBundle(key)
87+
const message = this.getMessage(context, key)
88+
89+
if (message === null || message.value === null) {
90+
return {}
91+
}
92+
93+
const errors: string[] = []
94+
const result = {}
95+
96+
for (const [attrName, attrValue] of Object.entries(message.attributes)) {
97+
;(result as any)[attrName] = this.formatPattern(context, attrValue, value, errors)
98+
}
99+
77100
for (const error of errors) {
78101
warn(`Fluent error for key [${key}]: ${error}`)
79102
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`method works with vue components 1`] = `<div attr="Attr value">Inner data</div>`;

test/vue/custom-components.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { createLocalVue, mount } from '@vue/test-utils'
2+
3+
import { FluentBundle, FluentResource } from '@fluent/bundle'
4+
import ftl from '@fluent/dedent'
5+
6+
import FluentVue from '../../src'
7+
8+
describe('method', () => {
9+
let options: any
10+
let bundle: FluentBundle
11+
12+
beforeEach(() => {
13+
const localVue = createLocalVue()
14+
localVue.use(FluentVue)
15+
16+
bundle = new FluentBundle('en-US')
17+
18+
const fluent = new FluentVue({
19+
bundles: [bundle]
20+
})
21+
22+
options = {
23+
fluent,
24+
localVue
25+
}
26+
})
27+
28+
it('works with vue components', () => {
29+
// Arrange
30+
bundle.addResource(
31+
new FluentResource(ftl`
32+
key = Inner data
33+
.attr = Attr value
34+
`)
35+
)
36+
37+
const child = {
38+
props: {
39+
text: { type: String },
40+
attrs: { type: Object }
41+
},
42+
template: `<div :attr="attrs.attr">{{ text }}</div>`
43+
}
44+
45+
const component = {
46+
components: {
47+
child
48+
},
49+
template: `<child :text="$t('key')" :attrs="$ta('key')"></child>`
50+
}
51+
52+
// Act
53+
const mounted = mount(component, options)
54+
55+
// Assert
56+
expect(mounted).toMatchSnapshot()
57+
})
58+
})

0 commit comments

Comments
 (0)