Skip to content

Commit

Permalink
Add support for translations in actual addons
Browse files Browse the repository at this point in the history
  • Loading branch information
robinborst95 committed Apr 20, 2022
1 parent 8eb8ea8 commit 3304981
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ build/Release
node_modules/
jspm_packages/

# Include node modules used in tests
!fixtures/**/node_modules

# TypeScript v1 declaration files
typings/

Expand Down
14 changes: 14 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ exports[`Test Fixtures emblem 1`] = `

exports[`Test Fixtures emblem 2`] = `Map {}`;

exports[`Test Fixtures external-addon-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
👏 No unused translations were found!
👏 No missing translations were found!
"
`;

exports[`Test Fixtures external-addon-translations 2`] = `Map {}`;

exports[`Test Fixtures in-repo-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default class ApplicationController extends Controller {
get foo() {
return this.intl.t('js-translation');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{t "hbs-translation"}}
{{t "external-addon.used-by-app-translation"}}
{{t "company.scoped-addon.used-by-app-translation"}}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions fixtures/external-addon-translations/translations/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hbs-translation: HBS!
js-translation: JS!
37 changes: 31 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ async function run(rootDir, options = {}) {

log(`${step(3)} ⚙️ Checking for unused translations...`);

let translationFiles = await findTranslationFiles(rootDir);
let existingTranslationKeys = await analyzeTranslationFiles(rootDir, translationFiles);
let ownTranslationFiles = await findOwnTranslationFiles(rootDir);
let addonTranslationFiles = await findAddonTranslationFiles(rootDir);
let existingOwnTranslationKeys = await analyzeTranslationFiles(rootDir, ownTranslationFiles);
let existingAddonTranslationKeys = await analyzeTranslationFiles(rootDir, addonTranslationFiles);
let existingTranslationKeys = mergeMaps(existingOwnTranslationKeys, existingAddonTranslationKeys);
let whitelist = config.whitelist || [];

let unusedTranslations = findDifferenceInTranslations(
existingTranslationKeys,
existingOwnTranslationKeys,
usedTranslationKeys,
whitelist
);
Expand Down Expand Up @@ -82,7 +85,7 @@ async function run(rootDir, options = {}) {
let totalErrors = missingTranslations.size + unusedTranslations.size;

if (shouldFix) {
removeUnusedTranslations(writeToFile, rootDir, translationFiles, unusedTranslations);
removeUnusedTranslations(writeToFile, rootDir, ownTranslationFiles, unusedTranslations);
log();
log(' 👏 All unused translations were removed');
}
Expand Down Expand Up @@ -113,8 +116,15 @@ async function findInRepoFiles(cwd) {
return globby(joinPaths(inRepoFolders, ['**/*.js', '**/*.hbs', '**/*.emblem']), { cwd });
}

async function findTranslationFiles(cwd) {
let inputFolders = ['', ...findInRepoPaths(cwd)];
async function findOwnTranslationFiles(cwd) {
return findTranslationFiles(cwd, ['', ...findInRepoPaths(cwd)]);
}

async function findAddonTranslationFiles(cwd) {
return findTranslationFiles(cwd, ['node_modules/*', 'node_modules/@*/*']);
}

async function findTranslationFiles(cwd, inputFolders) {
let translationPaths = joinPaths(inputFolders, ['translations']);

return globby(joinPaths(translationPaths, ['**/*.json', '**/*.yaml', '**/*.yml']), {
Expand Down Expand Up @@ -388,4 +398,19 @@ function getNestedAttribute(parent, keys) {
return attribute;
}

function mergeMaps(mapA, mapB) {
let resultMap = new Map([...mapA]);

for (let [key, bFiles] of mapB) {
if (!resultMap.has(key)) {
resultMap.set(key, bFiles);
} else {
let aFiles = resultMap.get(key);
resultMap.set(key, new Set([...aFiles, ...bFiles]));
}
}

return resultMap;
}

module.exports = { run, generateFileList };

0 comments on commit 3304981

Please sign in to comment.