xml-locales
v1.0.0
Published
Tool for locales in xml files
Downloads
23
Readme
xml-locales
This is core package to work with localization files in xml.
Installation
npm install xml-localesyarn add xml-localespnpm add xml-locales[!IMPORTANT] This package, which works with XML files, has one root node named
resources. This root node has child nodes namedstring. For example:<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 |
| builderOptions| XmlBuilderOptionsOptional | false | Options to customize how the XML data is built. See below for details. For more information, see here |
| formateOptions| XMLFormatterOptions | false | Options to customize the formatting of the XML data. See below for details. For more information, see here |
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
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({keys: ['newKey'], values: ['newValue']}).toXML()
console.log(jsonData)Output:
<resources>
<string name="key1">value1</string>
<string name="key2">value2</string>
<string name="newKey">newValue</string>
</resources>Update key/value
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({keys: ['newKey'], values: ['newValue']})
.update({oldValues: ['key1'], newValues: ['firstKey']})
.update({oldValues: ['value2'], newValues: ['secondValue']})
.toXML()
console.log(jsonData)Output:
<resources>
<string name="firstKey">value1</string>
<string name="key2">secondValue</string>
</resources>Delete by key/value
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({keys: ['newKey'], values: ['newValue']})
.deleteByKey('key1')
.deleteByValue('value2')
.toXML()
console.log(jsonData)Output:
<resources>
</resources>XML and JSON
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 JSONOutput 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:
{
"resources": {
"string": [
{
"key_name": "key1",
"#text": "value1"
},
{
"key_name": "key2",
"#text": "value2"
}
]
}
}Chaining
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({keys: ['newKey'], values: ['newValue']})
.add({keys: ['newKey2'], values: ['newValue2']})
.update({oldValues: ['key1'], newValues: ['firstKey']})
.sort('desc')
.toXML()
console.log(jsonData)Output:
<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 | |
| 💻@xml-locales/cli |
|
