Skip to content

Commit

Permalink
chore(i18n): cleanup i18n file
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Sep 19, 2024
1 parent fc9e5fb commit c0c61b5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 420 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/sync-i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: Sync I18n with Crowdin

on:
push:
branches:
- canary
paths:
- 'packages/frontend/i18n/**'
workflow_dispatch:

jobs:
Expand All @@ -16,6 +20,16 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Generate Crowdin Status
uses: crowdin/github-action@v2
with:
config: packages/frontend/i18n/crowdin.yml
command: status
command_args: translation --plain > status.txt
env:
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}

- name: crowdin action
uses: crowdin/github-action@v2
with:
Expand Down
74 changes: 74 additions & 0 deletions packages/frontend/i18n/cleanup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// this script is used to clean up the unused keys in the i18n file
// just run `node packages/frontend/i18n/cleanup.mjs`

import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { glob } from 'glob';

const REPO_ROOT = path.resolve(
fileURLToPath(import.meta.url),
'..',
'..',
'..',
'..'
);

const files = await glob('packages/frontend/**/src/**/*.{js,tsx,ts}', {
ignore: [
'**/node_modules/**',
'**/packages/frontend/i18n/**',
'**/dist/**',
'**/lib/**',
],
cwd: REPO_ROOT,
absolute: true,
});

const filesWithContent = files.map(file => {
return {
path: file,
content: fs.readFileSync(file, 'utf8'),
};
});

const enjson = JSON.parse(
fs.readFileSync(
path.join(REPO_ROOT, 'packages/frontend/i18n/src/resources/en.json'),
'utf8'
)
);

const keys = Object.keys(enjson).filter(
// exceptions
key => !key.startsWith('com.affine.payment.modal.')
);

const unusedKeys = keys.filter(key => {
const regex1 = new RegExp(`[\`'"]${key.replace('.', '\\.')}[\`'"]`, 'g');
// some place use i18n key like `t[`com.affine.modal.${var}`]`
// com.affine.modal.confirm -> com.affine.modal.
const keyWithoutLastDot = key.replace(/(?<=\.)[^.]+$/, '');
const regex2 = new RegExp(
`[\`'"]${keyWithoutLastDot.replace('.', '\\.')}`,
'g'
);
for (const file of filesWithContent) {
const match = file.content.match(regex1) || file.content.match(regex2);
if (match) {
return false;
}
}
return true;
});

for (const key of unusedKeys) {
delete enjson[key];
}

// write back to file
fs.writeFileSync(
path.join(REPO_ROOT, 'packages/frontend/i18n/src/resources/en.json'),
JSON.stringify(enjson, null, 2)
);
6 changes: 1 addition & 5 deletions packages/frontend/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
"undici": "^6.12.0"
},
"devDependencies": {
"@types/prettier": "^3.0.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.5",
"vitest": "2.1.1"
"glob": "^10.4.1"
},
"version": "0.16.0"
}
16 changes: 0 additions & 16 deletions packages/frontend/i18n/src/resources/en-US.json

This file was deleted.

Loading

0 comments on commit c0c61b5

Please sign in to comment.