Skip to content

Commit

Permalink
fix: handles ts script tags as well (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Jun 19, 2023
1 parent 079ef55 commit 1333e5e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
6 changes: 5 additions & 1 deletion assets/example/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<script>
<script lang="ts">
function $i18n(key: string, params?: any) {
return key;
}
let title = $i18n('title');
const def = [$i18n('item0'), $i18n('item1')];
</script>

<main>
<section>
{$i18n('intro')}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vardario/svelte-i18next",
"version": "0.1.12",
"version": "0.1.13",
"description": "",
"license": "MIT",
"author": "Sahin Vardar",
Expand Down
19 changes: 12 additions & 7 deletions src/i18n-preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ import * as compiler from 'svelte/compiler';
import { describe, expect, test } from 'vitest';
import { i18nProcessor } from './i18n-preprocessor.js';
import { __dirname } from './utils.js';
//@ts-expect-error
import { vitePreprocess } from '@sveltejs/kit/vite';

const examplePath = path.resolve(__dirname(import.meta), '../assets/example');
const preprocessor = i18nProcessor({
indent: {
indent: '',
lineEnd: ''
}
});
const preprocessors = [
vitePreprocess(),
i18nProcessor({
indent: {
indent: '',
lineEnd: ''
}
})
];

async function testComponent(filename: string, expectation: string) {
const componentPath = path.resolve(examplePath, filename);
const componentContent = (await fs.readFile(componentPath)).toString('utf-8');
const preprocessed = await compiler.preprocess(componentContent, preprocessor, { filename });
const preprocessed = await compiler.preprocess(componentContent, preprocessors, { filename });

expect(preprocessed.code).toBe(expectation);
}
Expand Down
2 changes: 1 addition & 1 deletion src/i18n-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const i18nProcessor = (options?: I18nProcessorOptions): PreprocessorGroup
},
async script({ content, filename }) {
return preprocess(content, filename, async () => {
const program = parse(content, { ecmaVersion: 'latest' }) as Node;
const program = parse(content, { ecmaVersion: 'latest', sourceType:'module' }) as Node;
const keyPath = extractKeyPathFromFile(filename!);
adjustI18nCall(program, keyPath, callIdentifier);
return { code: generate(program) };
Expand Down

0 comments on commit 1333e5e

Please sign in to comment.