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

lang-packer

v1.0.10

Published

Lang files packer/unpacker

Downloads

49

Readme

Overview

This module helps to collect different project lang files into one large language file (this is packing) and then parse it back for separate small files (and this is unpacking).

This utility is designed to simplify general translation of modular applications.

It can be used as a cmd module or as a separate library.

Installation

  • npm i lang-packer - for in project use (as a library)
  • npm i lang-packer -g - to install it globaly and use from command line

Usage

Packing

For this purpose we should use a LangPacker class from lang-packer module.

The constructor takes three parameters:

  • input folder path - string - the folder in which LangPacker should search for language files. The base language folder should be the .lang folder. All files with .json extension will be extracted as a language files. See below for example of correct folder structure.
  • output folder path - string - the folder for resulting language files.
  • options - object - available options:
  • prefix - output lang file name prefix. Default is "".
  • postfix - output lang file name postfix. Default is ""
  • extension - output lang file name extension. Default is .lng;

The name of the language file is built according to the following pattern:

[prefix]<lang code>[postfix][extension]

For options:

{
    "prefix": "test",
    "postfix": "file",
    "extension": ".lng"
}

the output lang files will be look like this:

test0000file.lng
test0413file.lng
test0123file.lng

Folder structure example:

- /src/
 - /classes/
  - /.lang/
   - en-EN.json
   - ru-RU.json
 - /components/
  - /.lang/
   - /componentA/
    - en-EN.json
    - ru-RU.json
 - /lang/
  - ...
 - /languages/
  - ...

LangPacker will collect all .json files from /src/classes/.lang and /src/components/.lang (include subdirectories), but no /lang and /languages folders.

The language file's name should be in the format: <Lang tag>.json

Examples:

en-EN.json
nl-NL.json
es-US.json

The full list of available tags can be found here

Usage example

const { LangPacker } = require("lang-packer");

const packer = new LangPacker("./src/", "./lang-out/", {
    "prefix": "test",
    "postfix": "file",
    "extension": ".lng"
});

packer.do();

Using from cmd-line

If you install lang-packer module globaly you can run packlng command from it.

packlng command supports the following options:

-i - input folder path; -o - output folder path; -pr - prefix option; -po - postfix option; -e - extension option.

Also you can use packlng -h command to see the full list of available options;

Example call: packlng -i "./src/" -o "./lng-out" -e ".lng" -pr "language"

It will collect all lang files from ./src/ folder (relative to the current folder) and place it to ./lng-out/ folder (also relative). If outout folder does not exist the packer will create it.

Unpacking

For this purpose we should use a LangUnpacker class from lang-packer module.

The constructor takes three parameters:

  • input folder path - string - the folder containing lang files, generated by LangPacker.
  • output folder path - string - the folder into which LangUnpacker will parse the language files. Usually this is the same folder from which LangPacker collected these files. (./src/ in out example)
  • options - object - available options:
  • prefix - output lang file name prefix. Default is "".
  • postfix - output lang file name postfix. Default is ""
  • extension - output lang file name extension. Default is .lng;
  • index - whether to generate index.js file

This options should be relevant to previously generated lang files. Another words they should be equal with options, that was passed to LangPacker for henerating this lang files.

Unpacking process:

LangUnpacker will parse each lang file from input folder and create a set of language files. The result language files structure will be the same as structure, from which it was packed.

It means that all languages files will rewriten source language files if LangPacker input folder will be the same as LangUnpacker output folder.

Usage example

const { LangUnpacker } = require("lang-packer");

const packer = new LangPacker("./lang-out/", "./src/", {
    "prefix": "test",
    "postfix": "file",
    "extension": ".lng"
});

packer.do();

This example assumes that we are running it in the same folder as the example for LangPacker

Using from cmd-line

If you install lang-packer module globaly you can run unpacklng command from it.

packlng command supports the following options:

-i - input folder path; -o - output folder path; -pr - prefix option; -po - postfix option; -e - extension option. -in - should generate index.js file in each .lang folder -s - should file parser skip empty values in result output files

Also you can use unpacklng -h command to see the full list of available options;

Example call: unpacklng -o "./src/" -i "./lng-out" -e ".lng" -pr "language" -in

It will parse all lang files from ./lng-out/ folder (relative to the current folder) and place it to ./src/ folder acording to elemnt's paths. If output folders does not exist the unpacker will create them.