Skip to content

Add an option to parse FTL during build #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ export default defineConfig({
SFCFluentPlugin({ // define messages in SFCs
blockType: 'fluent', // default 'fluent' - name of the block in SFCs
checkSyntax: true, // default true - whether to check syntax of the messages
parseFtl: false, // default false - whether to parse ftl files during build
}),
ExternalFluentPlugin({ // define messages in external ftl files
baseDir: path.resolve('src'), // required - base directory for Vue files
ftlDir: path.resolve('src/locales'), // required - directory with ftl files
locales: ['en', 'da'], // required - list of locales
checkSyntax: true, // default true - whether to check syntax of the messages
parseFtl: false, // default false - whether to parse ftl files during build
}),
],
})
Expand Down
2 changes: 2 additions & 0 deletions __tests__/fixtures/errors.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ shared-photos =
[female] her stream
*[other] their stream
}.

entry-without-error = Hello, World!
</fluent>
Binary file modified __tests__/frameworks/vite/__snapshots__/external.spec.ts.snap
Binary file not shown.
Binary file modified __tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap
Binary file not shown.
76 changes: 76 additions & 0 deletions __tests__/frameworks/vite/external.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ describe('Vite external', () => {
expect(code).toMatchSnapshot()
})

describe('parseFtl', () => {
it('parses ftl syntax during compilation', async () => {
// Arrange
// Act
const code = await compile({
plugins: [
vue3({
compiler,
}),
ExternalFluentPlugin({
baseDir: resolve(baseDir, 'fixtures'),
ftlDir: resolve(baseDir, 'fixtures/ftl'),
locales: ['en', 'da'],
parseFtl: true,
}),
],
}, '/fixtures/components/external.vue')

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

it('virtual:ftl-for-file', async () => {
// Arrange
// Act
Expand Down Expand Up @@ -106,4 +129,57 @@ describe('Vite external', () => {
"
`)
})

it('can import FTL files', async () => {
// Arrange
// Act
const code = await compile({
plugins: [
vue3({
compiler,
}),
ExternalFluentPlugin({
baseDir: resolve(baseDir, 'fixtures'),
ftlDir: resolve(baseDir, 'fixtures/ftl'),
locales: ['en', 'da'],
}),
],
}, '/fixtures/ftl/en/importer.js.ftl')

// Assert
expect(code).toMatchInlineSnapshot(`
"=== /fixtures/ftl/en/importer.js.ftl ===

import { FluentResource } from "/@id/virtual:empty:fluent-bundle"

export default /*#__PURE__*/ new FluentResource("key = Translations for js file")
"
`)
})

it('can parse FTL files', async () => {
// Arrange
// Act
const code = await compile({
plugins: [
vue3({
compiler,
}),
ExternalFluentPlugin({
baseDir: resolve(baseDir, 'fixtures'),
ftlDir: resolve(baseDir, 'fixtures/ftl'),
locales: ['en', 'da'],
parseFtl: true,
}),
],
}, '/fixtures/ftl/en/importer.js.ftl')

// Assert
expect(code).toMatchInlineSnapshot(`
"=== /fixtures/ftl/en/importer.js.ftl ===

export default {"body":[{"id":"key","value":"Translations for js file","attributes":{}}]}
"
`)
})
})
40 changes: 40 additions & 0 deletions __tests__/frameworks/vite/sfc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,46 @@ describe('Vite SFC', () => {
expect(code).toMatchSnapshot()
})

describe('parseFtl', () => {
it('parses ftl syntax during compilation', async () => {
// Arrange
// Act
const code = await compile({
plugins: [
vue3({
compiler,
}),
SFCFluentPlugin({
parseFtl: true,
checkSyntax: false,
}),
],
}, '/fixtures/test.vue')

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

it('generates block code even if it has errors', async () => {
// Arrange
// Act
const code = await compile({
plugins: [
vue3({
compiler,
}),
SFCFluentPlugin({
parseFtl: true,
checkSyntax: false,
}),
],
}, '/fixtures/errors.vue')

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

it('supports custom blockType', async () => {
// Arrange
// Act
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"release": "dotenv release-it"
},
"peerDependencies": {
"@fluent/bundle": "*",
"@nuxt/kit": "^3"
},
"peerDependenciesMeta": {
Expand All @@ -111,6 +112,7 @@
},
"devDependencies": {
"@antfu/eslint-config": "^4.3.0",
"@fluent/bundle": "^0.18.0",
"@nuxt/kit": "^3.15.4",
"@nuxt/schema": "^3.15.4",
"@release-it-plugins/lerna-changelog": "7.0.0",
Expand Down
55 changes: 45 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/loader-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export function parseVueRequest(id: string) {
ret.type = params.get('type') as VueQuery['type']

if (params.has('blockType'))
ret.blockType = params.get('blockType')
ret.blockType = params.get('blockType') ?? undefined

if (params.has('index'))
ret.index = Number(params.get('index'))

if (params.has('locale'))
ret.locale = params.get('locale')
ret.locale = params.get('locale') ?? undefined

return {
filename,
Expand Down
Loading