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

express-it

v1.2.3

Published

serve custom translation variants for your Node.js Express app

Downloads

14

Readme

Express it

Serve custom translation variants for your Express 4.x app.

$ npm install express-it --save

Peer dependencies required

  • express: For translation center & middleware integration.
  • redis: For storing persistent translations in memory.

Purpose

If you’ve ever wanted to serve different message copy to a user based on varying cirumstances, this is the right tool.

This tool can also be used as a light-weight solution to i18n (internationalization), however be mindful that the scope of this project is not aimed at creating custom routing, dealing with pluralisation, or custom number formatting. It can, however, live along side your existing i18n framework (if this is your desire).

How it works

The idea is to put the user into a custom translation variant based on any arbitrary data you see fitting. For example, several websites will use different messaging based on the users demographic, or if they were invited through a specific campaign or community.

Thus, instead of having to do this everywhere throughout your code...

if (user.age > 18 && user.age < 24 && user.gender == 'female') {
  render('<p>Like, hello {name}. Your totes welcome to the fridge</p>')
}
else if (user.age >= 21 && user.gender == 'male') {
  render('<p>Yo {name}. The beer is in the fridge.</p>')
} else {
  render('<p>Welcome {name}. Here is the fridge.</p>')
}

You could do something like this (we’ll show you how to setup a user in a variant later):

.welcome
  p!= t('welcome.fridge.msg', { name: user.first_name }).backup('Welcome {name}. Here is the fridge.')

A fully managed translation center

This tool comes with an administration interface (integration required) allowing you to update message copy in real-time.

http://f.cl.ly/items/1u263H1O3X0z3H2r2y3T/Screen%20Shot%202015-06-23%20at%205.42.42%20PM.png

Integration

var client = require('redis').createClient()
var VARIANTS = ['en.default', 'en.tech', 'en.millenials', 'en.fb_mom_campaign', 'en.pinterest_users']
YOUR_APP.use('/path/to/translation-center', require('express-it').init({
 express: YOUR_EXPRESS,
 redis: client,
 variants: VARIANTS
}))


YOUR_APP.use(function (req, res, next) {
 // this introspection can be async
 req.variant = 'en.default' // or some other variant based on user data
 next()
})

YOUR_APP.use(require('express-it').middleware({
 redis: client,
 variants: VARIANTS
}))

Usage

Both client and server templates can take advantage of the t() function. The usage of the backup() method is required in all circumstances in cases where redis fails, or if a key is not found.

.welcome
  p= t('welcome.msg', { name: user.first_name }).backup('Hello {name}. Welcome to our website')

Usage from pure client-side

// use the "E" view local to load your translations and "t()" function
html
  head
    title example
    != E.getLoader()
    // etc...
// call `t` from anywhere...
<script>
  window.t('welcome.msg', { name: user.username }).backup('Welcome to Example.com')
</script>

Cascading variations

One great feature ExpressIt provides is the ability to cascade a user with multiple variations. For example one user might be part of both a Pinterest Campaign variant, and millenial (age group) variant. You can set your req.variant as such in your integrated middleware.

YOUR_APP.use(function (req, res, next) {
  req.variant = ['en.pinterest_campaign', 'en.millenial', 'en.default']
  next()
})

In the example above, you may have a translation key for welcome.msg in the en.pinterest_campaign variant.

However in the absense of a translation for the tutorial.step.1 translation key for en.pinterest_campaign, it will cascade to the next variant — which in this case is en.millenial.

The advantage of this is that not every variant requires a translation for each key, allowing you to fallback onto other (perhaps more appropriate) messaging.

Tests

see tests/index.js

$ npm test

Contributions

Yes please, and always welcome! Please consider the primary goals too. This is not intended to be a full i18n replacement. However performance improvements, bug fixes, and minor feature additions are fully welcomed!

Licence MIT

express-it is © 2015 Dustin Diaz and licensed under MIT. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for details.