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

traks

v3.0.1

Published

Translations for React; Automatic, Kind and Superior

Downloads

49

Readme

Traks - a translation system for React/JSX.

Demo

See traks-zoo (https://github.com/aktiedysten/traks-zoo). Its commit history also serves as a step-by-step guide for setting up traks using create-react-app.

Introduction

The basic idea is to add elements in your JSX code like this:

<T>You have {count} unread message(s)</T>

Then you run bin/traks update (in this example bin/traks update --langs en,da) which searches for <T> tags in your source code. New tags are automatically added to your translations file, and looks like this:

	"e8c2410dd77c": {
		"_new": true, // FIXME remove this line when translation is done
		"en": (count) => <O>You have {count} unread message(s)</O>,
		"da": (count) => <O>You have {count} unread message(s)</O>,
	},

You can then translate it and even handle plural/singular cases by changing it into this:

	"e8c2410dd77c": {
		"en": (count) => {
			switch (count) {
			case 0: return <O>You have no unread messages</O>;
			case 1: return <O>You have 1 unread message</O>;
			default: return <O>You have {count} unread messages</O>;
			}
		},
		"da": (count) => {
			switch (count) {
			case 0: return <O>Du har ingen ulæste beskeder</O>;
			case 1: return <O>Du har 1 ulæst besked</O>;
			default: return <O>Du har {count} ulæste beskeder</O>;
			}
		},
	},

More features

  • You can provide context when ambiguities arise, e.g. <T context="file">Save</T> and <T context="money">Save</T> - these result in separate translation keys so you can translate them differently.
  • Dependencies are captured automatically, for example count in the introduction above. But you can also specify additional dependencies: <T deps={[foo]}>Translate me</T> will add foo as dependency and pass it to your translation function even though foo is not used inside the <T> tag.
  • At Aktiedysten ApS we've combined context and deps to format dates and such, e.g. <T context="1 January 2018" deps={[timestamp]}></T> - then the language specific date formatting happens in the respective translation functions.
  • Translations can be "baked", so that only one language is included when the site is built. See traks-zoo (specifically https://github.com/aktiedysten/traks-zoo/blob/master/bake-npm-start.sh), for an example on how to do this. By default, all translations are included in the build, so baking saves space.

Caveats

  • It's not trivial to install; npm install traks isn't enough; you also need to insert our custom Babel preset (see react-app.js) into the compilation flow (this is where the "magic" handling of <T> tags happens). If you're using create-react-app you need to eject it before modifying the Babel preset is even possible (this is what traks-zoo did). Other build setups have not been tested.
  • There are restrictions on how you can use <T> tags; see the autogenerated traks.js and traks-translations.js files for documentation on this.

Why?

The two most popular translation solutions for React are react-intl and react-i18next. Both have a verbose and unnatural syntax, and want you to assign keys to every translation. I have previously worked with gettext which is intuitive to work with, has a light syntax and it automatically extracts translation strings from your source code. So, I desired something similar for React.