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

translation-files

v0.1.5

Published

Extract all translation files from typescript source code. Clean output format that can be used by translators.

Downloads

3

Readme

Translation files

Node utility tool to extract translation files from typescript source code and output them in a human readable format so they can be sent to translation services.

Install

You can find this package from npm

npm install translation-files

Usage

// src/todo-list/translations.ts
export const translations = {
    todo: {
        key: "page.title.todo",
        default: "Todo",
        description: "Displayed as page title",
    },
    completed: {
        key: "todo-list.completed.message",
        default: "You have finished {0}/{1} items of your todo list.",
        0: "Number of finished todo items",
        1: "Number of unfinished todo items",
    }
}
// src/todo-list/translations.tsx
import { translations } from "./translations";

export const TodoList = ({
    getTranslationByKey,
}: {
    // this part you need to provide yourself :)
    getTranslationByKey: (key: string) => string;
}) => {
    const todoText = getTranslationByKey(
        translations.todo,
    );

    let completedText = getTranslationByKey(
        translations.completed,
    );

    const totalTodos = 10;
    const completedTodos = 3;

    // dummy function to replace {0} and {1} with values
    completedText = format(
        completedText,
        completedTodos,
        totalTodos,
    );

    return (
        <>
        <header>
            <h1>{todoText}</h1>
        </header>
        <div className="todo-list">
            ...
            <p>{}
        </div>
        </>
    );
});

You can use it as follows. In your package.json, add a new script to run:

  "scripts": {
    "translations": "translation-files",
  },

Then when you run npm run translations it will output a translation file with default settings.

Example of the output for the translations.ts file listed above:

key:            page.title.todo
description:    Displayed as page title
default:        Todo

key:            todo-list.completed.message
default:        You have finished {0}/{1} items of your todo list.
                    0: Number of finished todo items
                    1: Number of unfinished todo items

You can modify the npm script to your needs using the following cli options:

Usage: translation-files [options]

Options:
  --pattern [globPattern]  Pattern (glob) to used to find translation files. (default: "**/**/translations.ts")
  --outFile [file]         File where translations should be saved to.
  --exportName [var]       Name of exported root variable used in each source translation file. (default: "translations")
  --keyName [prop]         Name of property containing the key for translation (default: "key")
  --cwd [cwd]              Current working directory
  -h, --help               display help for command

Javascript API

This package can also be used by it's javascript API.

const {
    extractTranslations,
} = require('translation-files');

// with default settings:
extractTranslations();

// or with custom settings:
extractTranslations({
    globOptions: {
        cwd: __dirname, // default process.cwd()
    },
    globPattern, // default "**/**/translations.ts"
    processOptions: {
        translationKeyProperty: "label", // default "key"
        translationExport: "labels", // default "translations"
    },
    outFile: "dist/labels.txt", // default undefined (output in console)
});