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

@zakkudo/translate-webpack-plugin

v0.3.0

Published

A webpack plugin for generating localization files using static analysis of source files similar to gettext with code splitting

Downloads

10

Readme

@zakkudo/translate-webpack-plugin

A webpack plugin for scanning javscript files to build translation mappings in json automatically.

Build Status Coverage Status Known Vulnerabilities Node License

Why use this?

  • Automated code splittiing
  • You no longer have to manage hierarchies of translations
  • Designed for architectures leveraging dynamic imports, allowing splitting of the translations based off of file structure
  • Templates are automatically generated for the translators where they only need to fill in the blanks
  • The translations are annoted if they are new or unused as well as the file names and line numbers of usages
  • Easy auditing for missing or non-updated translation strings with never running your application or enlisting QA
  • Any string wrapped in __() or __n() or __p() or __np(), will be picked up as a translatable making usage extremely easy for developers
  • Works similarly to the venerable gettext. Any translation strategies that work for that library work for this library. translatable making usage extremely easy for developers

What does it do?

  • It searches your source code for translatable strings and aggregates them
  • It writes human-centric translation templates in json5 showing usages, new strings and no longer used strings
  • It generates developer-centric optimized json templates, stripping away any unused strings and metadata

Install

# Install using npm
npm install @zakkudo/translate-webpack-plugin
# Install using yarn
yarn add @zakkudo/translate-webpack-plugin

Setup

  1. Wrap strings you want to be translated in __('text') or __n('singlular', 'plural', number) using a library like @zakkudo/translator
  2. Add the plugin to webpack
const TranslateWebpackPlugin = require('@zakkudo/translate-webpack-plugin');

module.exports = {
    plugins: [
        new TranslateWebpackPlugin({
            // Analyzes all javscript files in the src directory, which is a good initial value
            files: 'src/**/*.js',
            // Use verbose output to see what files are parsed, what keys are extracted, and where they are being written to
            debug: true,
            // You do not need to add your default language (which for most people will be English)
            locales: ['fr'],
            // Consolidate all of the optimized localizations into `src/.locale`, good as an initial configuration
            target: 'src'
        })
    ]
};
  1. Add .locales to your .gitignore so it isn't commited. It is a dynamic source file that has no value being added to a repository. Its existance in the src directory is simply to facilitate importing it.
  2. Add find src -name '.locales' | xargs rm -r to your clean scripts for an easy way to remove the auto generated src/.locales from your source code
  3. Import locales into your source code from the src/.locales folder so you can merge it into the lookup of @zakkudo/translator. It is plain old json with the untranslated and unexisting values optimized out.
  4. Have your localization team use the files from locales (without a period.) It's annoted with information about new, unused, and existing usages of the translations to help them audit what needs updating.

You'll end up with a file structure similar to below.

File Structure
├── locales <- For your translators
│   ├── en.json
│   └── fr.json
└── src
    ├── .locales <- For your developers
    │   ├── en.json
    │   └── fr.json
    └── pages
        ├── About
        │   └── index.js
        └── Search
            └── index.js

Where locales/fr.json will look like this for use by your translators:

{
    "About": {
        // NEW
        // src/pages/AboutPage/index.js:14
        "default": ""
    },
    "Search": {
        // UNUSED
        "default": "French translation",
        // UNUSED
        "menuitem": "French translation"
    },
    "There is one user": {
        // src/pages/AboutPage/index.js:40
        "default": {"1":"French translation", "2":"French translation"},
    },
    "Welcome to the about page!": {
        // src/pages/AboutPage/index.js:38
        "default": "French translation"
    }
}

And the optimized src/.locales/fr.json will look like this for use by your developers:

{
    "There is one user": {"one":"French translation", "other":"French translation"},
    "Welcome to the about page!": "French translation"
}

Your developers will use the translation similarly to below:

import Translator from '@zakkudo/translator';
import fr from 'src/.locales/fr.json';
const translator = new Translator();
const {__, __n} = translator;
const language = navigator.language.split('-')[0];

translator.setLocalization('fr', fr);
translator.setLocale(language);

document.title = __('About');
document.body.innerHTML = __n('There is one user', 'There are %d users', 2);

Examples

Configure the plugin to build a single .locales directory

module.exports = {
    plugins: [
        new TranslateWebpackPlugin({
            files: 'src/**/*.js',
            locales: ['es', 'fr'],
            target: 'src'
        });
    ]
};
File Structure
├── locales <- For your translators. Contains translations for everything
│   ├── es.json
│   └── fr.json
└── src
    ├── Application.js
    ├── .locales <- For your developers. Contains translations for everything
    │   ├── es.json
    │   └── fr.json
    └── pages
        ├── About
        │   └── index.js
        └── Search
            └── index.js

Configure the plugin for a split .locales directory

module.exports = {
    plugins: [
        new TranslateWebpackPlugin({
            files: 'src/**/*.js',
            locales: ['es', 'fr'],
            target: 'src/pages/*'
        })
    ]
};
File Structure
├── locales <- For your translators. Contains translations for everything
│   ├── es.json
│   └── fr.json
└── src
    ├── Application.js
    └── pages
        ├── About
        │   ├── .locales <- For your developers. Contains translations for `Application.js` and `About/index.js`
        │   │   ├── es.json
        │   │   └── fr.json
        │   └── index.js
        └── Search
            ├── .locales <- For your developers. Contains translations for `Application.js` and `Search/index.js`
            │   ├── es.json
            │   └── fr.json
            └── index.js

Also see

  • @zakkudo/translation-static-analyzer for the generic library that this package thinly wraps
  • @zakkudo/translator is a library that can read the localization with no fuss and apply the translations.
  • Polymer 3 Starter Project is an example project using this library.

API

@zakkudo/translate-webpack-plugin~TranslateWebpackPlugin ⏏

Plugin for analyzing javascript source files, extracting the translations, and converting them into localization templates.

Kind: Exported class

new TranslateWebpackPlugin(options)

| Param | Type | Default | Description | | --- | --- | --- | --- | | options | Object | | The modifiers for how the analyzer is run | | options.files | String | | A glob pattern of the files to pull translations from | | [options.debug] | Boolean | false | Show debugging information in the console | | [options.locales] | Array.<String> | [] | The locales to generate (eg fr, ja_JP, en) | | [options.templates] | String | | The location to store the translator translatable templates for each language. Defaults to making a locales directory in the current working directory | | [options.target] | String | | Where to write the final translations, which can be split between multiple directories for modularity. If there are no targets, no .locales directory will be generated anywhere. |

translateWebpackPlugin.apply(compiler)

Method called by the webpack plugin system during watch to inform the plugin when some files have been updated.

Kind: instance method of TranslateWebpackPlugin

| Param | Type | Description | | --- | --- | --- | | compiler | Object | The webpack compiler object |