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

Commit 735ccb0

Browse files
committed
Add basic command to export translations
1 parent a0a5731 commit 735ccb0

File tree

10 files changed

+80
-33
lines changed

10 files changed

+80
-33
lines changed
File renamed without changes.

example/App.vue

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"@oclif/config": "^1",
1313
"@oclif/errors": "^1.3.5",
1414
"@oclif/plugin-help": "^3",
15+
"fast-glob": "^3.2.7",
1516
"tslib": "^1"
1617
},
1718
"files": [

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/export.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Command, flags } from '@oclif/command'
2+
import { stream } from 'fast-glob'
3+
import { promises as fs, existsSync } from 'fs'
4+
import { resolve, dirname } from 'path'
5+
6+
import { getVueMessages, mergeFtl } from '..'
7+
8+
export default class Export extends Command {
9+
static description = 'Exports translation from Vue.js SFC files into ftl files.'
10+
11+
static examples = [
12+
`$ fluent-vue-cli export **/*.src --outDir translations/`,
13+
]
14+
15+
static flags = {
16+
help: flags.help({ char: 'h' }),
17+
outDir: flags.string({ name: 'out-dir', char: 'd', description: 'output directory', default: 'translations' }),
18+
clean: flags.boolean({ name: 'clean', char: 'c', description: 'clean output directory', default: true })
19+
}
20+
21+
static args = [{
22+
name: 'FILES',
23+
description: 'list of Vue.js files',
24+
required: true
25+
}]
26+
27+
static strict = false
28+
29+
async run() {
30+
const { argv, flags } = this.parse(Export)
31+
32+
for await (const file of stream(argv)) {
33+
const data = await fs.readFile(file)
34+
35+
const vueMessages = getVueMessages(data.toString())
36+
37+
for (const { locale, source, messages } of vueMessages) {
38+
const outputPath = resolve(flags.outDir, locale, `${file}.ftl`)
39+
40+
await fs.mkdir(dirname(outputPath), { recursive: true })
41+
42+
if (flags.clean || !existsSync(outputPath)) {
43+
console.log('clean')
44+
await fs.writeFile(outputPath, source)
45+
} else {
46+
console.log('merge')
47+
const existingFtlData = await fs.readFile(outputPath)
48+
49+
const newData = mergeFtl(existingFtlData.toString(), messages)
50+
51+
await fs.writeFile(outputPath, newData)
52+
}
53+
}
54+
}
55+
}
56+
}

src/commands/hello.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export { merge as mergeVue, getMessages as getVueMessages } from './vue'
22
export { merge as mergeFtl, getMessages as getFtlMessages } from './ftl'
33
export { MessagesWithLocale } from './types'
44

5-
export { default as Hello } from './commands/hello'
5+
export { default as Hello } from './commands/export'
66
export { run } from '@oclif/command'

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface MessagesWithLocale {
22
locale: string
33
messages: Record<string, string>
4+
source: string
45
}

src/vue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ function extractFromCustomBlocks (blocks: SFCBlock[]): MessagesWithLocale[] {
7676

7777
return {
7878
locale,
79-
messages: getFtlMessages(block.content)
79+
messages: getFtlMessages(block.content),
80+
source: block.content.trimStart()
8081
}
8182
}).filter(Boolean) as MessagesWithLocale[]
8283
}

translations/en/example/App.vue.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Comment
2+
user-name = World
3+
aria-key = Aria value
4+
greeting = Hello, {$name}
5+
.aria-label = Label value

0 commit comments

Comments
 (0)