digitalascetic / iron-router-i18n

Meteor iron router support for i18n
MIT License
36 stars 5 forks source link

Cannot read property 'route' of null #64

Closed paulo2nd closed 9 years ago

paulo2nd commented 9 years ago
Router.getLangCode = function () {

    var langCode = null;
    var url = this.current().route.path(); //Cannot read property 'route' of null

    if (this.options.i18n.getLangCode) {
        langCode = this.options.i18n.getLangCode.call(this, url);
    }

    return langCode;

};

I am using Router.getLangCode as below:

getUserLanguage = function () {
    return Router.getLangCode();
};

if (Meteor.isClient) {
  Meteor.startup(function () {
    Session.set("showLoadingIndicator", true);

    TAPi18n.setLanguage(getUserLanguage())
      .done(function () {
        Session.set("showLoadingIndicator", false);
      })
      .fail(function (error_message) {
        console.log(error_message);
      });
  });
} 
martinopic commented 9 years ago

The problem is that during startup phase the current controller has not yet been set so this.current() is null. I think the method can be rewritten to just use current url.

martinopic commented 9 years ago

Ok, this patch should solve your problem and improve a bit other edge cases.

By the way my suggestion is to use I18NConf.getLanguage to get current language, Router.getLangCode is just a utility method to extract the language code from the url (if any!).

paulo2nd commented 9 years ago

Had change my code yesterday to use I18NConf.getLanguage. Thx