Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

[Feature] Allow to pass an array of fallback translations #230

Closed
wants to merge 10 commits into from

Conversation

cibernox
Copy link
Collaborator

I'm need to create a helper that translates error messages following the same conventions that rails-i18n uses for translate error messages in active record.

For those not familiar with rails-i18n, the fallback chain that it uses is like this (being Admin a subclass of User)

activerecord.errors.models.admin.attributes.name.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

While this is specific of my in progress meta-addon (addon built on top of ember-i18n 💥 ), the ability of having fallback translations is a must for me and I think general purpose feature.

ATM i need to compare if the returned string matches the regular expression /Missing translation/ or alternatively use the private API i18n._locale.

The downside of this change is that fallbacks becomes keyword-ish. I'm open to other ideas about how to implement this.

@jamesarosen
Copy link
Owner

ATM i need to compare if the returned string matches the regular expression /Missing translation/ or alternatively use the private API i18n._locale.

We mark missing translations with _isMissing = true. The service:i18n doesn't expose a way to get the compiled translation at the moment, but it could.

@@ -19,8 +19,20 @@ export default Ember.Object.extend(Ember.Evented, {
t: function(key, data = {}) {
const locale = this.get('_locale');
Ember.assert("I18n: Cannot translate when locale is null", locale);
const fallbacks = get(data, 'fallbacks');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic below will be a little cleaner if you do

const fallbacks = getWithDefault(data, 'fallbacks', []);

@cibernox
Copy link
Collaborator Author

@jamesarosen Mixin the main key with the fallbacks in a single array and let Locale#getCompiledTemplate do it's job made a lot of sense.

The point am not 100% convinced yet is the fallbacks being specified as a fallbacks option in the hash. Maybe it makes more sense to allow the key itself to be an array. What do you think?

@cibernox
Copy link
Collaborator Author

Updated this PR to almost mimic 1:1 rails-i18n behaviour (see http://guides.rubyonrails.org/i18n.html#defaults)

The array of fallbacks is received in the options hash and the name of the key with the fallback translations is named default.
Like rails, that key can contain a single default value or an array of them that are applied in order.

Examples:

// Transltions: { "puppies.cat": "kitty", "puppies.dog": "cub" } 
i18n.t('puppies.tiger', { default: 'puppies.cat' }) // => "kitty"
i18n.t('puppies.wolf', { default: ['puppies.fox', 'puppies.dog'] }) // => "cub"
i18n.t('puppies.ferret', { default: ['puppies.polecat'] }) // => "Translation Missing: puppet.ferret"

@cibernox
Copy link
Collaborator Author

Tests are failing because of the glimmer incompatibility.

…er_to_readme

Explain that you can't translate until `ember-i18n` initializer runs
@jamesarosen
Copy link
Owner

Tests are failing because of the glimmer incompatibility.

You can either rebase off master now that the beta and canary tests are marked as may-fail, or wait until emberjs/ember.js#11445 and then #227 are merged.

@cibernox
Copy link
Collaborator Author

Green 😁

@cibernox
Copy link
Collaborator Author

Now that 4.0 is out of the door, can we revisit this?

}

Ember.assert(`Template for ${key} in ${this.id} is not a function`, Ember.typeOf(result) === 'function');
Ember.assert(`Template for ${fallbackChain[0]} in ${this.id} is not a function`, Ember.typeOf(result) === 'function');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be confusing, since it could be one of the fallbacks that's wrong. I'm wondering whether we should save off the key of the translation we found so we can use it here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jamesarosen
Copy link
Owner

I'm 100% sold on the idea and the API! I made a couple comments about smaller details.

Decission: Receive the fallbacks as an option in the `data` object
or is the key itself the one than can be an array
Also, default can be either an array or a string
@cibernox
Copy link
Collaborator Author

Feedback applied.

}

Ember.assert(`Template for ${key} in ${this.id} is not a function`, Ember.typeOf(result) === 'function');
Ember.assert(`Template for ${fallbackChain[i]} in ${this.id} is not a function`, Ember.typeOf(result) === 'function');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be 0, not i, right?

No, I'm wrong. This is the found version.

@jamesarosen
Copy link
Owner

+1 merging and squashing...

@jamesarosen
Copy link
Owner

Closed in 8aa758c

@cibernox
Copy link
Collaborator Author

wohoo!

@jamesarosen
Copy link
Owner

I'd like to document this in the wiki, but I'm not sure of a good spot. Maybe at the bottom of Translating Text in a new "Fallback Translation Keys" section?

@cibernox
Copy link
Collaborator Author

cibernox commented Jul 8, 2015

Yes, that seems a good place. That section basically teaches all the usages of the library.

@cibernox cibernox mentioned this pull request Jul 14, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants