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

@offen/l10nify

v0.4.0

Published

Localization workflow for Browserify

Downloads

70

Readme

l10nify

Localization workflow for Browserify

How does this work?

This is a Browserify-based workflow for localizing client side applications. It is built for use cases where you can and want to ship one bundle per locale. Strings are defined by calling a pseudo-global function (i.e. __(string, args..)) in the default language in your code (similar to GNU gettext or similar). Strings are stored in .po files.

Installation

Install from npm:

npm install @offen/l10nify

This installs two things:

  • a Browserify transform for localizing strings at bundle time. It is references as @offen/l10nify
  • a extract-strings command that you can use to generate PO files from your JavaScript code.

Usage

Defining strings in client side code

In your code, use the __(string, args...) function (__ is the default, but you can use anything) to declare strings in your default language (which defaults to en but can be anything you want it to):

const HappyBirthdayComponent = (props) => {
  return (
    <h1>{__('Happy birthday, %s!', props.name)}</h1>
  )
}

Extract strings from your code

Next, you can extract these strings from your code into .po files using the extract-strings command:

$(npm bin)/extract-strings **/*.js

This will extract the strings from all matching files and print a .po file to stdout. Use the standard gettext tools like msgmerge and msgcat to combine the output with existing .po files.

Refer to extract-strings --help for a full list of options

Why not just use xgettext

While xgettext works perfectly fine on ES5 code, it will choke on ES6+ syntax and also does not support parsing JSX, which l10nify supports.

Apply the transform at bundle time

Apply the transform to your Browserify setup passing the target locale. In development, you can omit this parameter to make the transform return the default locale, i.e. the strings you defined in code.

var browserify = require('browserify')

var b = browserify()
b.add('app.js')
b.transform('@offen/l10nify', { locale: 'fr' })
b.bundle(function (err, src) {
  // consume bundle
})

Options

The following options can be passed to the transform:

locale

locale specifies the locale which you want to return when bundling. It defaults to en or process.env.LOCALE when set.

defaultLocale

defaultLocale specifies the default locale that is used to define strings in code. It defaults to en or process.env.DEFAULT_LOCALE when set.

source

source specifies the directory in which the <LOCALE>.po files are stored. It defaults to ./locales or process.env.SOURCE when set.

global

global defines the global function identifier that is used for defining strings in code. It defaults to __ or process.env.GLOBAL when set.

License

Copyright 2020 Frederik Ring - l10nify is available under the MIT License