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

provide handlebars helpers #4

Open
jenseng opened this issue Oct 10, 2014 · 7 comments
Open

provide handlebars helpers #4

jenseng opened this issue Oct 10, 2014 · 7 comments

Comments

@jenseng
Copy link
Collaborator

jenseng commented Oct 10, 2014

need to provide handlebars helpers for the following:

  • t
  • __i18nliner_escape
  • __i18nliner_safe
  • __i18nliner_concat

these don't need to be ember-aware; that could potentially be done in an add-on node package

@fivetanley
Copy link
Owner

Awesome! Will these integrate with i18nliner-js in some way I assume?

@jenseng
Copy link
Collaborator Author

jenseng commented Oct 10, 2014

yeah i'm still figuring out how some of the glue between them should work... i have an app that has i18nliner + i18nliner-js + i18nliner-handlebars all hacked together w/o issues, so it is possible. just need to settle on a nicer/cleaner approach

@jenseng
Copy link
Collaborator Author

jenseng commented Oct 10, 2014

my working ember version of these looks like this:

Ember.Handlebars.registerHelper("t", function() {
  var args = [].slice.call(arguments);
  var hbsOptions = args.pop();

  if (hbsOptions.fn) {
    window.console.log("WARNING: unhandled {{#t}} block call; looks like i18nliner-handlebars pre-processor is not wired up");
    // IOW, translations no worky
    return hbsOptions.fn(this);
  }

  var hash = hbsOptions.hash;
  var hashTypes = hbsOptions.hashTypes;
  var hashContexts = hbsOptions.hashContexts;
  var key;
  var value;
  var type;
  var wrappers;
  var result;
  var options = {};

  for (key in hash) {
    if (!hash.hasOwnProperty(key)) continue;

    value = hash[key];
    type = hashTypes[key];
    if (type === "ID") {
      options[key] = Ember.get(hashContexts[key], value);
    } else if (type === "STRING"){
      options[key] = value;
    }
  }

  wrappers = [];
  while ((key = "w" + wrappers.length) && hash[key]) {
    wrappers.push(hash[key]);
  }
  if (wrappers.length)
    options.wrappers = wrappers;

  args.push(options);
  result = I18n.t.apply(I18n, args);

  if (wrappers.length) // i18nliner-js auto html-escapes if there are wrappers
    result = new Ember.Handlebars.SafeString(result);
  return result;
});

Ember.Handlebars.registerHelper("__i18nliner_escape", function(val) {
  return Ember.Handlebars.Utils.escapeExpression(val);
});

Ember.Handlebars.registerHelper("__i18nliner_safe", function(val) {
  return Ember.Handlebars.SafeString(val);
});

Ember.Handlebars.registerHelper("__i18nliner_concat", function() {
  var args = [].slice.call(arguments, 0, arguments.length - 1);
  return args.join("");
});

the vanilla handlebars ones could be quite a bit simpler

@fivetanley
Copy link
Owner

Nice. I'm ok with vanilla handlebars helpers first. I mean, you're doing all the work and stuff I'm just providing input, so it doesn't mean much. When I get some time I'd like to get this wired up with broccoli/ember-cli and work on helpers then. I'd like i18nliner to be the go-to solution for i18n in Emberland as I found the development experience extremely pleasant (thank you!). I know y'all aren't heavily invested in Ember (save for one project maybe?) so this working version is great! Stuff is about to change in emberland with htmlbars coming (eventually :trollface: ), which gives us compile-time tag balancing warnings and other nice things.

@jenseng
Copy link
Collaborator Author

jenseng commented Oct 14, 2014

what do you think about having some of that be in other modules/add-ons? iow, i18nliner-handlebars at most just provides a framework for:

  1. extracting strings
  2. preprocessing block->inline
  3. vanilla helpers needed so that 2. works at runtime

then you could have separate add-ons for things like:

  1. ember helpers
  2. broccoli preprocessing
  3. grunt
  4. gulp, etc

as it is, i am now incorporating i18nliner-handlebars into my third app, and every one of them has a different build process (broccoli vs grunt vs rjs), and a mix of ember-ish vs vanilla handlebars. so i think it'd be good if we weren't too prescriptive about how people use it (in the main/core lib anyway)

@fivetanley
Copy link
Owner

definitely in favor of different addons for all the things

@fivetanley
Copy link
Owner

i'd be happy to work on broccoli if you guys need it soon

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

2 participants