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

xgettext-js

v3.0.0

Published

xgettext string extractor tool capable of parsing JavaScript files

Downloads

9,239

Readme

xgettext-js

Build Status NPM version

xgettext-js is a utility for extracting translatable strings, written in and capable of parsing JavaScript files. It is similar to the GNU xgettext program, but returns strings as a JavaScript array. It makes use of @babel/parser and estree-walker to parse JavaScript code, which facilitates the use of custom logic for string extraction. Because of this, xgettext-js is quite flexible, allowing you to define your own logic for extracting strings from any number of function keywords.

Installation

xgettext-js is a Node.js package available through npm. You must first install Node.js if it is not already installed, which will also install the npm package manager. Once installed, use your terminal to execute the following command from your project directory:

$ npm install xgettext-js

Usage

An instance of xgettext-js exposes a getMatches method which, when passed a JavaScript source string, will return an array of translatable strings. Each item in the array is an object which includes a string property, the line of the matched string, and any optionally included translator comments as the comments property. By default, xgettext-js will search for any occurrence of a _ function within your JavaScript code and assume that a translatable string exists as the first parameter. Translator comments can exist on the same or previous code line, formatted as translators: [insert comment here] by default.

Below is an example of this simple use case in a Node.js application:

var XGettext = require( 'xgettext-js' ),
	source = '_( "Hello World!" ); /* translators: greeting */',
	parser = new XGettext();

console.log( parser.getMatches( source ) );
// Will output: [ { "string": "Hello World!", "comment": "greeting", "line": 1, "column": 0 } ]

Options

To override the default behavior, you can pass an options object when creating an instance of xgettext-js, using one or more of the following options:

  • keywords : An object which defines keywords to be searched (the key) and a function or number (the value). If passed a function, it is expected to return either a string replacement for the string value of the getMatches array return value, or a replacement for the object itself. The function is passed a match including up to four keys: keyword (the matched keyword), arguments (a CallExpression arguments array, see parser documentation), line (the line of the matched string), and comment if one exists. If passed a number, it is expected that the translatable string exists at the corresponding argument position on a 1-based index.
  • commentPrefix: The comment prefix string to match translator comments to be included with translatable strings. A comment will be matched if it is prefixed by this option. If undesired, setting the value to undefined will omit comments from the getMatches return value.

License

Copyright 2016 Automattic

This package is made available under the GPLv2 or later license