Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit 80005ec

Browse files
committed
Move code to separate repo
1 parent 2a95ebd commit 80005ec

14 files changed

+1077
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
node_modules
2+
coverage
3+
.nyc_output
4+
.DS_Store
5+
*.log
6+
.vscode
7+
.idea
8+
dist
9+
compiled
10+
.awcache
11+
.rpt2_cache
12+
.cache/
13+
temp
14+
.env
15+
.parcel-cache/
16+
.temp
17+
.cache

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fluent-vue-cli
2+
=================
3+
4+
fluent-vue tool for managing locale messages
5+
6+
WIP

__tests__/fixtures/Multiple.vue

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
{{ $t('greeting', { name: 'World' }) }}
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
}
10+
</script>
11+
12+
<fluent locale="en">
13+
user-name = World
14+
aria-key = Aria value
15+
greeting = Hello, {$name}
16+
.aria-label = Label value
17+
</fluent>
18+
19+
<fluent locale="uk">
20+
user-name = Світ
21+
aria-key = Значення aria
22+
greeting = Привіт, {$name}
23+
.aria-label = Значення мітки
24+
</fluent>

__tests__/fixtures/Simple.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div>
3+
{{ $t('greeting', { name: 'World' }) }}
4+
</div>
5+
</template>
6+
7+
<script>
8+
export default {
9+
}
10+
</script>
11+
12+
<fluent locale="en">
13+
user-name = World
14+
aria-key = Aria value
15+
greeting = Hello, {$name}
16+
.aria-label = Label value
17+
</fluent>

__tests__/getFtlMessages.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getFtlMessages } from '../src'
2+
3+
describe('getVueMessages', () => {
4+
it('extracts messages from ftl file', async () => {
5+
// Arrange
6+
const source = `
7+
user-name = World
8+
aria-key = Aria value
9+
greeting = Hello, {$name}
10+
.aria-label = Label value
11+
`
12+
13+
// Act
14+
const messages = getFtlMessages(source)
15+
16+
// Assert
17+
expect(messages).toMatchInlineSnapshot(`
18+
Object {
19+
"aria-key": "Aria value",
20+
"greeting": "Hello, { $name }
21+
.aria-label = Label value",
22+
"user-name": "World",
23+
}
24+
`)
25+
})
26+
27+
it('ignores comments', async () => {
28+
// Arrange
29+
const source = `
30+
## Group comment
31+
32+
# Inline comment
33+
user-name = World
34+
aria-key = Aria value
35+
greeting = Hello, {$name}
36+
.aria-label = Label value
37+
`
38+
39+
// Act
40+
const messages = getFtlMessages(source)
41+
42+
// Assert
43+
expect(messages).toMatchInlineSnapshot(`
44+
Object {
45+
"aria-key": "Aria value",
46+
"greeting": "Hello, { $name }
47+
.aria-label = Label value",
48+
"user-name": "World",
49+
}
50+
`)
51+
})
52+
})

__tests__/getVueMessages.spec.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { readFile } from 'fs/promises'
2+
import { resolve } from 'path'
3+
4+
import { getVueMessages, MessagesWithLocale } from '../src'
5+
6+
describe('getVueMessages', () => {
7+
it('extracts locale messages from SFC', async () => {
8+
// Arrange
9+
const source = await readFile(resolve(__dirname, 'fixtures', './Simple.vue'))
10+
11+
// Act
12+
const messages = getVueMessages(source.toString())
13+
14+
// Assert
15+
expect(messages).toHaveLength(1)
16+
expect(messages).toMatchInlineSnapshot(`
17+
Array [
18+
Object {
19+
"locale": "en",
20+
"messages": Object {
21+
"aria-key": "Aria value",
22+
"greeting": "Hello, { $name }
23+
.aria-label = Label value",
24+
"user-name": "World",
25+
},
26+
},
27+
]
28+
`)
29+
})
30+
31+
it('extracts multiple SFC blocks', async () => {
32+
// Arrange
33+
const source = await readFile(resolve(__dirname, 'fixtures', './Multiple.vue'))
34+
35+
// Act
36+
const messages = getVueMessages(source.toString())
37+
38+
// Assert
39+
expect(messages).toHaveLength(2)
40+
expect(messages).toMatchInlineSnapshot(`
41+
Array [
42+
Object {
43+
"locale": "en",
44+
"messages": Object {
45+
"aria-key": "Aria value",
46+
"greeting": "Hello, { $name }
47+
.aria-label = Label value",
48+
"user-name": "World",
49+
},
50+
},
51+
Object {
52+
"locale": "uk",
53+
"messages": Object {
54+
"aria-key": "Значення aria",
55+
"greeting": "Привіт, { $name }
56+
.aria-label = Значення мітки",
57+
"user-name": "Світ",
58+
},
59+
},
60+
]
61+
`)
62+
})
63+
64+
it('throws if fluent block does not have locale', () => {
65+
// Arrange
66+
const source = `
67+
<fluent>
68+
key = value
69+
</fluent>
70+
`
71+
72+
// Act
73+
const func = (): MessagesWithLocale[] => getVueMessages(source)
74+
75+
// Assert
76+
expect(func).toThrowError('fluent custom block does not have locale specified')
77+
})
78+
79+
it('ignores non-fluent blocks', () => {
80+
// Arrange
81+
const source = `
82+
<fluent locale="en">
83+
key = value
84+
fluent-key = fluent value
85+
</fluent>
86+
87+
<i18n>
88+
{
89+
"en": {
90+
"key": "value",
91+
"i18n-key": "i18n value"
92+
}
93+
}
94+
</i18n>
95+
`
96+
97+
// Act
98+
const messages = getVueMessages(source)
99+
100+
// Assert
101+
expect(messages).toMatchInlineSnapshot(`
102+
Array [
103+
Object {
104+
"locale": "en",
105+
"messages": Object {
106+
"fluent-key": "fluent value",
107+
"key": "value",
108+
},
109+
},
110+
]
111+
`)
112+
})
113+
})

__tests__/mergeFtl.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { mergeFtl } from '../src'
2+
3+
describe('mergeFtl', () => {
4+
it('adds new key/values', () => {
5+
// Arrange
6+
const source = `
7+
user-name = World
8+
aria-key = Aria value
9+
greeting = Hello, {$name}
10+
.aria-label = Label value
11+
`
12+
const newTranslation = { hello: 'Hello' }
13+
14+
// Act
15+
const newSource = mergeFtl(source, newTranslation)
16+
17+
// Assert
18+
expect(newSource).toMatchInlineSnapshot(`
19+
"user-name = World
20+
aria-key = Aria value
21+
greeting = Hello, { $name }
22+
.aria-label = Label value
23+
hello = Hello
24+
"
25+
`)
26+
})
27+
28+
it('preserves comments', () => {
29+
// Arrange
30+
const source = `
31+
## Group comment
32+
33+
# Inline comment
34+
user-name = World
35+
aria-key = Aria value
36+
greeting = Hello, {$name}
37+
.aria-label = Label value
38+
`
39+
const newTranslation = { 'user-name': 'John Doe' }
40+
41+
// Act
42+
const newSource = mergeFtl(source, newTranslation)
43+
44+
// Assert
45+
expect(newSource).toMatchInlineSnapshot(`
46+
"## Group comment
47+
48+
# Inline comment
49+
user-name = John Doe
50+
aria-key = Aria value
51+
greeting = Hello, { $name }
52+
.aria-label = Label value
53+
"
54+
`)
55+
})
56+
57+
it('changes value if key is already present', () => {
58+
// Arrange
59+
const source = `
60+
user-name = World
61+
aria-key = Aria value
62+
greeting = Hello, {$name}
63+
.aria-label = Label value
64+
`
65+
66+
const existingTranslation = { 'user-name': 'Jorn Doe' }
67+
68+
// Act
69+
const newSource = mergeFtl(source, existingTranslation)
70+
71+
// Assert
72+
expect(newSource).toMatchInlineSnapshot(`
73+
"user-name = Jorn Doe
74+
aria-key = Aria value
75+
greeting = Hello, { $name }
76+
.aria-label = Label value
77+
"
78+
`)
79+
})
80+
})

0 commit comments

Comments
 (0)