Socket
Socket
Sign inDemoInstall

hogan-i18n

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hogan-i18n

Testing out i18n functionality in hogan


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

hogan-i18n

Provides a new compile option that translates templates prior to rendering. While lambda functions can be used to support i18n in mustache templates, Hogan.js does not support lambda functions in pre-compiled templates. Pre-compiling templates offers both bandwidth and start-up performance benefits, which hogan-i18n enables users to take advantage of while still supporting i18n. Developers can use hogan-i18n.compile(text, options, gettext) as part of their toolchain when building client-side assets.

Usage example


var hogan = require('hogan-i18n'),
    Gettext = require("node-gettext"),
    gt = new Gettext(),
    gettext = function(str) { return gt.gettext(str) },
    fileContents = fs.readFileSync("es.po");

gt.addTextdomain("es", fileContents);

var text =  [
  "<em>",
    "{{_i}}", // notice the opening and closing i18n tags
      "My name is {{name}}.",
    "{{/i}}",
  "</em>"
].join('');

// Only change needed is to call compile with a gettext function that
// will translate the given string.
var content = hogan.compile(text, {asString: true}, gettext);

// then whatever you need to do to incorporate in your build...

template.render({name: "Chad"});
// Mi nombre es Chad.

getstrings

getstrings is cli tool to extract all strings from your mustache templates and put them into a .po file for translation.


bin/getstrings templates/

# You can also specify an extension to use other than the default .mustache
# Here specifying .html

bin/getstrings templates/ html

Limitations

hogan-i18n doesn't support options like pluralization, just simple string-for-string lookup. The mustache templating syntax just isn't expressive enough for these tasks. If you'd like to control pluralization, I suggest the following convention.


{{#plural}}
{{_i}}She has {{num}} votes.{{/i}}
{{/plural}}

<!-- Ella tiene 3 votos. -->

{{^plural}}
{{_i}}She has one vote.{{/i}}
{{/plural}}

<!-- Ella tiene un voto. -->

How does it work?

The nice folks maintaining hogan provide a scan and parse function that exposes an AST. hogan-i18n walks that tree to find {{_i}}{{/i}} nodes and translates any strings before creating the final template. It will remove {{_i}}{{/i}} tags and just keep the translated content within them.

FAQs

Package last updated on 04 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc