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

canvas_react_i18n

v1.2.1

Published

Convert React <Text /> components to Canvas-ready I18n.t() calls.

Downloads

257

Readme

canvas_react_i18n

Write HTML markup inside a special <Text /> React component and it will be used as defaultValue to a translated phrase, with wrappers automatically extracted. Best described with an example.

This JSX:

<Text key="greeting">
    <p>Hello World!</p>
</Text>

Compiles into something like this:

<div dangerouslySetInnerHTML={
    {
        __html: (function() {
            var wrapper = {
                "*": "<p>$1</p>"
            };

            return I18n.t("greeting", "* Hello World! *", { "wrapper": wrapper });
        }())
    }
} />

The output will be more compact than the above making it ready to be injected anywhere. And of course, we're assuming that an I18n variable is in-scope. canvas_react_i18n assumes you are using i18n-js in conjunction with i18nliner

See the test/fixtures/ folder for more examples. It contains pairs of files; raw JSX inputs and their compiled outputs.

Usage

Just require the module and use the function on a block of text; a script that contains any number of <Text /> tags.

/* jshint node: true */
var transform = require('canvas_react_i18n');
var fs = require('fs');

var component = fs.readFileSync('./some/jsx/component.jsx');
var transformedComponent = transform(component);

fs.writeFileSync('./some/jsx/component-transformed.jsx', transformedComponent);

A sample implementation of the <Text /> component can be found in the Wiki.

Internals

The transformation is done in three passes:

Pass 1: extracting blocks

Go through the source and locate <Text /> tags, extract the I18n phrase, any interpolation variables, and the actual markup.

Pass 2: wrapping the markup

Scan for HTML tags, replace them with "I18n wrappers" (stuff like *content* and **content**), and build the wrapper set that will be injected into the I18n.t() directive later on.

The transformer fully supports any level of tag-nesting. An input like this is totally valid:

<p>
  <span>
    Hello
    <span>%{name}!</span>
  </span>

  <a href="http://google.com">Click me!</a>

  <iframe src="%{page_url}"></iframe>
</p>

Output would be:

*
  **
    Hello
    ***%{name}!***
  **

  ****Click me!****

  **********
*

The generated "wrapper": {} set for the example above looks something like this:

var wrapper = {
    "*": "<p>$1</p>",
    "**": "<span>$1</span>",
    "***": "<span>$1</span>",
    "****": "<a href=\"http://google.com\">$1</a>",
    "*****": "<iframe src=\"%{page_url}\">$1</iframe>"
};

Pass 3: transform

Using the extracted I18n parameters and the "wrapped" markup, we go over the source and replace the original, raw <Text ... >...</Text> contents with the compiled I18n.t() directive.

Running tests

npm install
npm run test

LICENSE

MIT