Skip to content

Commit

Permalink
fix: support for more input components (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Jun 26, 2023
1 parent 3c4d788 commit 333d201
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions assets/example/components/user-form.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Form>
<Input name="firstName"/>
<Input name="lastName"/>
<Select name="countryCode"/>
<BubbleSelect name="amenities"/>
<LocalizedInput name="text"/>
</Form>
3 changes: 3 additions & 0 deletions assets/translations/de.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
components.auth.firstName;xxxTODOxxx
components.auth.lastName;xxxTODOxxx
components.auth.password;xxxTODOxxx
components.user-form.amenities;xxxTODOxxx
components.user-form.countryCode;xxxTODOxxx
components.user-form.firstName;xxxTODOxxx
components.user-form.lastName;xxxTODOxxx
components.user-form.text;xxxTODOxxx
routes.about.page.intro;xxxTODOxxx
routes.layout.header;xxxTODOxxx
routes.page.intro;xxxTODOxxx
Expand Down
3 changes: 3 additions & 0 deletions assets/translations/en.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
components.auth.firstName;xxxTODOxxx
components.auth.lastName;xxxTODOxxx
components.auth.password;xxxTODOxxx
components.user-form.amenities;xxxTODOxxx
components.user-form.countryCode;xxxTODOxxx
components.user-form.firstName;xxxTODOxxx
components.user-form.lastName;xxxTODOxxx
components.user-form.text;xxxTODOxxx
routes.about.page.intro;xxxTODOxxx
routes.layout.header;xxxTODOxxx
routes.page.intro;xxxTODOxxx
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.14",
"version": "0.1.15",
"description": "",
"license": "MIT",
"author": "Sahin Vardar",
Expand Down
5 changes: 4 additions & 1 deletion src/extract-i18n-keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ describe('extract-i18n-keys', () => {
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const exampleDir = path.resolve(__dirname, '../assets/example');

const keys = await extractI18NextKeys([exampleDir]);
const keys = await extractI18NextKeys([exampleDir]);

expect(keys.sort()).toStrictEqual(
[
'components.auth.firstName',
'components.auth.lastName',
'components.auth.password',
'components.user-form.amenities',
'components.user-form.countryCode',
'components.user-form.firstName',
'components.user-form.lastName',
'components.user-form.text',
'routes.about.page.intro',
'routes.layout.header',
'routes.page.intro',
Expand Down
8 changes: 4 additions & 4 deletions src/extract-i18n-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import * as compiler from 'svelte/compiler';
import { Ast } from 'svelte/types/compiler/interfaces.js';
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
import { extractKeyPathFromFile, stripScriptTag } from './string-utils.js';
import { scanDir } from './utils.js';
import { SUPPORTED_COMPONENTS, scanDir } from './utils.js';

function extractI18nKeys(ast: Ast | Node, callIdentifier: string): string[] {
const result: string[] = [];

compiler.walk(ast, {
enter: (node: any) => {
if (node.type === 'CallExpression') {
if (node.type === 'CallExpression') {
const callExpressionNode = node as CallExpression;
const { callee } = callExpressionNode;
if (callee.type === 'Identifier' && callee.name === callIdentifier) {
Expand All @@ -27,7 +27,7 @@ function extractI18nKeys(ast: Ast | Node, callIdentifier: string): string[] {
}
}

if (node.type === 'InlineComponent' && ['Input', 'Checkbox'].includes(node.name)) {
if (node.type === 'InlineComponent' && SUPPORTED_COMPONENTS.includes(node.name)) {
const nameAttribute = node.attributes?.find((attr: any) => attr.name === 'name');
if (nameAttribute) {
result.push(nameAttribute.value[0].data);
Expand Down Expand Up @@ -57,7 +57,7 @@ export async function processSvelteFile(
return { code: content };
},
async script({ content, filename }) {
const ast = acorn.parse(content, { ecmaVersion: 'latest', sourceType:'module' }) as Node;
const ast = acorn.parse(content, { ecmaVersion: 'latest', sourceType: 'module' }) as Node;
keys.push(...extractI18nKeys(ast, callIdentifier));
return { code: content };
}
Expand Down
3 changes: 3 additions & 0 deletions src/i18n-preprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('i18n-preprocessor', () => {
`<Form>
<Input name="firstName" label={$i18n("components.user-form.firstName")}/>
<Input name="lastName" label={$i18n("components.user-form.lastName")}/>
<Select name="countryCode" label={$i18n("components.user-form.countryCode")}/>
<BubbleSelect name="amenities" label={$i18n("components.user-form.amenities")}/>
<LocalizedInput name="text" label={$i18n("components.user-form.text")}/>
</Form>`
);
});
Expand Down
5 changes: 3 additions & 2 deletions src/i18n-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as compiler from 'svelte/compiler';
import { Ast, TemplateNode } from 'svelte/types/compiler/interfaces';
import type { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
import { extractKeyPathFromFile, stripScriptTag } from './string-utils.js';
import { SUPPORTED_COMPONENTS } from './utils.js';

export function create18nCallLabelAttribute(callIdentifier: string, i18nKey: string) {
const expression = parse(`${callIdentifier}("${i18nKey}")`, { ecmaVersion: 'latest' });
Expand Down Expand Up @@ -51,7 +52,7 @@ export function adjustI18nCall(ast: Ast | Node, prefix: string, callIdentifier:
export function adjustInputElementLabels(ast: Ast, prefix: string, callIdentifier: string = '$i18n') {
compiler.walk(ast.html, {
enter: (node: TemplateNode) => {
if (node.type === 'InlineComponent' && ['Input', 'Checkbox'].includes(node.name)) {
if (node.type === 'InlineComponent' && SUPPORTED_COMPONENTS.includes(node.name)) {
const nameAttribute = node.attributes?.find((attr: any) => attr.name === 'name');
const labelAttribute = node.attributes?.find((attr: any) => attr.name === 'label');

Expand Down Expand Up @@ -129,7 +130,7 @@ export const i18nProcessor = (options?: I18nProcessorOptions): PreprocessorGroup
},
async script({ content, filename }) {
return preprocess(content, filename, async () => {
const program = parse(content, { ecmaVersion: 'latest', sourceType:'module' }) 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
3 changes: 1 addition & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ export async function scanDir(dir: string, filter?: (file: string) => boolean) {
return result.sort();
}



export function __dirname(meta: ImportMeta) {
const __filename = url.fileURLToPath(import.meta.url);
return path.dirname(__filename);
}

export const SUPPORTED_COMPONENTS = ['Input', 'Checkbox', 'Select', 'BubbleSelect', 'LocalizedInput'];

0 comments on commit 333d201

Please sign in to comment.