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

bawlerjs

v0.1.2

Published

Simple messages for your js

Downloads

4

Readme

Bawlerjs

Simple translatable messages in your javascript coming from a backend or object.

npm version Build Status Scrutinizer Code Quality

Motivation

Talking with a colleague about how to integrate translations into an old js codebase I came up with the idea of making this package. It is the simplest approach to translations in javascript I could think about.

As you are probably thinking, the purpose of this package is to be as easy to setup and use as possible.

Installation

Using npm

Execute this command in the console:

npm i bawlerjs

Using yarn

Execute this command in the console:

yarn add bawlerjs

Standalone

Add this to your html:

<script src="https://cdn.jsdelivr.net/npm/bawlerjs/dist/bawler.js"></script>

You can see all available versions here: https://www.jsdelivr.com/package/npm/bawlerjs

Usage

Rememeber that if you are using the package as a js module using yarn or npm you have to import the package:

import Bawler from 'bawlerjs';

You can also import the msg method directly:

import { msg } from 'bawlerjs';

Setting output language for the messages

You can change the language Bawler is outputting messages to by calling the lang method. By default the selected language will be en.

Bawler.lang('es'); //set the "es" language

Registering messages to languages

You can set messages in Bawler by using the register and registerUrl methods.

Synchronous message registration

By using the register method you can register the messages of a language in a synchronous way.

Bawler.register('en', {
    'HELLO WORLD': 'Hello world!',
});

Asynchronous message registration

You can also register messages from an url by calling the registerUrl method. The output of the url must be a json object whose keys are the message keys and the values are the translated messages.

The method will return a promise that will be resolved when the url is correctly loaded.

Example:

await Bawler.registerUrl('/messages-en.json', 'en');

Outputting messages

To output a message, simply call the msg method. Remember you can import the msg function directly from the module.

Bawler.register('en', {
    'HELLO WORLD': 'Hello world!',
});

Bawler.lang('en');

Bawler.msg('HELLO WORLD'); //Outputs: Hello world!

You can also pass variables to the messages by using sprintf syntax (as in C and similar languages):

Bawler.register('en', {
    'HELLO %s': 'Hello, %s!',
});

Bawler.lang('en');

Bawler.msg('HELLO %s', ['handsome']); //Outputs: Hello handsome!

If the message is not defined in the current language, Bawler will output the passed key directly:

Bawler.register('en', {
    'HELLO WORLD': 'Hello world!',
});

Bawler.lang('es');

Bawler.msg('HELLO WORLD'); //Outputs: HELLO WORLD

Bawler.lang('en');

Bawler.msg('THIS DOES NOT EXIST'); //Outputs: THIS DOES NOT EXIST