Skip to content

Commit

Permalink
Add translator scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
barshathakuri committed Mar 19, 2024
1 parent 427e74e commit ea3877e
Show file tree
Hide file tree
Showing 7 changed files with 4,383 additions and 1,385 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
run: yarn install

- name: Css Lint
run: yarn css-lint
run: yarn lint:css
typecheck:
name: Typecheck
runs-on: ubuntu-latest
Expand Down
14 changes: 13 additions & 1 deletion .unimportedrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
"#assets/content/operational_timeline_title.svg?react",
"#assets/content/operational_timeline_body.svg?react"
],
"ignoreUnused": ["@tinymce/tinymce-react", "@mapbox/mapbox-gl-draw"],
"ignoreUnused": [
"@tinymce/tinymce-react",
"@mapbox/mapbox-gl-draw",
"@apollo/client",
"@graphql-codegen/introspection",
"@graphql-codegen/typescript-operations",
"@togglecorp/re-map",
"@turf/bbox",
"@turf/buffer",
"graphql-request",
"sanitize-html",
"@sentry/react"
],
"extensions": [".ts", ".js", ".tsx", ".jsx"],
"aliases": {
"#assets/*": ["./src/assets/*"],
Expand Down
76 changes: 0 additions & 76 deletions CHECKLIST.md

This file was deleted.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
"lint:unused": "unimported",
"lint:translation": "node ./scripts/translator.js",
"lint": "yarn lint:js && yarn lint:css && yarn lint:unused && yarn lint:translation",
"typecheck": "tsc",
"postinstall": "patch-package"
"test": "vitest",
"test:coverage": "vitest run --coverage",
"typecheck": "tsc"
},
"dependencies": {
"@apollo/client": "^3.9.7",
"@graphql-codegen/introspection": "^4.0.3",
"@graphql-codegen/typescript-operations": "^4.2.0",
"@ifrc-go/icons": "^1.3.1",
"@ifrc-go/icons": "^1.3.3",
"@mapbox/mapbox-gl-draw": "^1.2.0",
"@sentry/react": "^7.81.1",
"@togglecorp/fujs": "^2.1.1",
"@togglecorp/re-map": "^0.2.0-beta-6",
"@turf/bbox": "^6.5.0",
Expand All @@ -30,7 +32,7 @@
"mapbox-gl": "^1.13.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.11.1",
"react-router-dom": "^6.22.3",
"sanitize-html": "^2.9.0"
},
"devDependencies": {
Expand All @@ -39,7 +41,8 @@
"@types/mapbox-gl": "^1.13.0",
"@types/node": "^20.1.3",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@types/react-dom": "^18.2.22",
"@types/react-router-dom": "^5.3.3",
"@typescript-eslint/eslint-plugin": "^5.59.5",
"@typescript-eslint/parser": "^5.59.5",
"@vitejs/plugin-react-swc": "^3.5.0",
Expand All @@ -60,7 +63,6 @@
"fast-glob": "^3.3.0",
"happy-dom": "^9.18.3",
"openapi-typescript": "6.5.5",
"patch-package": "^7.0.0",
"postcss": "^8.3.0",
"postcss-nested": "^6.0.1",
"postcss-normalize": "^10.0.1",
Expand Down
26 changes: 0 additions & 26 deletions patches/vite-plugin-webfont-dl+3.7.4.patch

This file was deleted.

83 changes: 83 additions & 0 deletions scripts/translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { isDefined, listToMap, mapToList } from '@togglecorp/fujs';
import fg from 'fast-glob';
import { readFile } from 'fs';
import { join } from 'path';
import { cwd, exit } from 'process';
import { promisify } from 'util';

const glob = fg.glob;

const readFilePromisify = promisify(readFile);

function getDuplicates(
list,
keySelector,
) {
if (!list) {
return undefined;
}
const counts = listToMap(
list,
keySelector,
(_, key, __, acc) => {
const value = acc[key];
return isDefined(value) ? value + 1 : 1;
},
);

return list
.filter((item) => counts[keySelector(item)] > 1)
.sort((foo, bar) => keySelector(foo).localeCompare(keySelector(bar)));
}

const currentDir = cwd();
const fullPath = join(currentDir, 'src/**/i18n.json');
console.info('Searching in', fullPath);

const files = await glob(fullPath, { ignore: ['node_modules'], absolute: true });
console.info(`Found ${files.length} i18n.json files.`);

const translationsPromise = files.map(async (file) => {
const fileDescriptor = await readFilePromisify(file);
const filename = `.${file.slice(currentDir.length)}`;
try {
return {
file: filename,
content: JSON.parse(fileDescriptor.toString()),
};
} catch (e) {
console.error(`Error while parsing JSON for ${filename}`);
exit(1);
}
});
const translations = await Promise.all(translationsPromise);

const strings = translations.flatMap((translation) => {
const { file, content } = translation;

return mapToList(
content.strings,
(item, key) => ({
file,
namespace: content.namespace,
key,
value: item,
}),
);
});

const namespaces = new Set(strings.map((item) => item.namespace));

console.info(`Found ${namespaces.size} namespaces.`);
console.info(`Found ${strings.length} strings.`);

const duplicates = getDuplicates(
strings,
(string) => `${string.namespace}:${string.key}`,
);

console.error(`Found ${duplicates.length} duplicated strings.`);
if (duplicates.length > 0) {
console.info(JSON.stringify(duplicates, null, 2));
exit(2);
}
Loading

0 comments on commit ea3877e

Please sign in to comment.