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

i18nify

v3.0.2

Published

An internationalization plugin for browserify

Downloads

11

Readme

i18nify

Version npmnpm DownloadsDependenciesTips

i18nify is an internationalization plugin for browserify. It works greath with jedify.

Simple usage example

If you have the following files:

// entry.js
const hello = require('i18nify', './hello/%s')
console.log(i18n)
// hello/en.js
module.exports = 'Hello World'
// hello/nb.js
module.exports = 'Hallo Verden'

And then runs:

mkdir bundle
browserify                              \
  --entry entry.js                      \
  --plugin                              \
    [ i18nify                           \
      -l en                             \
      -l nb                             \
      -o 'bundle/%s.js'                 \
    ]                                   \
  --outfile bundle/main.js

The the following the files will be produced.

  • bundle/main.js
  • bundle/en.js
  • bundle/nb.js

Then, in your html file you should add main.js and one of the language spesific files.

If you are using browserify entries, then the localized bundle must run before the main bundle because entries are executed synchronous on script execution.

If you do not use entries, then the load order does not matter which means you can use the async flag on the script tag.

<script src="bundle/en.js"></script>
<script src="bundle.main.js"></script>

Options

When creating a bundle, i18nify should be used as a browserify plugin.

browserify -p [ i18nify OPTIONS ]

where OPTIONS are:

  -l
  --lang
      Add language code. This should be set multiple times to add multiple
      languages.

  -o
  --output
      Output expression that maps to a corresponding entry file at the same
      index. This should include %s in the string which will be replaced with
      the language code.
      It can either be reference a FILE or be a CMD. CMDs are executed with
      $LANG set to the current language code.
      Optionally specify a function that returns a valid value for this
      argument.

  -d
  --default
      Default language code. Defaults to the first language defined.

  --factor-bundle
      Similar to -o expect it maps to factor-bundle. CMDs are executed with
      $FILE set to the corresponding input file.

Moment.js example

Moment.js does not have a locale/en.js language file. To support this, the easiest way is to just tell browserify to ignore it.

Eg.

// entry.js

try {
  require('i18nify', 'moment/locale/%s.js')
} catch(e) {}

const moment = require('moment')

console.log(moment().format('LL'))

And when running browserify:

mkdir bundle
browserify                              \
  --entry entry.js                      \
  --ignore moment/locale/en.js          \
  --plugin                              \
    [ i18nify                           \
      -l en                             \
      -l nb                             \
      -o 'bundle/%s.js'                 \
    ]                                   \
  --outfile bundle/main.js

Usage in libraries

If you are writing a library which will be required from node_modules, then you should add i18nify as a browserify transform. If the library consumer is not using i18nify as a plugin, it will just bundle all the language variations in the main bundle and just work.

What about factor-bundle?

i18nify works with factor-bundle. Just add multiple --factor-bundle options which maps to the -o option in factor-bundle.

Eg.

mkdir bundle
browserify                              \
  --entry foo.js                        \
  --entry bar.js                        \
                                        \
  --plugin                              \
    [ i18nify                           \
      -l en                             \
      -l nb                             \
      -o 'bundle/%s.js'                 \
      --factor-bundle bundle/foo.%.js   \
      --factor-bundle bundle/bar.%.js   \
    ]                                   \
  --plugin                              \
    [ factor-bundle                     \
      -o bundle/foo.js                  \
      -o bundle/bar.js                  \
    ]                                   \
                                        \
  --outfile bundle/common.js            \

The above command will produce the following files in the bundle folder. Then it is up to you to include the right combination.

  • common.js
  • en.js
  • nb.js
  • foo.js
  • foo.en.js
  • foo.nb.js
  • bar.js
  • bar.en.js
  • bar.nb.js

What about watchify?

Yes, that should work out of the box.

Ok, I'm ready

npm install --save i18nify

Questions?

I'm available on IRC in #browserify@freenode with username tellnes.

License

MIT