-
Notifications
You must be signed in to change notification settings - Fork 250
destructure Ember.I18n each time render is called #436
base: master
Are you sure you want to change the base?
Conversation
Do not destructure before exporting. Fixes using `ember-i18n`. Otherwise the workaround [described here](https://github.com/DockYard/ember-validations/issues/366#issuecomment-169869004) using `ember-i18n` does not work. When running multiple tests, ember-validations will use a destroyed version of `i18n` from the 1st test run.
👍 |
Actually, let me back up. I might be following this incorrectly. So It might make more sense to inject the service instead rather than destructure from the |
Yes injecting the service would be better. We have a hack in an instance-initializer to set |
In older versions of ember-i18n, there was a fixed global, Later, ember-i18n switched to @iezer referenced a comment I made a while back on how to fake the old interface from the new (to support this library with newer ember-i18n), but that relies on setting up and tearing down |
It would be lovely if this library were to rely on For example, // ember-validations/app/services/validation-messages.js
import Ember from 'ember';
import getOwner from 'ember-getowner-polyfill';
export default Ember.Service.extend({
// i18n: Ember.inject.service(), // can't do this because service:i18n might not exist
render(attribute, context) {
const i18n = getOwner(this).lookup('service:i18n');
if (i18n) { return i18n.t(`errors.${attribute}`, context);
...
}
}); That, in turn, means that the various validators have to change to get the // ember-validations/addon/validators/local/numericality.js
import Ember from 'ember';
export default Base.extend({
validationMessages: Ember.inject.service(),
init() {
...
this.options.messages.numericality = this.get('validationMessages').render('notANumber', this.options);
}
}); |
confirm, I have been planing a rewrite for 2.0. I may reclassify this as a defunct 2.x attempt (we never left alpha for 2.x) and that would be a better place for hooking into i18n correctly. Odds are I would just leave it out of this library and up to the consumer to enable. |
How do you feel about shipping this in the meantime? |
Fixes using
ember-i18n
.Otherwise the workaround described in https://github.com/DockYard/ember-validations/issues/366#issuecomment-169869004 no longer works. When running multiple tests, ember-validations will use a destroyed version of
i18n
from the 1st test run, leaking memory in the process. CC @jamesarosen