1.0.0 • Published 3 months ago

xml-locales v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
3 months ago

xml-locales

NPM

This is core package to work with localization files in xml.

Installation

npm install xml-locales
yarn add xml-locales
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:

<resources>
  <string name="key1">value1</string>
  <string name="key2">value2</string>
</resources> 

Properties

ArgsTypeRequiredDescription
xmlDatastring | Buffer | XmlJsonDatafalseData of the xml document
xmlOptionsobjectfalseIt 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.

PropTypeRequiredDescription
parserOptionsX2jOptionsOptionalfalseOptions to customize how the XML data is parsed. See below for details. For more information, see here
builderOptionsXmlBuilderOptionsOptionalfalseOptions to customize how the XML data is built. See below for details. For more information, see here
formateOptionsXMLFormatterOptionsfalseOptions to customize the formatting of the XML data. See below for details. For more information, see here

Default Parser Options (X2jOptionsOptional)

OptionValue
trimValuesfalse
ignoreDeclarationtrue
attributeNamePrefix'key_'
alwaysCreateTextNodetrue
ignoreAttributesfalse

Default Builder Options (XmlBuilderOptionsOptional)

OptionValue
ignoreAttributesfalse
attributeNamePrefix'key_'
processEntitiesfalse

Default Formatter Options (XMLFormatterOptions)

OptionValue
collapseContenttrue
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 JSON

Output 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

Packageversion
📦xml-localesNPM
💻@xml-locales/cliNPM
1.0.0

3 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago

0.0.0

6 months ago