Skip to content

Commit 2ce9f10

Browse files
committed
Add support for translations in actual addons
1 parent 3511aea commit 2ce9f10

File tree

10 files changed

+69
-6
lines changed

10 files changed

+69
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ build/Release
3636
node_modules/
3737
jspm_packages/
3838

39+
# Include node modules used in tests
40+
!fixtures/**/node_modules
41+
3942
# TypeScript v1 declaration files
4043
typings/
4144

__snapshots__/test.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ exports[`Test Fixtures emblem 1`] = `
4444

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

47+
exports[`Test Fixtures external-addon-translations 1`] = `
48+
"[1/4] 🔍 Finding JS and HBS files...
49+
[2/4] 🔍 Searching for translations keys in JS and HBS files...
50+
[3/4] ⚙️ Checking for unused translations...
51+
[4/4] ⚙️ Checking for missing translations...
52+
53+
👏 No unused translations were found!
54+
55+
👏 No missing translations were found!
56+
"
57+
`;
58+
59+
exports[`Test Fixtures external-addon-translations 2`] = `Map {}`;
60+
4761
exports[`Test Fixtures in-repo-translations 1`] = `
4862
"[1/4] 🔍 Finding JS and HBS files...
4963
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Controller from '@ember/controller';
2+
3+
export default class ApplicationController extends Controller {
4+
get foo() {
5+
return this.intl.t('js-translation');
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{t "hbs-translation"}}
2+
{{t "external-addon.used-by-app-translation"}}
3+
{{t "company.scoped-addon.used-by-app-translation"}}

fixtures/external-addon-translations/node_modules/@company/scoped-addon/templates/application.hbs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/external-addon-translations/node_modules/@company/scoped-addon/translations/en.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/external-addon-translations/node_modules/external-addon/templates/application.hbs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixtures/external-addon-translations/node_modules/external-addon/translations/en.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hbs-translation: HBS!
2+
js-translation: JS!

index.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ async function run(rootDir, options = {}) {
3737

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

40-
let translationFiles = await findTranslationFiles(rootDir);
41-
let existingTranslationKeys = await analyzeTranslationFiles(rootDir, translationFiles);
40+
let ownTranslationFiles = await findOwnTranslationFiles(rootDir);
41+
let addonTranslationFiles = await findAddonTranslationFiles(rootDir);
42+
let existingOwnTranslationKeys = await analyzeTranslationFiles(rootDir, ownTranslationFiles);
43+
let existingAddonTranslationKeys = await analyzeTranslationFiles(rootDir, addonTranslationFiles);
44+
let existingTranslationKeys = mergeMaps(existingOwnTranslationKeys, existingAddonTranslationKeys);
4245
let whitelist = config.whitelist || [];
4346

4447
let unusedTranslations = findDifferenceInTranslations(
45-
existingTranslationKeys,
48+
existingOwnTranslationKeys,
4649
usedTranslationKeys,
4750
whitelist
4851
);
@@ -82,7 +85,7 @@ async function run(rootDir, options = {}) {
8285
let totalErrors = missingTranslations.size + unusedTranslations.size;
8386

8487
if (shouldFix) {
85-
removeUnusedTranslations(writeToFile, rootDir, translationFiles, unusedTranslations);
88+
removeUnusedTranslations(writeToFile, rootDir, ownTranslationFiles, unusedTranslations);
8689
log();
8790
log(' 👏 All unused translations were removed');
8891
}
@@ -113,8 +116,15 @@ async function findInRepoFiles(cwd) {
113116
return globby(joinPaths(inRepoFolders, ['**/*.js', '**/*.hbs', '**/*.emblem']), { cwd });
114117
}
115118

116-
async function findTranslationFiles(cwd) {
117-
let inputFolders = ['', ...findInRepoPaths(cwd)];
119+
async function findOwnTranslationFiles(cwd) {
120+
return findTranslationFiles(cwd, ['', ...findInRepoPaths(cwd)]);
121+
}
122+
123+
async function findAddonTranslationFiles(cwd) {
124+
return findTranslationFiles(cwd, ['node_modules/*', 'node_modules/@*/*']);
125+
}
126+
127+
async function findTranslationFiles(cwd, inputFolders) {
118128
let translationPaths = joinPaths(inputFolders, ['translations']);
119129

120130
return globby(joinPaths(translationPaths, ['**/*.json', '**/*.yaml', '**/*.yml']), {
@@ -390,4 +400,19 @@ function getNestedAttribute(parent, keys) {
390400
return attribute;
391401
}
392402

403+
function mergeMaps(mapA, mapB) {
404+
let resultMap = new Map([...mapA]);
405+
406+
for (let [key, bFiles] of mapB) {
407+
if (!resultMap.has(key)) {
408+
resultMap.set(key, bFiles);
409+
} else {
410+
let aFiles = resultMap.get(key);
411+
resultMap.set(key, new Set([...aFiles, ...bFiles]));
412+
}
413+
}
414+
415+
return resultMap;
416+
}
417+
393418
module.exports = { run, generateFileList };

0 commit comments

Comments
 (0)