-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Pisyukaev/readmes
- Loading branch information
Showing
3 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# xml-locales | ||
|
||
This repository created to work with localization files in xml and simplified usual jobs like as add, update or remove strings in several XML files. | ||
|
||
## Core package - 📦xml-locales | ||
|
||
This is core package to work with localization files in xml. | ||
|
||
## CLI - 💻@xml-locales/cli | ||
|
||
This is cli tool to work with localization files in xml. | ||
|
||
> [!IMPORTANT] | ||
> This packages, which works with XML files, has one root node named `resources`. This root node has child nodes named `string`. For example: | ||
> | ||
> ```xml | ||
> <resources> | ||
> <string name="key1">value1</string> | ||
> <string name="key2">value2</string> | ||
> </resources> | ||
>``` | ||
## Packages | ||
| Package | version | | ||
| ------- | -------- | | ||
| 📦[xml-locales](https://github.com/Pisyukaev/xml-locales/tree/master/packages/xml-locales) | [![NPM](https://img.shields.io/npm/v/xml-locales.svg)](https://www.npmjs.com/package/xml-locales) | | ||
| 💻[@xml-locales/cli](https://github.com/Pisyukaev/xml-locales/tree/master/packages/cli) | [![NPM](https://img.shields.io/npm/v/@xml-locales/cli.svg)](https://www.npmjs.com/package/@xml-locales/cli) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# @xml-locales/cli | ||
|
||
[![NPM](https://img.shields.io/npm/v/xml-locales.svg)](https://www.npmjs.com/package/xml-locales) | ||
|
||
This is cli tool to work with localization files in xml. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install -g @xml-locales/cli | ||
``` | ||
|
||
```sh | ||
yarn global add @xml-locales/cli | ||
``` | ||
|
||
```sh | ||
pnpm add -g @xml-locales/cli | ||
``` | ||
|
||
> [!IMPORTANT] | ||
> This package, which works with XML files, has one root node named `resources`. This root node has child nodes named `string`. For example: | ||
> | ||
> ```xml | ||
> <resources> | ||
> <string name="key1">value1</string> | ||
> <string name="key2">value2</string> | ||
> </resources> | ||
>``` | ||
### Or use without | ||
```sh | ||
npx @xml-locales/cli <command> [...args] | ||
``` | ||
## Commands | ||
|
||
### Add Command | ||
|
||
The `add` command is used to add a new key-value pair to the XML localization file or files from directory. | ||
|
||
```sh | ||
xml-locales add --path path/to/file_or_directory --key newKey --value newValue | ||
``` | ||
|
||
| Flag | Alias | Default | Description | | ||
|---------|-------|---------|-----------------------------------------------------------------------------| | ||
| `--path`| `-p` | `process.cwd()` | The path to the XML localization file or directory where the key-value pair will be added. | | ||
| `--key` | `-k` | None | The new key to be added. | | ||
| `--value`| `-v` | None | The value to be associated with the new key. | | ||
|
||
### Remove Command | ||
|
||
The `remove` command is used to remove a key-value pair from the XML localization file or files from directory. | ||
|
||
```sh | ||
xml-locales remove --path path/to/file_or_directory --key keyToRemove --value valueToRemove | ||
``` | ||
|
||
| Flag | Alias | Default | Description | | ||
|---------|-------|---------|-----------------------------------------------------------------------------| | ||
| `--path`| `-p` | `process.cwd()` | The path to the XML localization file or directory where the key-value pair will be removed. | | ||
| `--key` | `-k` | None | Removed string by the key. | | ||
| `--value`| `-v` | None | Or removed string by the value. | | ||
|
||
### Update Command | ||
|
||
The `update` command is used to update a key-value pair in the XML localization file or files from directory. | ||
|
||
```sh | ||
xml-locales update --path path/to/file_or_directory --oldValue oldValue --newValue newValue | ||
``` | ||
|
||
| Flag | Alias | Default | Description | | ||
|------------|-------|---------|-----------------------------------------------------------------------------| | ||
| `--path` | `-p` | `process.cwd()` | The path to the XML localization file or directory where the key-value pair will be updated. | | ||
| `--oldValue` | `-o` | None | The old key or value to be updated. | | ||
| `--newValue`| `-n` | None | The new key or value to replace the old one. | | ||
|
||
### Sort Command | ||
|
||
The `sort` command is used to sort the keys in the XML localization file or directory in ascending or descending order. | ||
|
||
```sh | ||
xml-locales sort --path path/to/file_or_directory --direction asc_or_desc | ||
``` | ||
|
||
| Flag | Alias | Default | Description | | ||
|------------|-------|---------|-----------------------------------------------------------------------------| | ||
| `--path` | `-p` | `process.cwd()` | The path to the XML localization file or directory where the keys will be sorted. | | ||
| `--direction` | `-d` | 'asc' | The sort direction. Can be 'asc' for ascending order or 'desc' for descending order. | | ||
|
||
## Packages | ||
|
||
| Package | version | | ||
| ------- | -------- | | ||
| 📦[xml-locales](https://github.com/Pisyukaev/xml-locales/tree/master/packages/xml-locales) | [![NPM](https://img.shields.io/npm/v/xml-locales.svg)](https://www.npmjs.com/package/xml-locales) | | ||
| 💻[@xml-locales/cli](https://github.com/Pisyukaev/xml-locales/tree/master/packages/cli) | [![NPM](https://img.shields.io/npm/v/@xml-locales/cli.svg)](https://www.npmjs.com/package/@xml-locales/cli) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
# xml-locales | ||
|
||
[![NPM](https://img.shields.io/npm/v/xml-locales.svg)](https://www.npmjs.com/package/xml-locales) | ||
|
||
This is core package to work with localization files in xml. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install xml-locales | ||
``` | ||
|
||
```sh | ||
yarn add xml-locales | ||
``` | ||
|
||
```sh | ||
pnpm add xml-locales | ||
``` | ||
|
||
> [!IMPORTANT] | ||
> This package, which works with XML files, has one root node named `resources`. This root node has child nodes named `string`. For example: | ||
> | ||
> ```xml | ||
> <resources> | ||
> <string name="key1">value1</string> | ||
> <string name="key2">value2</string> | ||
> </resources> | ||
>``` | ||
## Properties | ||
| Args | Type | Required | Description | | ||
|------|------|----------|-------------| | ||
| `xmlData` | `string` \| `Buffer` \| `XmlJsonData` | `false` | Data of the xml document | | ||
| `xmlOptions` | `object` | `false` | It represents the options for the XML parser. | ||
### xmlOptions | ||
The `xmlOptions` is used to customize the behavior of the XML parser and builder in the `XmlLocales` class. | ||
| Prop | Type | Required | Description | | ||
|------|------|----------|-------------| | ||
| `parserOptions` | `X2jOptionsOptional` | `false` | Options to customize how the XML data is parsed. See below for details. For more information, see [here](https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md) | | ||
| `builderOptions`| `XmlBuilderOptionsOptional` | `false` | Options to customize how the XML data is built. See below for details. For more information, see [here](https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/3.XMLBuilder.md) | | ||
| `formateOptions`| `XMLFormatterOptions` | `false` | Options to customize the formatting of the XML data. See below for details. For more information, see [here](https://github.com/chrisbottin/xml-formatter#options) | | ||
### Default Parser Options (X2jOptionsOptional) | ||
| Option | Value | | ||
|-----------------------|--------| | ||
| `trimValues` | `false` | | ||
| `ignoreDeclaration` | `true` | | ||
| `attributeNamePrefix` | `'key_'` | | ||
| `alwaysCreateTextNode`| `true` | | ||
| `ignoreAttributes` | `false` | | ||
### Default Builder Options (XmlBuilderOptionsOptional) | ||
| Option | Value | | ||
|-----------------------|--------| | ||
| `ignoreAttributes` | `false` | | ||
| `attributeNamePrefix` | `'key_'` | | ||
| `processEntities` | `false` | | ||
### Default Formatter Options (XMLFormatterOptions) | ||
| Option | Value | | ||
|------------------|--------| | ||
| `collapseContent`| `true` | | ||
| `indentation` | `' '` | | ||
## Usage | ||
```js | ||
import {XmlLocales} from 'xml-locales' | ||
const xmlData = ` | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources> | ||
` | ||
const xmlLocales = new XmlLocales(xmlData) | ||
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'}).toXML() | ||
console.log(jsonData) | ||
``` | ||
Output: | ||
|
||
```xml | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
<string name="newKey">newValue</string> | ||
</resources> | ||
``` | ||
|
||
### Update key/value | ||
|
||
```js | ||
import {XmlLocales} from 'xml-locales' | ||
|
||
const xmlData = ` | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources> | ||
` | ||
|
||
const xmlLocales = new XmlLocales(xmlData) | ||
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'}) | ||
.update({oldValue: 'key1', newValue: 'firstKey'}) | ||
.update({oldValue: 'value2', newValue: 'secondValue'}) | ||
.toXML() | ||
|
||
console.log(jsonData) | ||
``` | ||
|
||
Output: | ||
|
||
```xml | ||
<resources> | ||
<string name="firstKey">value1</string> | ||
<string name="key2">secondValue</string> | ||
</resources> | ||
``` | ||
|
||
### Delete by key/value | ||
|
||
```js | ||
import {XmlLocales} from 'xml-locales' | ||
|
||
const xmlData = ` | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources> | ||
` | ||
|
||
const xmlLocales = new XmlLocales(xmlData) | ||
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'}) | ||
.deleteByKey('key1') | ||
.deleteByValue('value2') | ||
.toXML() | ||
|
||
console.log(jsonData) | ||
``` | ||
|
||
Output: | ||
|
||
```xml | ||
<resources> | ||
</resources> | ||
``` | ||
|
||
### XML and JSON | ||
|
||
```js | ||
import {XmlLocales} from 'xml-locales' | ||
|
||
const xmlData = ` | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources> | ||
` | ||
|
||
const xmlLocales = new XmlLocales(xmlData) | ||
const xmlString = xmlLocales.toXML() | ||
const jsonXml = xmlLocales.toJSON() | ||
|
||
console.log(xmlString) // output XML | ||
console.log(jsonXml) // output JSON | ||
``` | ||
|
||
Output XML: | ||
|
||
```xml | ||
<resources> | ||
<string name="newKey2">newValue2</string> | ||
<string name="newKey">newValue</string> | ||
<string name="key2">value2</string> | ||
<string name="firstKey">value1</string> | ||
</resources> | ||
``` | ||
|
||
Output JSON: | ||
|
||
```json | ||
{ | ||
resources: { | ||
string: [ | ||
{ | ||
key_name: 'key1', | ||
'#text': 'value1' | ||
}, | ||
{ | ||
key_name: 'key2', | ||
'#text': 'value2' | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
### Chaining | ||
|
||
```js | ||
import {XmlLocales} from 'xml-locales' | ||
|
||
const xmlData = ` | ||
<resources> | ||
<string name="key1">value1</string> | ||
<string name="key2">value2</string> | ||
</resources> | ||
` | ||
|
||
const xmlLocales = new XmlLocales(xmlData) | ||
|
||
const jsonData = xmlLocales.add({key: 'newKey', value: 'newValue'}) | ||
.add({key: 'newKey2', value: 'newValue2'}) | ||
.update({oldValue: 'key1', newValue: 'firstKey'}) | ||
.sort('desc') | ||
.toXML() | ||
|
||
console.log(jsonData) | ||
``` | ||
|
||
Output: | ||
|
||
```xml | ||
<resources> | ||
<string name="newKey2">newValue2</string> | ||
<string name="newKey">newValue</string> | ||
<string name="key2">value2</string> | ||
<string name="firstKey">value1</string> | ||
</resources> | ||
``` | ||
|
||
## Packages | ||
|
||
| Package | version | | ||
| ------- | -------- | | ||
| 📦[xml-locales](https://github.com/Pisyukaev/xml-locales/tree/master/packages/xml-locales) | [![NPM](https://img.shields.io/npm/v/xml-locales.svg)](https://www.npmjs.com/package/xml-locales) | | ||
| 💻[@xml-locales/cli](https://github.com/Pisyukaev/xml-locales/tree/master/packages/cli) | [![NPM](https://img.shields.io/npm/v/@xml-locales/cli.svg)](https://www.npmjs.com/package/@xml-locales/cli) | |