Permalink
Browse files

Adding publicOnly as a build config option, writes translations to di…

…st folder and not into the application
  • Loading branch information...
1 parent e4dde11 commit 93155bde13d42d0a60b4dd8cb90d75e2c20edf6d @jasonmit committed Jan 26, 2016
View
@@ -0,0 +1 @@
+progress=false
@@ -26,11 +26,19 @@ var TranslationPreprocessor = require('./lib/broccoli/translation-preprocessor')
function generateOptions(app) {
var addonConfig = app.project.config(app.env)['intl'] || {};
+ if (addonConfig.defaultLocale) {
+ console.warn('[ember-intl] DEPRECATION: intl.defaultLocale is deprecated in favor of intl.baseLocale');
+ console.warn('[ember-intl] Please update config/environment.js')
+
+ addonConfig.baseLocale = addonConfig.defaultLocale;
+ }
+
var options = utils.assign({
locales: undefined,
- defaultLocale: undefined,
+ baseLocale: undefined,
allowEmpty: true,
disablePolyfill: false,
+ publicOnly: false,
inputPath: 'translations',
outputPath: 'translations'
}, addonConfig);
@@ -56,21 +64,21 @@ module.exports = {
this.app = app;
}
- this.addonOptions = generateOptions(app);
- this.hasTranslationDir = existsSync(this.project.root + '/' + this.addonOptions.inputPath);
- this.locales = this._discoverLocales();
+ this.opts = generateOptions(app);
+ this.hasTranslationDir = existsSync(this.project.root + '/' + this.opts.inputPath);
+ this.locales = this.knownLocales(this.opts);
this.trees = {
- translations: new WatchedDir(this.addonOptions.inputPath),
+ translations: new WatchedDir(this.opts.inputPath),
intl: new UnwatchedDir(path.dirname(require.resolve('intl')))
};
},
treeForApp: function(tree) {
var trees = [tree];
- if (this.hasTranslationDir) {
- trees.push(new TranslationPreprocessor(this.trees.translations, this.addonOptions));
+ if (this.hasTranslationDir && !this.opts.publicOnly) {
+ trees.push(new TranslationPreprocessor(this.trees.translations, this.opts));
}
if (tree && this.locales.length) {
@@ -92,61 +100,64 @@ module.exports = {
treeForPublic: function() {
var publicTree = this._super.treeForPublic.apply(this, arguments);
- if (this.addonOptions.disablePolyfill) {
- return publicTree;
- }
-
- var assetPath = 'assets/intl';
- var appOptions = this.app.options;
var trees = [];
- if (appOptions.app && appOptions.app.intl) {
- assetPath = appOptions.app.intl;
- }
-
if (publicTree) {
trees.push(publicTree);
}
- trees.push(new Funnel(this.trees.intl, {
- srcDir: 'dist',
- files: ['Intl.js.map'],
- destDir: assetPath
- }));
-
- trees.push(lowercaseTree(new Funnel(this.trees.intl, {
- srcDir: 'dist',
- files: ['Intl.complete.js', 'Intl.js', 'Intl.min.js'],
- destDir: assetPath
- })));
-
- var localeFunnel = {
- srcDir: 'locale-data/jsonp',
- destDir: assetPath + '/locales'
- };
+ if (!this.opts.disablePolyfill) {
+ var assetPath = 'assets/intl';
+ var appOptions = this.app.options;
+
+ if (appOptions.app && appOptions.app.intl) {
+ assetPath = appOptions.app.intl;
+ }
+
+ trees.push(new Funnel(this.trees.intl, {
+ srcDir: 'dist',
+ files: ['Intl.js.map'],
+ destDir: assetPath
+ }));
- if (this.locales.length) {
- localeFunnel.include = this.locales.map(function(locale) {
- return new RegExp(locale, 'i');
- });
+ trees.push(lowercaseTree(new Funnel(this.trees.intl, {
+ srcDir: 'dist',
+ files: ['Intl.complete.js', 'Intl.js', 'Intl.min.js'],
+ destDir: assetPath
+ })));
+
+ var localeFunnel = {
+ srcDir: 'locale-data/jsonp',
+ destDir: assetPath + '/locales'
+ };
+
+ if (this.locales.length) {
+ localeFunnel.include = this.locales.map(function(locale) {
+ return new RegExp(locale, 'i');
+ });
+ }
+
+ trees.push(lowercaseTree(new Funnel(this.trees.intl, localeFunnel)));
}
- trees.push(lowercaseTree(new Funnel(this.trees.intl, localeFunnel)));
+ if (this.hasTranslationDir && this.opts.publicOnly) {
+ trees.push(new TranslationPreprocessor(this.trees.translations, this.opts));
+ }
return mergeTrees(trees, { overwrite: true });
},
- _discoverLocales: function() {
+ knownLocales: function(options) {
var locales = [];
if (this.hasTranslationDir) {
- locales = walkSync(this.project.root + '/' + this.addonOptions.inputPath).map(function(filename) {
+ locales = walkSync(this.project.root + '/' + options.inputPath).map(function(filename) {
return path.basename(filename, path.extname(filename));
}).filter(utils.isSupportedLocale);
}
- if (this.addonOptions.locales) {
- locales = locales.concat(this.addonOptions.locales);
+ if (options.locales) {
+ locales = locales.concat(options.locales);
}
return utils.uniqueByString(locales);
@@ -129,26 +129,26 @@ TranslationPreprocessor.prototype.build = function() {
mkdirp.sync(outputPath);
- if (this.options.defaultLocale) {
- var defaultTranslationPath = glob.sync(inputPath + '/' + this.options.defaultLocale + '\.@(json|yaml|yml)', {
+ if (this.options.baseLocale) {
+ var defaultTranslationPath = glob.sync(inputPath + '/' + this.options.baseLocale + '\.@(json|yaml|yml)', {
nosort: true,
silent: true
})[0];
if (!defaultTranslationPath) {
- console.log(chalk.yellow('ember-intl: "' + this.options.defaultLocale + '" default locale missing `translations` folder'));
+ console.log(chalk.yellow('ember-intl: "' + this.options.baseLocale + '" default locale missing `translations` folder'));
return;
}
- defaultTranslation = translations[this.options.defaultLocale];
+ defaultTranslation = translations[this.options.baseLocale];
defaultTranslationKeys = propKeys(defaultTranslation);
}
for (var key in translations) {
if (translations.hasOwnProperty(key)) {
translation = translations[key];
- if (this.options.defaultLocale) {
+ if (this.options.baseLocale) {
missedKeys(translation, defaultTranslationKeys, key);
}
@@ -14,7 +14,8 @@ module.exports = function(environment) {
intl: {
locales: ['en-us', 'es-es', 'fr-fr', 'de-de', 'aa-dj'],
- defaultLocale: 'en-us',
+ baseLocale: 'en-us',
+ publicOnly: false,
disablePolyfill: false,
allowEmpty: true,
outputPath: 'translations',
@@ -11,7 +11,7 @@ import { runAppend, runDestroy } from '../../helpers/run-append';
import createRenderer from '../../helpers/create-intl-block';
const date = 1390518044403;
-const defaultLocale = 'en-us';
+const locale = 'en-us';
let view;
@@ -35,42 +35,42 @@ test('invoke the formatDate directly', function(assert) {
const service = this.container.lookup('service:intl');
assert.equal(service.formatDate(date, {
timeZone: 'UTC',
- locale: defaultLocale
+ locale: locale
}), '1/23/2014');
});
test('should render empty string for a null value', function(assert) {
assert.expect(1);
- view = this.render(hbs`{{format-date null}}`, defaultLocale);
+ view = this.render(hbs`{{format-date null}}`, locale);
runAppend(view);
assert.equal(view.$().text(), '');
});
test('should render empty string for an empty string value', function(assert) {
assert.expect(1);
- view = this.render(hbs`{{format-date ''}}`, defaultLocale);
+ view = this.render(hbs`{{format-date ''}}`, locale);
runAppend(view);
assert.equal(view.$().text(), '');
});
test('should render empty string for an undefined value', function(assert) {
assert.expect(1);
- view = this.render(hbs`{{format-date undefined}}`, defaultLocale);
+ view = this.render(hbs`{{format-date undefined}}`, locale);
runAppend(view);
assert.equal(view.$().text(), '');
});
test('should render epoch date for a null value when allow empty is false', function(assert) {
assert.expect(1);
- view = this.render(hbs`{{format-date null allowEmpty=false}}`, defaultLocale);
+ view = this.render(hbs`{{format-date null allowEmpty=false}}`, locale);
runAppend(view);
- assert.equal(view.$().text(), new Intl.DateTimeFormat(defaultLocale).format(0));
+ assert.equal(view.$().text(), new Intl.DateTimeFormat(locale).format(0));
});
test('it should return a formatted string from a date string', function(assert) {
assert.expect(1);
// Must provide `timeZone` because: https://github.com/yahoo/ember-intl/issues/21
- view = this.render(hbs`{{format-date date timeZone='UTC'}}`, defaultLocale);
+ view = this.render(hbs`{{format-date date timeZone='UTC'}}`, locale);
view.set('context', { date: date });
runAppend(view);
assert.equal(view.$().text(), '1/23/2014');
@@ -79,23 +79,23 @@ test('it should return a formatted string from a date string', function(assert)
test('it should return a formatted string from a timestamp', function(assert) {
assert.expect(1);
// Must provide `timeZone` because: https://github.com/yahoo/ember-intl/issues/21
- view = this.render(hbs`{{format-date date timeZone='UTC'}}`, defaultLocale);
+ view = this.render(hbs`{{format-date date timeZone='UTC'}}`, locale);
view.set('context', { date: date });
runAppend(view);
assert.equal(view.$().text(), '1/23/2014');
});
test('it should return a formatted string of just the date', function(assert) {
assert.expect(1);
- view = this.render(hbs`{{format-date date hour='numeric' minute='numeric' timeZone='UTC'}}`, defaultLocale);
+ view = this.render(hbs`{{format-date date hour='numeric' minute='numeric' timeZone='UTC'}}`, locale);
view.set('context', { date: date });
runAppend(view);
assert.equal(view.$().text(), '11:00 PM');
});
test('it should format the epoch timestamp', function(assert) {
assert.expect(1);
- view = this.render(hbs`{{format-date 0}}`, defaultLocale);
+ view = this.render(hbs`{{format-date 0}}`, locale);
runAppend(view);
- assert.equal(view.$().text(), new Intl.DateTimeFormat(defaultLocale).format(0));
+ assert.equal(view.$().text(), new Intl.DateTimeFormat(locale).format(0));
});

0 comments on commit 93155bd

Please sign in to comment.