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

transgator

v1.0.0

Published

A performance-focused, 0 dependency i18n library

Downloads

1

Readme

transgator

Transgator is a tiny (1.4kb minified) translation library that has no dependencies and is pretty easy to use and implement. Browser support should be IE9+ and the usual browsers that don't suck.

Installing

Add a <script> tag to your <head> with a reference to transgator.min.js.

Wasn't that easy?

Documentation

Translating the content of an element

Add a data-attribute to any element you want to translate with text in it and add a key to it. data-i18n-key is the default.

Write your translations in JSON files in the below format and place them all in the same directory. I recommend naming your translation files with a valid ISO 639-2 language code.

HTML

<p data-i18n-key="translation_key">This is the default text.</p>

JSON

{
 "translation_key" : "Translated value!"
}

You'll end up with:

<p data-i18n-key="translation_key">Translated value!</p>

Translating attributes on an element

Specify your attributes by separating a translation key with a pipe, then the name of the attribute you want to translate. You can specify an infinite number of attributes if you really want to.

HTML

<input data-i18n-key="somekey|value" type="button" value="Translate me!"/>

JSON

{
  "somekey|value" : "Translated value for the attribute 'value' "
}

You'll then end up with:

<input data-i18n-key="somekey|value" type="button" value="Translated value for the attribute 'value' "/>

Surgeon General's Warning: If you need to translate an attribute, you must specify a root keyname in your data attribute. However, you do not need to specify this key in your JSON translation file.

Translating an attribute and an element's content at the same times

Use the exact same method for translating an attribute, but specify a keyname for the translation in your JSON file.

HTML

<div data-i18n="elementcontent|attribute" attribute="Translate me!">Translate me!</div>

JSON

{
  "elementcontent" : "Translated element content value"
  "elementcontent|attribute": "Translated attribute value"
}

This yields:

<div data-i18n="elementcontent|attribute" attribute="Translated attribute value">Translated element content value</div>

Running Transgator

Ideally, you should place your config object and the initial call to Trangator at the end of your document, before the closing body tag. If you do not, you must init Transgator after document ready is fired, or the DOM node <-> translation dictionary that is generated on init may not get fully populated.

  • Build a config object to pass into Transgator.
  • Specify the directory that your translations are in on your server in a i18n_dir key
  • Specify the data-attribute key that Transgator should inspect in your DOM with the i18n_key key

See below for an example config and some basic explanations.

var config = {
  i18n_key : "data-i18n", //the data attrib to inspect in your document.
  i18n_dir : "/i18n/" //the directory your translations are stored in.
};

var i18n = new Transgator(config); //Initiates Transgator. You should do this after docready.

Keep in mind that you should only init Transgator after document ready, or your translation hashmap could fail to generate.

Changing language

There is only one method available, and that is the lang method. You can utilise it like so;

i18n.lang('en'); //String passed corresponds to your translation file

You can call lang any number of times you like after the Transgator is initialised. Calling lang will also update the HTML lang attribute in your markup accordingly.


Things to consider

Currently, support for dynamically updated DOM doesn't exist. This means the library is not really suited to single page applications etc etc. Pull requests are welcome, but your implementation must be minimal and fast.