fivetanley / i18nliner-handlebars

i18nliner, but for your handlebars templates
MIT License
10 stars 9 forks source link

provide handlebars helpers #4

Open jenseng opened 9 years ago

jenseng commented 9 years ago

need to provide handlebars helpers for the following:

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

fivetanley commented 9 years ago

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

jenseng commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

definitely in favor of different addons for all the things

fivetanley commented 9 years ago

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