kimroen / ember-cli-document-title

Adding document title behaviour to your ember app
MIT License
215 stars 61 forks source link

Support for Auto Page Title with I18n #8

Open blimmer opened 9 years ago

blimmer commented 9 years ago

Thanks for this project, it's awesome to not have to have @machty 's source just copy/pasted into our project anymore.

We originally modified the gist just slightly to allow automatically providing a page title in our Ember I18n translation file so we didn't need to create a route file simply to provide a title or titleToken.

To get it working with this add-on we made use of an initializer to support the auto-lookup. Something like this:

import Ember from 'ember';

export function initialize(/* container, application */) {
  Ember.Route.reopen({
    titleToken: Ember.computed('routeName', function() {
      var pageTitleKey = `pageTitles.${this.get(routeName)}`;
      if(Ember.I18n.exists(pageTitleKey)) {
        return Ember.I18n.t(pageTitleKey);
      }
    })
  });
}

export default {
  name: 'route',
  initialize: initialize
};

and then we provide a translation for the route name in our translations file, like this:

en = {
  pageTitles: {
    application: 'My Page Name'
    ...
  }
}

I'm happy to keep-on-keepin' on with our custom re-open but I was curious if this sort of thing would be helpful for anyone else using this project.

kimroen commented 9 years ago

Hi again - sorry I didn't get to this sooner.

I don't think this functionality belongs in this addon - there are multiple I18n-solutions for Ember and I'm not too keen on supporting everyone.

Having to define a route to define a title is a separate issue though, and it might be worth looking in to that!

I was first thinking that maybe the best way would be to implement the basic-route (the default route that is used when one is generated automatically), but then I noticed there has been some movement there that suggest it might be going away (or at least changing names), so maybe that's not the best way.

I like the title to be route and router-driven, so I'm not too sure about having some kind of title map either, although it might work.

acorncom commented 9 years ago

@kimroen Totally understood on not supporting all the i18n solutions out there for Ember. Spitballing here, but would we be able to set up "plugin" functionality of some kind to allow each i18n solution to provide the needed config as desired?

That way, for this add-on, we'd only need to call the standard plugin interface, but it'd give folks the flexibility to customize this as desired ...

blimmer commented 9 years ago

@acorncom I love that idea and would help with an ember-i18n adapter.

acorncom commented 9 years ago

@blimmer So would I ;-)

@kimroen If we can get your "blessing" on that idea, we'll go ahead and send in a pull request ...

kimroen commented 9 years ago

Sure. I'm very curious as to what you have in mind for that.

I had a different thought though - how about allowing the user to define a default title token function? The user would define this in their router:

Ember.Router.extend({
  defaultTitleToken(routeName) {
    // Parse the name and return whatever you want
    if (Ember.I18n.exists(pageTitleKey)) {
      return Ember.I18n.t(pageTitleKey);
    }
  }
});

Then in ember-cli-document-title's route-implementation, we could do something like this in collectTitleTokens:

collectTitleTokens: function(tokens) {
  var titleToken = get(this, 'titleToken');
  if (titleToken === null) {
    titleToken = this.router.defaultTitleToken(get(this, 'routeName'));
  } else if (typeof titleToken === 'function') {
    titleToken = titleToken.call(this, get(this, 'currentModel'));
  }

  // The rest of the function
}

Now, whenever a route that has no titleToken implemented is entered, the defaultTitleToken will be consulted, and if it returns anything, that is used as the token for that route.

We might want to pass it the model and provide the context of the route instead of passing in the name, but I just wanted to communicate the idea.

Thoughts on that? This way, it should be fairly trivial to do whatever you want.