Skip to content

Commit

Permalink
feat: node script to clean up localization files 'npm run clean-local…
Browse files Browse the repository at this point in the history
…izations'

- removes unused localizations and sorts the remaining ones
- integration in 'npm run check'
- documentation and migration note
  • Loading branch information
suschneider authored and shauke committed Sep 21, 2020
1 parent d63f686 commit 9863fe7
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"name": "clean-up-localizations",
"request": "launch",
"program": "${workspaceFolder}/scripts/clean-up-localizations",
"args": [],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "chrome",
"request": "launch",
Expand Down
23 changes: 23 additions & 0 deletions docs/concepts/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,29 @@ For this purpose a [Gradle plugin](https://gitlab.intershop.de/ISPWA/ngx-transla
In the current state of the Intershop Progressive Web App, the converted localization properties from _a_responsive_ (without _app_sf_responsive_b2b_ and _app_sf_responsive_costcenter_) were added and should be used within the HTML templates.
## Localization File Clean Up Process
Localization files require constant maintenance to keep them up to date.
They should not contain any localizations that are no longer required by the project sources or functionality.
The PWA project provides a script that helps to keep the localization files clean.
It can be run with `npm run clean-localizations`.
The script removes all unused localization keys from the default localization file `src/assets/i18n/en_US.json` and sorts the remaining keys.
In a second step it generates localization files for the other available language files under `src/assets/i18n` by using the localization keys found for the default language.
Localization keys that are not available in these files - meaning they have no translation - are logged.
Not explicitly used localization keys, such as dynamic created keys or error keys from REST calls, are handled separately.
Their patterns are searched in all the localization keys of the default localization file and all matches will be included in the new cleaned up file.
Therefore additional patterns have to be added for additional keys used in this way.
```javascript
// regular expression for patterns of not explicitly used localization keys (dynamic created keys, error keys from REST calls)
// ADDITIONAL PATTERNS HAVE TO BE ADDED HERE
const regEx = /account\.login\..*\.message|.*\.error\..*/i;
```
The clean up script is integrated in the full check run (`npm run check`) and will also be performed in continuous integration on the whole code base.
## Extend Locales
To learn how languages other than English, German and French can be used in the progressive web app, see [Configuration - Extend Localization](./configuration.md#extend-locales).
5 changes: 5 additions & 0 deletions docs/guides/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ kb_sync_latest_only

# Migrations

## 0.23 to 0.24

We introduced a [localization file clean up script](../concepts/localization.md#localization-file-clean-up-process) that removes all unused localization keys from the localization files and sorts the remaining keys.
The clean up result is contained in a separate commit that should probably not be applied during a migration and instead a `npm run clean-localizations` should be performed on the project sources.

## 0.22 to 0.23

We removed deprecated exports related to the NgRx testing refactorings introduced in version 0.21.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"lint": "ng lint",
"format": "node docs/check-sentence-newline && stylelint \"**/*.{css,scss}\" --fix && prettier --loglevel warn --write \"**/*.*\"",
"dead-code": "npx ts-node scripts/find-dead-code.ts",
"clean-localizations": "node scripts/clean-up-localizations",
"clean": "git clean -xdf -e \"*environment.local.ts\" && npm ci",
"check": "npm run lint -- --fix && npm run format && tsc --project tsconfig.all.json && npm run build && npm run test && npm run dead-code",
"check": "npm run lint -- --fix && npm run format && tsc --project tsconfig.all.json && npm run build && npm run test && npm run dead-code && npm run clean-localizations",
"clean-check": "npm run clean && npm run check",
"changelog": "npx -p conventional-changelog-cli conventional-changelog -n intershop-changelog.js -i CHANGELOG.md -s",
"3rd-party-licenses": "npm ci && npx license-checker --csv --out 3rd-party-licenses.txt --customPath templates/3rd-party-licenses_format.json",
Expand Down
73 changes: 73 additions & 0 deletions scripts/clean-up-localizations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const _ = require('lodash');
const fs = require('fs');
const glob = require('glob');
const { execSync } = require('child_process');

const localizationFile_default = 'src/assets/i18n/en_US.json';

// regular expression for patterns of not explicitly used localization keys (dynamic created keys, error keys from REST calls)
// ADDITIONAL PATTERNS HAVE TO BE ADDED HERE
const regEx = /account\.login\..*\.message|.*\.error.*/i;

// store localizations from default localization file in an object
const localizations_default = JSON.parse(fs.readFileSync(localizationFile_default, 'utf8'));
console.log('Clean up file', localizationFile_default, 'as default localization file');

// add not explicitly used localization keys with their localization values
const localizationsFound = {};
Object.keys(localizations_default)
.filter(localization => regEx.test(localization))
.map(localizationKey => {
localizationsFound[localizationKey] = localizations_default[localizationKey];
delete localizations_default[localizationKey];
});

// go through directory recursively and find files to be searched
const filesToBeSearched = glob.sync('{src,projects}/**/!(*.spec).{ts,html}');

// add used localization keys with their localization values
filesToBeSearched.forEach(filePath => {
const fileContent = fs.readFileSync(filePath);
for (const localizationKey in localizations_default) {
if (fileContent.includes(localizationKey)) {
// store found localizations
localizationsFound[localizationKey] = localizations_default[localizationKey];
delete localizations_default[localizationKey];
}
}
});

// log removed localization keys
Object.keys(localizations_default).forEach(localizationKey => {
console.log('\x1b[31m%s\x1b[0m', ` Localization key ${localizationKey} removed because it is not used`);
});

// sort found localizations
const localizationsFoundOrdered = {};
Object.keys(localizationsFound)
.sort()
.forEach(key => {
localizationsFoundOrdered[key] = localizationsFound[key];
});

// write found localizations into default localization file
fs.writeFileSync(localizationFile_default, JSON.stringify(localizationsFoundOrdered, null, 2));

// find localization files for other languages
const localizationFiles_lang = glob
.sync('src/assets/i18n/*.json')
.filter(filePath => filePath !== localizationFile_default);

// create cleaned localization files for other languages
localizationFiles_lang.forEach(file => {
console.log('\nClean up file', file);
const localizations_lang = JSON.parse(fs.readFileSync(file, 'utf8'));
// find missing localization keys
_.difference(Object.keys(localizationsFoundOrdered), Object.keys(localizations_lang)).forEach(key => {
console.log('\x1b[33m%s\x1b[0m', ` Localization key ${key} not found in ${file}`);
});
fs.writeFileSync(file, JSON.stringify(_.pick(localizations_lang, Object.keys(localizationsFoundOrdered)), null, 2));
});

// run prettier to fix any formatting or line and file ending inconsistencies
execSync('npx prettier --write src/assets/i18n/*.*');

0 comments on commit 9863fe7

Please sign in to comment.