Skip to content

Commit

Permalink
Merge pull request #2 from vardario/sahin/improvements
Browse files Browse the repository at this point in the history
feat: added csvToi18n
  • Loading branch information
sahinvardar authored May 31, 2023
2 parents 75a89c4 + 077fddc commit f26043c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 38 deletions.
20 changes: 10 additions & 10 deletions assets/translations/de.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
translation.components.auth.firstName;xxxTODOxxx
translation.components.auth.lastName;xxxTODOxxx
translation.components.auth.password;xxxTODOxxx
translation.components.user-form.firstName;xxxTODOxxx
translation.components.user-form.lastName;xxxTODOxxx
translation.routes.about.page.intro;xxxTODOxxx
translation.routes.layout.header;xxxTODOxxx
translation.routes.page.intro;xxxTODOxxx
translation.routes.page.known-from;xxxTODOxxx
translation.routes.team.page.team-intro;xxxTODOxxx
components.auth.firstName;xxxTODOxxx
components.auth.lastName;xxxTODOxxx
components.auth.password;xxxTODOxxx
components.user-form.firstName;xxxTODOxxx
components.user-form.lastName;xxxTODOxxx
routes.about.page.intro;xxxTODOxxx
routes.layout.header;xxxTODOxxx
routes.page.intro;xxxTODOxxx
routes.page.known-from;xxxTODOxxx
routes.team.page.team-intro;xxxTODOxxx
20 changes: 10 additions & 10 deletions assets/translations/en.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
translation.components.auth.firstName;xxxTODOxxx
translation.components.auth.lastName;xxxTODOxxx
translation.components.auth.password;xxxTODOxxx
translation.components.user-form.firstName;xxxTODOxxx
translation.components.user-form.lastName;xxxTODOxxx
translation.routes.about.page.intro;xxxTODOxxx
translation.routes.layout.header;xxxTODOxxx
translation.routes.page.intro;xxxTODOxxx
translation.routes.page.known-from;xxxTODOxxx
translation.routes.team.page.team-intro;xxxTODOxxx
components.auth.firstName;xxxTODOxxx
components.auth.lastName;xxxTODOxxx
components.auth.password;xxxTODOxxx
components.user-form.firstName;xxxTODOxxx
components.user-form.lastName;xxxTODOxxx
routes.about.page.intro;xxxTODOxxx
routes.layout.header;xxxTODOxxx
routes.page.intro;xxxTODOxxx
routes.page.known-from;xxxTODOxxx
routes.team.page.team-intro;xxxTODOxxx
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.1",
"version": "0.1.2",
"description": "",
"license": "MIT",
"author": "Sahin Vardar",
Expand Down
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function diffLanguageFile(params: DiffProps) {

export async function isInSync(params: SynchroniesParams) {
const keys = await extractI18NextKeys(params.dirs);
let result = true;

for (const language of params.languages) {
const languageFile = path.resolve(params.output, language) + '.csv';
Expand All @@ -85,9 +86,9 @@ export async function isInSync(params: SynchroniesParams) {
console.error(`${languageFile} is not in sync!`);
console.error('adds', JSON.stringify(added, null, 2));
console.error('removals', JSON.stringify(removed, null, 2));
return false;
result = false;
}
}

return true;
return result;
}
20 changes: 10 additions & 10 deletions src/extract-i18n-keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ describe('extract-i18n-keys', () => {

expect(keys.sort()).toStrictEqual(
[
'translation.components.auth.firstName',
'translation.components.auth.lastName',
'translation.components.auth.password',
'translation.components.user-form.firstName',
'translation.components.user-form.lastName',
'translation.routes.about.page.intro',
'translation.routes.layout.header',
'translation.routes.page.intro',
'translation.routes.page.known-from',
'translation.routes.team.page.team-intro'
'components.auth.firstName',
'components.auth.lastName',
'components.auth.password',
'components.user-form.firstName',
'components.user-form.lastName',
'routes.about.page.intro',
'routes.layout.header',
'routes.page.intro',
'routes.page.known-from',
'routes.team.page.team-intro'
].sort()
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/extract-i18n-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ export async function extractI18NextKeys(dirSvelteApps: string[]) {
result.push(...appResult);
}

return result.map(key => `translation.${key}`).sort();
return result.map(key => `${key}`).sort();
}
1 change: 1 addition & 0 deletions src/i18n-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
23 changes: 19 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import url from 'node:url';

const CSV_OPTIONS = {
delimiter: ';',
trim: true,
bom: true
};

export async function scanDir(dir: string, filter?: (file: string) => boolean) {
const result: string[] = [];

Expand Down Expand Up @@ -86,9 +92,18 @@ export function recordsToCsv(items: Record<string, string>) {

return stringify(
csv.sort((a, b) => a[0].localeCompare(b[0])),
{
bom: true,
delimiter: ';'
}
CSV_OPTIONS
);
}

export function csvToI18Next(csv: string) {
const object = {};

const items = parse(csv, CSV_OPTIONS) as string[];

for (const item of items) {
const [path, value] = item;
_.set(object, path, value);
}
return object;
}

0 comments on commit f26043c

Please sign in to comment.