Skip to content

Commit 1ef57da

Browse files
committed
Correctly handle importing external FTL files
1 parent dd77a09 commit 1ef57da

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

__tests__/frameworks/vite/external.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,57 @@ describe('Vite external', () => {
129129
"
130130
`)
131131
})
132+
133+
it('can import FTL files', async () => {
134+
// Arrange
135+
// Act
136+
const code = await compile({
137+
plugins: [
138+
vue3({
139+
compiler,
140+
}),
141+
ExternalFluentPlugin({
142+
baseDir: resolve(baseDir, 'fixtures'),
143+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
144+
locales: ['en', 'da'],
145+
}),
146+
],
147+
}, '/fixtures/ftl/en/importer.js.ftl')
148+
149+
// Assert
150+
expect(code).toMatchInlineSnapshot(`
151+
"=== /fixtures/ftl/en/importer.js.ftl ===
152+
153+
import { FluentResource } from "/@id/virtual:empty:fluent-bundle"
154+
155+
export default /*#__PURE__*/ new FluentResource(new FluentResource("key = Translations for js file"))
156+
"
157+
`)
158+
})
159+
160+
it('can parse FTL files', async () => {
161+
// Arrange
162+
// Act
163+
const code = await compile({
164+
plugins: [
165+
vue3({
166+
compiler,
167+
}),
168+
ExternalFluentPlugin({
169+
baseDir: resolve(baseDir, 'fixtures'),
170+
ftlDir: resolve(baseDir, 'fixtures/ftl'),
171+
locales: ['en', 'da'],
172+
parseFtl: true,
173+
}),
174+
],
175+
}, '/fixtures/ftl/en/importer.js.ftl')
176+
177+
// Assert
178+
expect(code).toMatchInlineSnapshot(`
179+
"=== /fixtures/ftl/en/importer.js.ftl ===
180+
181+
export default /*#__PURE__*/ new FluentResource({"body":[{"id":"key","value":"Translations for js file","attributes":{}}]})
182+
"
183+
`)
184+
})
132185
})

src/plugins/ftl/inject.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export function getInjectFtl(options: { checkSyntax: boolean, parseFtl: boolean
3030

3131
const magic = new MagicString(source)
3232
const importString = options.parseFtl === true ? '' : '\nimport { FluentResource } from \'@fluent/bundle\'\n'
33-
const localeString = locale == null ? '' : locale
3433

3534
if (source.length === 0) {
3635
magic.append('undefined')
@@ -43,9 +42,14 @@ export function getInjectFtl(options: { checkSyntax: boolean, parseFtl: boolean
4342
magic.overwrite(0, source.length, `new FluentResource(${JSON.stringify(normalize(source))})`)
4443
}
4544

46-
magic.prepend(importString + template[0] + localeString + template[1])
47-
if (template[2] != null)
45+
if (template.length === 2) {
46+
magic.prepend(importString + template[0])
47+
magic.append(template[1])
48+
}
49+
else if (template.length === 3) {
50+
magic.prepend(importString + template[0] + locale + template[1])
4851
magic.append(template[2])
52+
}
4953

5054
return {
5155
code: {

0 commit comments

Comments
 (0)