Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Auto Page Title with I18n #8

Open
blimmer opened this issue Mar 26, 2015 · 5 comments
Open

Support for Auto Page Title with I18n #8

blimmer opened this issue Mar 26, 2015 · 5 comments

Comments

@blimmer
Copy link
Contributor

blimmer commented Mar 26, 2015

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
Copy link
Owner

kimroen commented Apr 22, 2015

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
Copy link

acorncom commented Jun 2, 2015

@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
Copy link
Contributor Author

blimmer commented Jun 3, 2015

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

@acorncom
Copy link

acorncom commented Jun 3, 2015

@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
Copy link
Owner

kimroen commented Jun 4, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants