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

gettextractor

v0.2.0

Published

Extracts gettext strings from code to .po file

Downloads

4

Readme

gettextractor

CLI for extracting translation strings from application source files to a .PO (gettext) file for later translation. It uses babel under the hood to traverse AST tree and find all strings that appear in specific function calls.

Usage

Consider the following application source fragment:

    // src/utils.ts
    import __ from './translate';

    export function injectHeader () {
        const element = document.createElement('h1');
        element.innerText = __('Hello world!');

        document.appendChild(element);
    }

regardless of what __ function does, in an application development lifecycle, at some point you'll want to extract all translatable strings from source code and generate a file that'll want to send for translation. This tool is able to extract those strings from your whole application code to a single .po file.

To do that on the above file, do:

$ gettextractor --name __ --dir src --filter '.ts'

This will print the .PO file contents to standard output. See --out flag usage for info on how to save it to a file.

Plural forms

If necessary, you can also add plural form that will be added to msgid_plural in resulting .PO file using context object:

const text = __('Hello world', { pluralForm: 'Hello worlds' });
#: src/HelloWorld.ts:1:13
msgid "Hello world!"
msgid_plural "Hello worlds!"
msgstr[0] "Hello world!"

CLI flags

--name

Provide the name of a function that will be looked for during extraction.

--dir

Base directory name where all your source files are.

--filter

You can provide extensions list in the --filter flag to modify what files will get checked for presence of translation strings. For example, if you have a React application, you can do:

$ gettextractor --name __ --dir src --filter '.js,.jsx,.ts,.tsx'

--out (optional)

If you want to have the result saved to a file, provide its name as a value for this flag:

$ gettextractor --name __ --dir src --filter '.ts' --out en.po

--annotation (optional)

Sometimes, you may want to pass some information to future translators (reg. context, etc). In such case, you can do so by adding a comment direcly above the line with translation:

// TRANSLATORS: It's defining the greeting message.
const text = __('Hello world!');

This flag allows you to change the default TRANSLATORS prefix used for comments that will be passed to the resulting .po file, i.e.:

// CONTEXT: Custom info for translators.
/*
 * CONTEXT: I can also accept
 * multiline comment.
 */
const text = __('Hello world!');
$ gettextrator --name __ --dir src --filter '.ts' --annotation 'CONTEXT'
msgid ""
msgstr "Content-Type: text/plain\n"

# Custom info for translators.; I can also accept multiline comment.
#: src/HelloWorld.tsx:14:20
msgid "Hello world!"
msgstr "Hello world!"