From a83ec97b3e2e87a3398eed17c000d26eb7a52c0b Mon Sep 17 00:00:00 2001 From: Wojciech Zielinski Date: Wed, 9 Oct 2013 11:22:54 +0200 Subject: [PATCH 1/5] added configurable variable to assign translations to --- lib/ember-i18n-rails.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ember-i18n-rails.rb b/lib/ember-i18n-rails.rb index bf766da..e2d09aa 100644 --- a/lib/ember-i18n-rails.rb +++ b/lib/ember-i18n-rails.rb @@ -116,8 +116,11 @@ def save(translations, file) file = Rails.root.join(file) FileUtils.mkdir_p File.dirname(file) + variable_to_assign = config.fetch(:variable, "Em.I18n.translations") + File.open(file, "w+") do |f| - f << 'Em.I18n.translations = ' + f << variable_to_assign + f << ' = ' f << JSON.pretty_generate(translations).html_safe f << ';' end From 78306154be18e181290a13f645b06305dff1b236 Mon Sep 17 00:00:00 2001 From: Jim Date: Fri, 9 May 2014 10:53:24 -0400 Subject: [PATCH 2/5] fix has_asset_pipeline? for rails 4 --- lib/ember-i18n-rails.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ember-i18n-rails.rb b/lib/ember-i18n-rails.rb index 2942b46..5b175c8 100644 --- a/lib/ember-i18n-rails.rb +++ b/lib/ember-i18n-rails.rb @@ -17,7 +17,7 @@ def assert_usable_configuration! end def has_asset_pipeline? - ::Rails.configuration.respond_to?(:assets) && ::Rails.configuration.assets.enabled + ::Rails.configuration.respond_to?(:assets) && (::Rails.configuration.assets.enabled || ::Rails.configuration.assets[:compile]) end def config_file From c54a2fd2e922cd7afd392e0e07a1425c2778b738 Mon Sep 17 00:00:00 2001 From: Josemar Luedke Date: Wed, 25 Jun 2014 12:29:55 +0200 Subject: [PATCH 3/5] Update ember-i18n to v2.0.0 --- vendor/assets/javascripts/ember-i18n.js | 74 ++++++++++--------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/vendor/assets/javascripts/ember-i18n.js b/vendor/assets/javascripts/ember-i18n.js index 6130bcd..abd33a2 100644 --- a/vendor/assets/javascripts/ember-i18n.js +++ b/vendor/assets/javascripts/ember-i18n.js @@ -2,17 +2,20 @@ (function(window) { var I18n, assert, findTemplate, get, set, isBinding, lookupKey, pluralForm, - keyExists, runAfterRender, compileTemplate; + PlainHandlebars, EmHandlebars, keyExists, compileTemplate; - get = Ember.Handlebars.get || Ember.Handlebars.getPath || Ember.getPath; + PlainHandlebars = window.Handlebars; + EmHandlebars = Ember.Handlebars; + get = EmHandlebars.get; set = Ember.set; + assert = Ember.assert; function warn(msg) { Ember.Logger.warn(msg); } if (typeof CLDR !== "undefined" && CLDR !== null) pluralForm = CLDR.pluralForm; if (pluralForm == null) { - warn("CLDR.pluralForm not found. Em.I18n will not support count-based inflection."); + warn("CLDR.pluralForm not found. Ember.I18n will not support count-based inflection."); } lookupKey = function(key, hash) { @@ -28,8 +31,6 @@ } }; - assert = Ember.assert != null ? Ember.assert : window.ember_assert; - findTemplate = function(key, setOnMissing) { assert("You must provide a translation key string, not %@".fmt(key), typeof key === 'string'); var result = lookupKey(key, I18n.translations); @@ -55,19 +56,6 @@ return translation != null && !translation._isMissing; }; - runAfterRender = (function() { - if (Em.run.queues.indexOf('afterRender') === -1) { - // Ember 0.9 doesn't have an afterRender queue. - return function runAfterRender(callback) { - return Em.run.once(callback); - }; - } - - return function runAfterRender(callback) { - return Em.run.scheduleOnce('afterRender', callback); - }; - }()); - function eachTranslatedAttribute(object, fn) { var isTranslatedAttribute = /(.+)Translation$/, isTranslatedAttributeMatch; @@ -81,13 +69,13 @@ } compileTemplate = (function() { - if (typeof Handlebars.compile === 'function') { + if (typeof PlainHandlebars.compile === 'function') { return function compileWithHandlebars(template) { - return Handlebars.compile(template); + return PlainHandlebars.compile(template); }; } else { - return function cannotCompileTemplate(template) { - error('The default Ember.I18n.compile function requires the full Handlebars. Either include the full Handlebars or override Ember.I18n.compile.'); + return function cannotCompileTemplate() { + throw new Ember.Error('The default Ember.I18n.compile function requires the full Handlebars. Either include the full Handlebars or override Ember.I18n.compile.'); }; } }()); @@ -123,7 +111,7 @@ exists: keyExists, - TranslateableProperties: Em.Mixin.create({ + TranslateableProperties: Ember.Mixin.create({ init: function() { var result = this._super.apply(this, arguments); eachTranslatedAttribute(this, function(attribute, translation) { @@ -137,7 +125,7 @@ } }), - TranslateableAttributes: Em.Mixin.create({ + TranslateableAttributes: Ember.Mixin.create({ didInsertElement: function() { var result = this._super.apply(this, arguments); eachTranslatedAttribute(this, function(attribute, translation) { @@ -152,17 +140,11 @@ isBinding = /(.+)Binding$/; - // CRUFT: in v2, which requires Ember 1.0+, Ember.uuid will always be - // available, so this function can be cleaned up. - var uniqueElementId = (function(){ - var id = Ember.uuid || 0; - return function() { - var elementId = 'i18n-' + id++; - return elementId; - }; - })(); - - Handlebars.registerHelper('t', function(key, options) { + function uniqueElementId() { + return ++Ember.uuid; + } + + EmHandlebars.registerHelper('t', function(key, options) { var attrs, context, data, elementID, result, tagName, view; context = this; attrs = options.hash; @@ -172,7 +154,7 @@ delete attrs.tagName; elementID = uniqueElementId(); - Em.keys(attrs).forEach(function(property) { + Ember.keys(attrs).forEach(function(property) { var bindPath, currentValue, invoker, isBindingMatch, normalized, normalizedPath, observer, propertyName, root, _ref; isBindingMatch = property.match(isBinding); @@ -182,13 +164,13 @@ currentValue = get(context, bindPath, options); attrs[propertyName] = currentValue; invoker = null; - normalized = Ember.Handlebars.normalizePath(context, bindPath, data); + normalized = EmHandlebars.normalizePath(context, bindPath, data); _ref = [normalized.root, normalized.path], root = _ref[0], normalizedPath = _ref[1]; observer = function() { var elem, newValue; - if (view.get('state') !== 'inDOM') { - Em.removeObserver(root, normalizedPath, invoker); + if (view.$() == null) { + Ember.removeObserver(root, normalizedPath, invoker); return; } newValue = get(context, bindPath, options); @@ -198,15 +180,15 @@ }; invoker = function() { - return runAfterRender(observer); + Ember.run.scheduleOnce('afterRender', observer); }; - return Em.addObserver(root, normalizedPath, invoker); + return Ember.addObserver(root, normalizedPath, invoker); } }); result = '<%@ id="%@">%@'.fmt(tagName, elementID, I18n.t(key, attrs), tagName); - return new Handlebars.SafeString(result); + return new EmHandlebars.SafeString(result); }); var attrHelperFunction = function(options) { @@ -214,16 +196,16 @@ attrs = options.hash; result = []; - Em.keys(attrs).forEach(function(property) { + Ember.keys(attrs).forEach(function(property) { var translatedValue; translatedValue = I18n.t(attrs[property]); return result.push('%@="%@"'.fmt(property, translatedValue)); }); - return new Handlebars.SafeString(result.join(' ')); + return new EmHandlebars.SafeString(result.join(' ')); }; - Handlebars.registerHelper('translateAttr', attrHelperFunction); - Handlebars.registerHelper('ta', attrHelperFunction); + EmHandlebars.registerHelper('translateAttr', attrHelperFunction); + EmHandlebars.registerHelper('ta', attrHelperFunction); }).call(undefined, this); From abd7743499485909d8e56616df8587437b3ba569 Mon Sep 17 00:00:00 2001 From: Jim Durand Date: Sun, 7 Sep 2014 23:24:54 -0400 Subject: [PATCH 4/5] updated ember-i18n to v2.2.1 --- vendor/assets/javascripts/ember-i18n.js | 139 ++++++++++++++++-------- 1 file changed, 95 insertions(+), 44 deletions(-) diff --git a/vendor/assets/javascripts/ember-i18n.js b/vendor/assets/javascripts/ember-i18n.js index abd33a2..5ad058f 100644 --- a/vendor/assets/javascripts/ember-i18n.js +++ b/vendor/assets/javascripts/ember-i18n.js @@ -2,7 +2,8 @@ (function(window) { var I18n, assert, findTemplate, get, set, isBinding, lookupKey, pluralForm, - PlainHandlebars, EmHandlebars, keyExists, compileTemplate; + PlainHandlebars, EmHandlebars, keyExists, + compileTemplate, compileWithHandlebars; PlainHandlebars = window.Handlebars; EmHandlebars = Ember.Handlebars; @@ -39,8 +40,7 @@ if (result == null) { result = I18n.translations[key] = function() { return "Missing translation: " + key; }; result._isMissing = true; - warn("Missing translation: " + key); - I18n[(typeof I18n.trigger === 'function' ? 'trigger' : 'fire')]('missing', key); //Support 0.9 style .fire + I18n.trigger('missing', key); } } @@ -68,7 +68,11 @@ } } - compileTemplate = (function() { + compileWithHandlebars = (function() { + if (Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS === undefined) { + warn("Ember.I18n will no longer include Handlebars compilation by default in the future; instead, it will supply its own default compiler. Set Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS to true to opt-in now."); + } + if (typeof PlainHandlebars.compile === 'function') { return function compileWithHandlebars(template) { return PlainHandlebars.compile(template); @@ -80,6 +84,20 @@ } }()); + function compileWithoutHandlebars(template) { + return function (data) { + return template.replace(/\{\{(.*?)\}\}/g, function(i, match) { + return data[match]; + }); + }; + } + + if (Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS === true) { + compileTemplate = compileWithoutHandlebars; + } else { + compileTemplate = compileWithHandlebars; + } + I18n = Ember.Evented.apply({ compile: compileTemplate, @@ -144,51 +162,84 @@ return ++Ember.uuid; } + var TranslationView = Ember._MetamorphView.extend({ + + translationKey: null, + + wrappingTagName: Ember.computed(function(propertyName, newValue) { + if (arguments.length > 1 && newValue != null) { return newValue; } + + var useSpanByDefault; + + if (Ember.FEATURES.hasOwnProperty('I18N_TRANSLATE_HELPER_SPAN')) { + useSpanByDefault = Ember.FEATURES.I18N_TRANSLATE_HELPER_SPAN; + } else { + Ember.deprecate('The {{t}} helper will no longer use a tag in future versions of Ember.I18n. Set Ember.FEATURES.I18N_TRANSLATE_HELPER_SPAN to false to quiet these warnings and maintain older behavior.'); + useSpanByDefault = true; + } + + return useSpanByDefault ? 'span' : null; + }), + + render: function(buffer) { + var wrappingTagName = this.get('wrappingTagName'); + var text = Ember.I18n.t(this.get('translationKey'), this.get('context')); + + if (wrappingTagName) { buffer.push('<' + wrappingTagName + ' id="' + uniqueElementId() + '">'); } + buffer.push(text); + if (wrappingTagName) { buffer.push(''); } + } + + }); + EmHandlebars.registerHelper('t', function(key, options) { - var attrs, context, data, elementID, result, tagName, view; - context = this; - attrs = options.hash; - data = options.data; - view = data.view; - tagName = attrs.tagName || 'span'; + var context = this; + var data = options.data; + var attrs = options.hash; + var tagName = attrs.tagName; delete attrs.tagName; - elementID = uniqueElementId(); + + if (options.types[0] !== 'STRING') { + warn("Ember.I18n t helper called with unquoted key: %@. In the future, this will be treated as a bound property, not a string literal.".fmt(key)); + } + + var translationView = TranslationView.create({ + context: attrs, + translationKey: key, + wrappingTagName: tagName + }); Ember.keys(attrs).forEach(function(property) { - var bindPath, currentValue, invoker, isBindingMatch, normalized, normalizedPath, observer, propertyName, root, _ref; - isBindingMatch = property.match(isBinding); - - if (isBindingMatch) { - propertyName = isBindingMatch[1]; - bindPath = attrs[property]; - currentValue = get(context, bindPath, options); - attrs[propertyName] = currentValue; - invoker = null; - normalized = EmHandlebars.normalizePath(context, bindPath, data); - _ref = [normalized.root, normalized.path], root = _ref[0], normalizedPath = _ref[1]; - - observer = function() { - var elem, newValue; - if (view.$() == null) { - Ember.removeObserver(root, normalizedPath, invoker); - return; - } - newValue = get(context, bindPath, options); - elem = view.$("#" + elementID); - attrs[propertyName] = newValue; - return elem.html(I18n.t(key, attrs)); - }; - - invoker = function() { - Ember.run.scheduleOnce('afterRender', observer); - }; - - return Ember.addObserver(root, normalizedPath, invoker); - } + var isBindingMatch = property.match(isBinding); + if (!isBindingMatch) { return; } + + var propertyName = isBindingMatch[1]; + var bindPath = attrs[property]; + var currentValue = get(context, bindPath, options); + + attrs[propertyName] = currentValue; + + var invoker = null; + var normalized = EmHandlebars.normalizePath(context, bindPath, data); + var _ref = [normalized.root, normalized.path], root = _ref[0], normalizedPath = _ref[1]; + + var observer = function() { + if (translationView.$() == null) { + Ember.removeObserver(root, normalizedPath, invoker); + return; + } + attrs[propertyName] = get(context, bindPath, options); + translationView.rerender(); + }; + + invoker = function() { + Ember.run.scheduleOnce('afterRender', observer); + }; + + return Ember.addObserver(root, normalizedPath, invoker); }); - result = '<%@ id="%@">%@'.fmt(tagName, elementID, I18n.t(key, attrs), tagName); - return new EmHandlebars.SafeString(result); + data.view.appendChild(translationView); }); var attrHelperFunction = function(options) { @@ -208,4 +259,4 @@ EmHandlebars.registerHelper('translateAttr', attrHelperFunction); EmHandlebars.registerHelper('ta', attrHelperFunction); -}).call(undefined, this); +}).call(undefined, this); \ No newline at end of file From 932d0a9e9644ea5eaef13dd59a536b748c128743 Mon Sep 17 00:00:00 2001 From: Jim Durand Date: Mon, 13 Oct 2014 20:43:44 -0400 Subject: [PATCH 5/5] bump ember-i18n to v2.2.2 --- vendor/assets/javascripts/ember-i18n.js | 56 +++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/vendor/assets/javascripts/ember-i18n.js b/vendor/assets/javascripts/ember-i18n.js index 5ad058f..7ae8554 100644 --- a/vendor/assets/javascripts/ember-i18n.js +++ b/vendor/assets/javascripts/ember-i18n.js @@ -1,9 +1,6 @@ -//= require cldr-1.0.0 - (function(window) { var I18n, assert, findTemplate, get, set, isBinding, lookupKey, pluralForm, - PlainHandlebars, EmHandlebars, keyExists, - compileTemplate, compileWithHandlebars; + PlainHandlebars, EmHandlebars, keyExists; PlainHandlebars = window.Handlebars; EmHandlebars = Ember.Handlebars; @@ -38,7 +35,7 @@ if (setOnMissing) { if (result == null) { - result = I18n.translations[key] = function() { return "Missing translation: " + key; }; + result = I18n.translations[key] = function() { return I18n.missingMessage(key); }; result._isMissing = true; I18n.trigger('missing', key); } @@ -68,8 +65,30 @@ } } - compileWithHandlebars = (function() { - if (Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS === undefined) { + var compileImplementation; + + function compileTemplate(template) { + if (compileImplementation === undefined) { + compileImplementation = selectCompileImplementation(); + } + + return compileImplementation(template); + } + + function selectCompileImplementation() { + var flag = Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS; + + if (flag === true) { + return function compileWithoutHandlebars(template) { + return function (data) { + return template.replace(/\{\{(.*?)\}\}/g, function(i, match) { + return data[match]; + }); + }; + }; + } + + if (flag === undefined) { warn("Ember.I18n will no longer include Handlebars compilation by default in the future; instead, it will supply its own default compiler. Set Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS to true to opt-in now."); } @@ -82,20 +101,6 @@ throw new Ember.Error('The default Ember.I18n.compile function requires the full Handlebars. Either include the full Handlebars or override Ember.I18n.compile.'); }; } - }()); - - function compileWithoutHandlebars(template) { - return function (data) { - return template.replace(/\{\{(.*?)\}\}/g, function(i, match) { - return data[match]; - }); - }; - } - - if (Ember.ENV.I18N_COMPILE_WITHOUT_HANDLEBARS === true) { - compileTemplate = compileWithoutHandlebars; - } else { - compileTemplate = compileWithHandlebars; } I18n = Ember.Evented.apply({ @@ -129,6 +134,10 @@ exists: keyExists, + missingMessage: function(key) { + return "Missing translation: " + key; + }, + TranslateableProperties: Ember.Mixin.create({ init: function() { var result = this._super.apply(this, arguments); @@ -158,8 +167,11 @@ isBinding = /(.+)Binding$/; + // Generate a universally unique id + var _uuid = 0; function uniqueElementId() { - return ++Ember.uuid; + var i = ++_uuid; + return 'i18n-' + i; } var TranslationView = Ember._MetamorphView.extend({