lang-packer

1.0.8 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i lang-packer

Weekly Downloads

3

Version

1.0.8

License

MIT

Unpacked Size

33.1 kB

Total Files

37

Last publish

Collaborators

  • yohanson555