Skip to content

Commit

Permalink
Refactored code in lib (#1882)
Browse files Browse the repository at this point in the history
* chore: Removed isLocalizationFramework (remnant from ember-cli-template-lint and ember-i18n)

* refactor: Simplified createOptions()

* refactor: Sorted keys

* refactor: Removed lib/logger

* refactor: Renamed function

* refactor: Removed unreachable code and made an early exit

* refactor: Simplified findEngine()

* chore: Moved test files

* chore: Reordered functions

* refactor: Removed order dependencies

* chore: Sorted require statements

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 committed May 14, 2024
1 parent 61333e0 commit 8fd1d43
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 274 deletions.
6 changes: 6 additions & 0 deletions .changeset/lucky-buttons-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"test-ember-intl-node": patch
"ember-intl": patch
---

Refactored code in lib
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
/* jshint node:true */

module.exports = function (/* environment */) {
return {
/**
* Cause a build error if missing translations are detected.
*
* See https://ember-intl.github.io/ember-intl/docs/guide/missing-translations#throwing-a-build-error-on-missing-when-required-translations
*
* @property errorOnMissingTranslations
* @type {Boolean}
* @default "false"
*/
errorOnMissingTranslations: false,

/**
* Cause a build error if ICU argument mismatches are detected between translations
* with the same key across all locales.
*
* @property errorOnNamedArgumentMismatch
* @type {Boolean}
* @default "false"
*/
errorOnNamedArgumentMismatch: false,

/**
* Merges the fallback locale's translations into all other locales as a
* build-time fallback strategy.
Expand Down Expand Up @@ -41,34 +60,17 @@ module.exports = function (/* environment */) {
publicOnly: false,

/**
* Add the subdirectories of the translations as a namespace for all keys.
*
* @property wrapTranslationsWithNamespace
* @type {Boolean}
* @default "false"
*/
wrapTranslationsWithNamespace: false,

/**
* Cause a build error if ICU argument mismatches are detected between translations
* with the same key across all locales.
*
* @property errorOnNamedArgumentMismatch
* @type {Boolean}
* @default "false"
*/
errorOnNamedArgumentMismatch: false,

/**
* Cause a build error if missing translations are detected.
* A function that is called whenever any translation key, from any locale, is missing at build time.
*
* See https://ember-intl.github.io/ember-intl/docs/guide/missing-translations#throwing-a-build-error-on-missing-when-required-translations
* See https://ember-intl.github.io/ember-intl/docs/guide/missing-translations#requiring-translations
*
* @property errorOnMissingTranslations
* @type {Boolean}
* @default "false"
* @property requiresTranslation
* @type {Function}
* @default "function(key,locale) { return true }"
*/
errorOnMissingTranslations: false,
requiresTranslation(/* key, locale */) {
return true;
},

/**
* Removes empty translations from the build output.
Expand All @@ -80,16 +82,12 @@ module.exports = function (/* environment */) {
stripEmptyTranslations: false,

/**
* A function that is called whenever any translation key, from any locale, is missing at build time.
*
* See https://ember-intl.github.io/ember-intl/docs/guide/missing-translations#requiring-translations
* Add the subdirectories of the translations as a namespace for all keys.
*
* @property requiresTranslation
* @type {Function}
* @default "function(key,locale) { return true }"
* @property wrapTranslationsWithNamespace
* @type {Boolean}
* @default "false"
*/
requiresTranslation(/* key, locale */) {
return true;
},
wrapTranslationsWithNamespace: true,
};
};
139 changes: 51 additions & 88 deletions packages/ember-intl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,30 @@
'use strict';

const { existsSync } = require('node:fs');
const { dirname, isAbsolute, join } = require('node:path');
const { dirname, join } = require('node:path');
const mergeTrees = require('broccoli-merge-trees');
const calculateCacheKeyForTree = require('calculate-cache-key-for-tree');

const buildTranslationTree = require('./lib/broccoli/build-translation-tree');
const TranslationReducer = require('./lib/broccoli/translation-reducer');
const defaultConfig = require('./lib/default-config');
const findEngine = require('./lib/utils/find-engine');
const Logger = require('./lib/logger');
const DEFAULT_CONFIG = require('./lib/default-config');

const OBSOLETE_OPTIONS = ['locales', 'disablePolyfill', 'autoPolyfill'];

module.exports = {
name: 'ember-intl',
logger: null,
configOptions: null,
isLocalizationFramework: true,

included(parent) {
this._super.included.apply(this, arguments);

this.app = this._findHost();
const options = this.app.options.intl || {};

this.logger = new Logger({
ui: this.ui,
silent: options.silent,
});
this.package = findEngine(parent) ?? this.project;

this.package = findEngine(parent) || this.project;
this.configOptions = this.createOptions(this.app.env, this.app.project);
this.configOptions = {
...defaultConfig,
...this.getUserConfig(),
};
},

cacheKeyForTree(treeType) {
Expand All @@ -46,54 +39,13 @@ module.exports = {
);
},

generateTranslationTree(options = {}) {
const {
outputPath,
fallbackLocale,
includeLocales,
excludeLocales,
requiresTranslation,
errorOnMissingTranslations,
errorOnNamedArgumentMismatch,
stripEmptyTranslations,
wrapTranslationsWithNamespace,
} = this.configOptions;

const [translationTree, addonsWithTranslations] = buildTranslationTree(
this.project,
this.configOptions.inputPath,
this.treeGenerator,
);

return new TranslationReducer([translationTree], {
fallbackLocale,
includeLocales,
excludeLocales,
requiresTranslation,
errorOnMissingTranslations,
errorOnNamedArgumentMismatch,
stripEmptyTranslations,
wrapTranslationsWithNamespace,
verbose: !this.isSilent,
outputPath: 'outputPath' in options ? options.outputPath : outputPath,
addonsWithTranslations,
mergeTranslationFiles: options.mergeTranslationFiles,
log: (...args) => {
return this.logger.log(...args);
},
warn: (...args) => {
return this.logger.warn(...args);
},
});
},

treeForAddon(tree) {
let trees = [tree];

if (!this.configOptions.publicOnly) {
const translationTree = this.generateTranslationTree({
outputPath: '',
const translationTree = this.getTranslationTree({
mergeTranslationFiles: true,
outputPath: '',
});

trees.push(translationTree);
Expand All @@ -109,48 +61,59 @@ module.exports = {
let trees = [];

if (this.configOptions.publicOnly) {
trees.push(this.generateTranslationTree());
trees.push(this.getTranslationTree());
}

return mergeTrees(trees, { overwrite: true });
},

readConfig(environment, project) {
// NOTE: For ember-cli >= 2.6.0-beta.3, project.configPath() returns absolute path
// while older ember-cli versions return path relative to project root
let configPath = dirname(project.configPath());
let config = join(configPath, 'ember-intl.js');

if (!isAbsolute(config)) {
config = join(project.root, config);
}
getTranslationTree(options = {}) {
const {
errorOnMissingTranslations,
errorOnNamedArgumentMismatch,
excludeLocales,
fallbackLocale,
includeLocales,
outputPath,
requiresTranslation,
stripEmptyTranslations,
wrapTranslationsWithNamespace,
} = this.configOptions;

if (existsSync(config)) {
return require(config)(environment);
}
const [translationTree, addonsWithTranslations] = buildTranslationTree(
this.project,
this.configOptions.inputPath,
this.treeGenerator,
);

return {};
return new TranslationReducer([translationTree], {
addonsWithTranslations,
errorOnMissingTranslations,
errorOnNamedArgumentMismatch,
excludeLocales,
fallbackLocale,
includeLocales,
log: (message) => {
return this.ui.writeLine(`[ember-intl] ${message}`);
},
mergeTranslationFiles: options.mergeTranslationFiles,
outputPath: options.outputPath ?? outputPath,
requiresTranslation,
stripEmptyTranslations,
verbose: !this.isSilent,
wrapTranslationsWithNamespace,
});
},

createOptions(environment, project) {
const config = {
...DEFAULT_CONFIG,
...this.readConfig(environment, project),
};
getUserConfig() {
const { env: environment, project } = this.app;

if (typeof config.requiresTranslation !== 'function') {
this.logger.warn(
'Configured `requiresTranslation` is not a function. Using default implementation.',
);
config.requiresTranslation = DEFAULT_CONFIG.requiresTranslation;
}
const config = join(dirname(project.configPath()), 'ember-intl.js');

OBSOLETE_OPTIONS.filter((option) => option in config).forEach((option) => {
this.logger.warn(
`\`${option}\` is obsolete and can be removed from config/ember-intl.js.`,
);
});
if (!existsSync(config)) {
return {};
}

return config;
return require(config)(environment);
},
};

0 comments on commit 8fd1d43

Please sign in to comment.