# lang-packer

> Lang files packer/unpacker

Latest version **1.0.10** (published 2024-05-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install lang-packer
pnpm add lang-packer
yarn add lang-packer
bun add lang-packer
```

Provides the commands `packlng`, `unpacklng`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2024-05-02 |
| First published | 2020-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 30.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Yohanson555 |
| Maintainers | yohanson555 |

## Links

- npm: https://www.npmjs.com/package/lang-packer
- Repository: https://github.com/Yohanson555/lang-packer
- Homepage: https://github.com/Yohanson555/lang-packer#readme
- Issues: https://github.com/Yohanson555/lang-packer/issues
- npm.io page: https://npm.io/package/lang-packer

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.17.20
- [commander](https://npm.io/package/commander.md) ^6.2.1
- [langcode-info](https://npm.io/package/langcode-info.md) ^1.1.4

## Recent versions

- 1.0.10 (latest) — 2024-05-02
- 1.0.9 — 2024-05-02
- 1.0.8 — 2024-01-16
- 1.0.7 — 2023-08-27
- 1.0.6 — 2023-03-07
- 1.0.5 — 2023-03-07
- 1.0.4 — 2023-02-05
- 1.0.3 — 2023-02-05
- 1.0.2 — 2020-12-17
- 1.0.1 — 2020-12-16
- 1.0.0 — 2020-12-16

## 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](https://github.com/Yohanson555/langcode-info/blob/main/src/data/code-to-hex.json)

### 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.

---
_Source: https://npm.io/package/lang-packer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
