Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(i18n): cleanup i18n file #8318

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .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,7 +20,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: crowdin action
- name: Crowdin action
uses: crowdin/github-action@v2
with:
upload_sources: true
Expand Down
4 changes: 0 additions & 4 deletions packages/common/infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"peerDependencies": {
"@affine/templates": "*",
"@blocksuite/presets": "*",
"async-call-rpc": "*",
"electron": "*",
"react": "*",
"yjs": "^13"
Expand All @@ -56,9 +55,6 @@
"@blocksuite/presets": {
"optional": true
},
"async-call-rpc": {
"optional": true
},
"electron": {
"optional": true
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Menu, MenuItem, MenuTrigger } from '@affine/component/ui/menu';
import { calcLocaleCompleteness } from '@affine/i18n';
import { DoneIcon } from '@blocksuite/icons/rc';
import type { ReactElement } from 'react';
import { memo } from 'react';
Expand All @@ -10,17 +11,19 @@ import * as styles from './style.css';
const LanguageMenuContent = memo(function LanguageMenuContent() {
const { currentLanguage, languagesList, onLanguageChange } =
useLanguageHelper();

return (
<>
{languagesList.map(option => {
const selected = currentLanguage?.originalName === option.originalName;
const completeness = calcLocaleCompleteness(option.tag);
return (
<MenuItem
key={option.name}
title={option.name}
lang={option.tag}
onSelect={() => onLanguageChange(option.tag)}
suffix={(option.Completeness * 100).toFixed(0) + '%'}
suffix={(completeness * 100).toFixed(0) + '%'}
data-selected={selected}
className={styles.menuItem}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function useLanguageHelper() {
tag: item.tag,
originalName: item.originalName,
name: item.name,
Completeness: item.completeRate,
})),
[]
);
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/src/resources/*',
'**/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)
);
5 changes: 1 addition & 4 deletions packages/frontend/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +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",
"glob": "^11.0.0",
"vitest": "2.1.1"
},
"version": "0.16.0"
Expand Down
22 changes: 22 additions & 0 deletions packages/frontend/i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,25 @@ export function setUpLanguage(i: i18n) {
}
return i.changeLanguage(language);
}

const cachedCompleteness: Record<string, number> = {};
export const calcLocaleCompleteness = (
locale: (typeof LOCALES)[number]['tag']
) => {
if (cachedCompleteness[locale]) {
return cachedCompleteness[locale];
}
const base = LOCALES.find(item => item.base);
if (!base) {
throw new Error('Base language not found');
}
const target = LOCALES.find(item => item.tag === locale);
if (!target) {
throw new Error('Locale not found');
}
const baseKeyCount = Object.keys(base.res).length;
const translatedKeyCount = Object.keys(target.res).length;
const completeness = translatedKeyCount / baseKeyCount;
cachedCompleteness[target.tag] = completeness;
return completeness;
};
16 changes: 0 additions & 16 deletions packages/frontend/i18n/src/resources/en-US.json

This file was deleted.

Loading
Loading