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

fluent-react-extract

v0.1.2

Published

String extraction CLI for fluent-react

Downloads

7

Readme

Fluent React Extract

Get default messages from your source code so that you don't have to switch context to manage translatable messages

Uses the fluent-syntax serializer and Babel parser to create an AST for your React code to pull out default messages and format them in FTL.

How to Use

l10n extract [--pattern '...'] [--outputDir '...']

This will run through all files matching the file pattern, parse the code into an AST, locate all the Localized or Loc.X (Loc. is the default, but this can be customized, see .l10nrc information below) helper components and pull the id, messages, and comments for that component into a fresh data.ftl file located in the specified output directory.

Example

// imports

export function NotAuthorized() {
  return (
    <div className="container">
      <Loc.H1 l10nId="NotAuthorized_oopsMessage">
        {/* here is a comment for the localizer */}
        Oops! looks like you're not authorized to use this app.
      </Loc.H1>
      <Loc.Img
        l10nId="NotAuthorized_warningImg"
        alt="warning sign"
        src={warningImg}
      />
      <Loc.P
        l10nId="NotAuthorized_possibleActions"
        l10nJsx={{
          link: <Link to="/" />,
          a: <a href="https://www.udacity.com" />,
          button: <button onClick={signOut} />
        }}
      >
        {
          '<link>Try again</link>, go <a>Home</a> or <button>Logout</button>'
        }
      </Loc.P>
    </div>
  );
}

Running the localization script on this file would result in the data.ftl below:

# here is a comment for the localizer
NotAuthorized_oopsMessage = Oops! looks like you're not authorized to use this app.
NotAuthorized_warningImg = 
  .alt = warning sign
NotAuthorized_possibleActions = <link>Try again</link>, go <a>Home</a> or <button>Logout</button>

NOTE: adding comments along with the children of a component will extract this out as a comment to the localizer. This can be helpful for leaving tips about context of the message to make translation easier.

.l10nrc File

In order to run the extraction based on your project's custom needs, a .l10nrc file is supported. Possible values of this file:

customElementsPath

If you use the custom elements as described above, if they include any attributes (i.e. label on the MyButton component), they need to be included to tell the extraction scripts which attributes to look for. The format for this is a json object with the component names as keys, and an object of their allowed (or explicitly disallowed) attributes. For example:

// custom-elements.json
{
  "MyButton": {"label": true}
}

It is recommended that this record of custom element attributes is held in a json file that is pulled into any file defining the attrs of that element in the React code.

import {augmentLoc, makeLocalizedElement} from 'fluent-react-utils';
import customAttrs from './custom-elements.json';

const customElements = {
  MyButton: makeLocalizedElement(MyButton, customAttrs.MyButton)
};

export const Loc = augmentLoc(customElements);

The value for customElementsPath is the path to the custom-elements.json file from the project root.

For example:

{
  "customElementsPath": "./src/app/utils/custom-elements.json",
}

shorthandName

By default, this is Loc, but if your project prefers not to use the same name as this library's Loc utility, you can update this in the .l10nrc file.

// my-utils
import {augmentLoc, makeLocalizedElement} from 'fluent-react-utils';
import customAttrs from './custom-elements.json';

const customElements = {
  MyButton: makeLocalizedElement(MyButton, customAttrs.MyButton)
};

export const L = augmentLoc(customElements);

// elsewhere

import {L} from 'my-utils';

...
<L.H1 l10nId="ButtonPage_title">
  Look, a button!
</L.H1>
<L.MyButton
  l10nId="ButtonPage_clickButton"
  label="click me!"
  onClick={handleClick}
/>
{
  "shorthandName": "L",
}

filePattern

In case your project always extracts the same file pattern, this can be defined in .l10nrc as opposed to passing it into the cli every time.

{
  "filePattern": "./src/app/**/*.{js,jsx}",
}

outputDir

The location you want the string extraction script to write its results.

{
  "outputDir": "./public/locales/new-strings/",
}

Future Work:

  • Message deduplication with interface
    • decide which message to keep, or to create a new l10nId and write back to file
  • Compare string extraction output with production files
    • provide no reference warnings (may indicate obsolete string)