Skip to content

Commit

Permalink
feat: add support for fluent-vue (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan committed Aug 29, 2021
1 parent 3e60c3e commit a2c6549
Show file tree
Hide file tree
Showing 24 changed files with 688 additions and 31 deletions.
4 changes: 4 additions & 0 deletions examples/by-frameworks/fluent-vue-sfc/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"i18n-ally.localesPaths": "i18n",
"i18n-ally.keystyle": "nested"
}
1 change: 1 addition & 0 deletions examples/by-frameworks/fluent-vue-sfc/i18n/en.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global-key = Global value
13 changes: 13 additions & 0 deletions examples/by-frameworks/fluent-vue-sfc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "basic",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fluent/bundle": "^0.16.0",
"fluent-vue": "^3.0.0-beta.16",
"vue": "~2.6.12"
},
"devDependencies": {
"fluent-vue-loader": "^3.0.0-beta.16"
}
}
64 changes: 64 additions & 0 deletions examples/by-frameworks/fluent-vue-sfc/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<template>
<div>
<!-- Methods -->
<div>{{ $t('test') }}</div>
<div>{{ $t('global-key') }}</div>
<div v-bind="$ta('props')" />

<!-- Directive -->
<div v-t:test />
<div v-t:greeting="{ name: username }" />

<!-- Component -->
<i18n path="term" tag="span">
<template #tos-link="{ termsLinkText }">
<RouterLink to="/login/register">
{{ termsLinkText }}
</RouterLink>
</template>
</i18n>

<div>
Plain text to extract
</div>
</div>
</template>

<script>
import { useFluent } from 'fluent-vue'
export default {
setup() {
const { $t } = useFluent()
return {
test: $t('test'),
}
},
computed: {
username() {
return this.$t('user-name')
},
notExists() {
return this.$t('wrong-key')
},
plainText() {
return 'Plain text in js to extract'
},
},
}
</script>

<fluent locale="en">
user-name = World
aria-key = Aria value
# {$name} Name of current user
greeting =
Hello, {$name}
.aria-label = Label value
test = Test value
props =
.aria-label = Aria
term = I accept {$tos-link}
.terms-link-text = Term of Service
</fluent>
9 changes: 9 additions & 0 deletions examples/by-frameworks/fluent-vue-sfc/src/fluent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FluentBundle } from '@fluent/bundle'
import { createFluentVue } from 'fluent-vue'

const bundle = new FluentBundle('en')

export const fluent = createFluentVue({
locale: 'en',
bundles: [bundle],
})
11 changes: 11 additions & 0 deletions examples/by-frameworks/fluent-vue-sfc/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Vue from 'vue'

import { fluent } from './fluent'
import App from './App.vue'

Vue.use(fluent)

new Vue({
el: '#root',
render: (h) => h(App),
})
4 changes: 4 additions & 0 deletions examples/by-frameworks/fluent-vue/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"i18n-ally.localesPaths": "i18n",
"i18n-ally.keystyle": "nested"
}
10 changes: 10 additions & 0 deletions examples/by-frameworks/fluent-vue/i18n/en.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
user-name = World
aria-key = Aria value
# {$name} Name of current user
greeting = Hello, { $name }
.aria-label = Label value
test = Test value
props = .aria-label = Aria
.aria-label = Aria
term = I accept { $tos-link }
.terms-link-text = Term of Service
10 changes: 10 additions & 0 deletions examples/by-frameworks/fluent-vue/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "basic",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fluent/bundle": "^0.16.0",
"fluent-vue": "^3.0.0-beta.16",
"vue": "~2.6.12"
}
}
49 changes: 49 additions & 0 deletions examples/by-frameworks/fluent-vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<template>
<div>
<!-- Methods -->
<div>{{ $t('test') }}</div>
<div v-bind="$ta('props')" />

<!-- Directive -->
<div v-t:test />
<div v-t:greeting="{ name: username }" />

<!-- Component -->
<i18n path="term" tag="span">
<template #tos-link="{ termsLinkText }">
<RouterLink to="/login/register">
{{ termsLinkText }}
</RouterLink>
</template>
</i18n>

<div>
Plain text to extract
</div>
</div>
</template>

<script>
import { useFluent } from 'fluent-vue'
export default {
setup() {
const { $t } = useFluent()
return {
test: $t('test'),
}
},
computed: {
username() {
return this.$t('user-name')
},
notExists() {
return this.$t('wrong-key')
},
plainText() {
return "Plain text in js to extract"
},
},
}
</script>
9 changes: 9 additions & 0 deletions examples/by-frameworks/fluent-vue/src/fluent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FluentBundle } from '@fluent/bundle'
import { createFluentVue } from 'fluent-vue'

const bundle = new FluentBundle('en')

export const fluent = createFluentVue({
locale: 'en',
bundles: [bundle],
})
11 changes: 11 additions & 0 deletions examples/by-frameworks/fluent-vue/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Vue from 'vue'

import { fluent } from './fluent'
import App from './App.vue'

Vue.use(fluent)

new Vue({
el: '#root',
render: (h) => h(App),
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"consolidate": "^0.16.0",
"esm": "^3.2.25",
"fast-glob": "^3.2.7",
"fluent-vue-cli": "^3.0.0-beta.18",
"glob-gitignore": "^1.0.14",
"htmlparser2": "^6.1.0",
"iconv-lite": "^0.6.3",
Expand Down
24 changes: 24 additions & 0 deletions src/core/CurrentFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { throttle } from 'lodash'
import { ComposedLoader } from './loaders/ComposedLoader'
import { Global } from './Global'
import { VueSfcLoader } from './loaders/VueSfcLoader'
import { FluentVueSfcLoader } from './loaders/FluentVueSfcLoader'
import { Loader, Analyst } from '.'
import { DetectHardStrings } from '~/commands/detectHardStrings'
import { DetectionResult } from '~/core/types'
import { Log } from '~/utils/Log'

export class CurrentFile {
static _vue_sfc_loader: VueSfcLoader | null = null
static _fluent_vue_sfc_loader: FluentVueSfcLoader | null = null
static _composed_loader = new ComposedLoader()
static _onInvalidate = new EventEmitter<boolean>()
static _onInitialized = new EventEmitter<void>()
Expand All @@ -24,6 +26,10 @@ export class CurrentFile {
return Global.hasFeatureEnabled('VueSfc')
}

static get FluentVueSfc() {
return Global.hasFeatureEnabled('FluentVueSfc')
}

static watch(ctx: ExtensionContext) {
ctx.subscriptions.push(workspace.onDidSaveTextDocument(e => this._currentUri && e?.uri === this._currentUri && this.update(e.uri)))
ctx.subscriptions.push(workspace.onDidChangeTextDocument(e => this._currentUri && e?.document?.uri === this._currentUri && this.throttleUpdate(e.document.uri)))
Expand Down Expand Up @@ -61,6 +67,21 @@ export class CurrentFile {
this._vue_sfc_loader = new VueSfcLoader(uri)
}

if (this.FluentVueSfc) {
if (this._fluent_vue_sfc_loader) {
if (uri && this._fluent_vue_sfc_loader.uri.path === uri.path) {
this._fluent_vue_sfc_loader.load()
return
}
else {
this._fluent_vue_sfc_loader.dispose()
this._fluent_vue_sfc_loader = null
}
}
if (uri && uri.fsPath.endsWith('.vue'))
this._fluent_vue_sfc_loader = new FluentVueSfcLoader(uri)
}

this.updateLoaders()
this._onInitialized.fire()
}
Expand All @@ -71,6 +92,9 @@ export class CurrentFile {
if (this.VueSfc && this._vue_sfc_loader)
loaders.push(this._vue_sfc_loader)

if (this.FluentVueSfc && this._fluent_vue_sfc_loader)
loaders.push(this._fluent_vue_sfc_loader)

this._composed_loader.loaders = loaders
}

Expand Down
45 changes: 21 additions & 24 deletions src/core/Extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,27 @@ export async function extractHardStrings(document: TextDocument, extracts: Extra

extracts.sort((a, b) => b.range.start.compareTo(a.range.start))

await Promise.all(
[
// replace
editor.edit((editBuilder) => {
for (const extract of extracts) {
editBuilder.replace(
extract.range,
extract.replaceTo,
)
}
}),
// save keys
CurrentFile.loader.write(
extracts
.filter(i => i.keypath != null && i.message != null)
.map(e => ({
textFromPath: filepath,
filepath: undefined,
keypath: e.keypath!,
value: e.message!,
locale: e.locale || sourceLanguage,
})),
),
],
// replace
await editor.edit((editBuilder) => {
for (const extract of extracts) {
editBuilder.replace(
extract.range,
extract.replaceTo,
)
}
})

// save keys
await CurrentFile.loader.write(
extracts
.filter(i => i.keypath != null && i.message != null)
.map(e => ({
textFromPath: filepath,
filepath: undefined,
keypath: e.keypath!,
value: e.message!,
locale: e.locale || sourceLanguage,
})),
)

if (saveFile)
Expand Down
Loading

0 comments on commit a2c6549

Please sign in to comment.