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-loader

v1.1.2

Published

Webpack loader that localizes HTML templates and JSON files by injecting translated content.

Downloads

47

Readme

translation-loader

Version Downloads Try on RunKit

Webpack loader that localizes HTML templates and JSON files by injecting translated content, replacing the original content that was previously exported for translation.

This leverages the core functionality of gulp-translate, allowing that exact same workflow to be used in a Webpack build process, with no dependency on Gulp.

Please refer to the documentation for gulp-translate for details on capabilities and configuration.

What about the export?

While this loader handles the import of translations, you will also need to export your content for translation. The recommended way to do that, is to either use a Gulp task and gulp-translate, or if you do not wish to take a dependency on Gulp, to write a simple script that uses the core export functionality of gulp-translate directly.

Example

The following example illustrates how this loader may be used in a Webpack configuration, as well as how a package.json script for exporting content may be written. Note that the code shown here also exists as a working example in the repository for this package.

webpack.config.js

Let's say you have a Webpack configuration that looks like the example below. Here the templates and JSON content files are piped through the translation-loader, before being passed to the regular loaders.

Note that Webpack applies the loaders from right to left, so the order shown here is correct.

Note that Webpack has built in support for JSON files, which is why no JSON loader is needed.

const path = require("path");
const translateConfig = require("./translate-config");

/**
 * The Webpack configuration.
 */
const webpackConfig =
{
    entry: "./source/entry.js",
    output:
    {
        path: path.resolve("./artifacts"),

        // This will be set based on the locale for which we are building.
        filename: undefined
    },
    module:
    {
        rules:
        [
            {
                test: /\.html$/,
                use:
                [
                    { loader: "html-loader" },
                    { loader: "translation-loader", options: translateConfig }
                ]
            },
            {
                test: /[/\\]content\.json$/,
                use:
                [
                    { loader: "translation-loader", options: translateConfig }
                ]
            }
        ]
    }
};

/**
 * Creates the Webpack configuration based on the command line arguments.
 * @param env An object representing the specified 'env' arguments, or undefined.
 * @returns The Webpack configuration to use.
 */
module.exports = function(env)
{
    if (env && env.locale)
    {
        // To build for the specified locale, set the import file path.
        translateConfig.importFilePath =
            translateConfig.importFilePath.replace("{locale}", env.locale);

        // Set the output file name so we get a bundle for each locale.
        webpackConfig.output.filename = `bundle.${env.locale}.js`;
    }
    else
    {
        // To build for the base locale without an import file, exclude all files.
        translateConfig.excludedFilePaths = ["**"];

        // Set the output file name so we get a bundle for each locale.
        webpackConfig.output.filename = "bundle.en-US.js";
    }

    return webpackConfig;
};

translate-export.js

While Webpack handles the import and build, you'll also need a way to export the content from your templates and JSON files, so it can be sent off for translation. This can be done using a script like the example below.

const fs = require("fs");
const globs = require("globs");
const translatePlugin = require("gulp-translate/lib/plugin/plugin");
const translateConfig = require("./translate-config");

// Get the source file paths.
const filePaths = globs.sync(translateConfig.includedFilePaths,
{
    ignore: translateConfig.excludedFilePaths
});

// Create the export task.
const plugin = new translatePlugin.Plugin(translateConfig);
const task = plugin.export(translateConfig);

// Process the source files.
for (let filePath of filePaths)
{
    const fileContents = fs.readFileSync(filePath);
    const file = { contents: fileContents, path: filePath };

    task.process(file);
}

// Finalize the export task.
task.finalize();

Depending on your workflow, you could consider automatically uploading the export file to your translation service of choice, and maybe have a similar script you can run to download the translations once they are ready.

translate-config.js

This file contains the configuration used during both content export translation import. Please refer to the documentation for gulp-translate for details.

/**
 * The configuration used during content export and translation import.
 * Note that paths must be absolute or relative to the current working directory.
 */
module.exports =
{
    // Options for 'gulp-translate'.
    // Note how the plugin and command options are all in the same object,
    // instead of being separated as in the 'gulp-translate' documentation.

    normalizeContent: true,
    prefixIdsInContentFiles: true,
    baseFilePath: "./source",

    // The path to the export file to which content should be exported.
    exportFilePath: "./translation/export/translate.json",

    // The path to the import file from which content should be imported,
    // where '{locale}' should be replaced with the locale code.
    importFilePath: "./translation/import/{locale}.json",

    // Options for 'translation-loader' and the export script.

    /**
     * An array of glob patterns matching files that should be included in the export.
     * Make sure this matches the tests guarding the use of the 'translation-loader' in your
     * Webpack configuration.
     */
    includedFilePaths:
    [
        "./source/**/*.html",
        "./source/**/content.json"
    ],

    /**
     * An array of glob patterns matching files that should be excluded from import and export.
     * Use this to exclude files related to features that are not yet ready for translation.
     * Default is undefined.
     */
    excludedFilePaths:
    [
        "./source/excluded/**"
    ]
};

How to use

Finally, to make your tasks more discoverable, you can add something like the following to your package.json file.

"scripts":
{
  "build": "webpack",
  "translate-export": "node translate-export",
  "translate-import": "node translate-import"
}

With this in place, you can execute the following commands:

  • npm run build This will produce a build for the base locale, which could be e.g. en-US.

  • npm run build --env.locale=en-GB This will produce a build localized for the en-GB locale, where the contents of your templates and JSON files is replaced with the translated content.

  • npm run translate-export This will export the contents of your templates and JSON files into a file that can be sent to translators, who then produce the import files needed during the build.

  • npm run translate-import This would be a convenient way to download the lates translations into your repository.