npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hogan-i18n

v0.1.0

Published

Testing out i18n functionality in hogan

Downloads

2

Readme

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.