Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xliff

Package Overview
Dependencies
Maintainers
2
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xliff - npm Package Compare versions

Comparing version 3.0.1 to 4.0.0

.npmignore

10

js2xliff.js
const convert = require('xml-js');
const makeElement = require('./util/makeNodes').makeElement;
const makeText = require('./util/makeNodes').makeText;
const ElementTypes2 = require('./inline-elements/ElementTypes2');
const makeElement = require('./xml-js/objectToXml').makeElement;
const makeText = require('./xml-js/objectToXml').makeText;
const makeValue = require('./xml-js/objectToXml').makeValue;
const escape = require('./util/escape');

@@ -31,4 +33,4 @@

const segment = makeElement('segment', null, true);
segment.elements.push(makeElement('source', null, [makeText(obj.resources[nsName][k].source)]));
segment.elements.push(makeElement('target', null, [makeText(obj.resources[nsName][k].target)]));
segment.elements.push(makeElement('source', null, makeValue(obj.resources[nsName][k].source, ElementTypes2)));
segment.elements.push(makeElement('target', null, makeValue(obj.resources[nsName][k].target, ElementTypes2)));
if ('note' in obj.resources[nsName][k]) {

@@ -35,0 +37,0 @@ segment.elements.push(makeElement('note', null, [makeText(obj.resources[nsName][k].note)]));

const convert = require('xml-js');
const makeElement = require('./util/makeNodes').makeElement;
const makeText = require('./util/makeNodes').makeText;
const ElementTypes12 = require('./inline-elements/ElementTypes12');
const makeElement = require('./xml-js/objectToXml').makeElement;
const makeText = require('./xml-js/objectToXml').makeText;
const makeValue = require('./xml-js/objectToXml').makeValue;
const escape = require('./util/escape');

@@ -38,4 +40,4 @@

const u = makeElement('trans-unit', {id: escape(k)}, true);
u.elements.push(makeElement('source', null, [makeText(obj.resources[nsName][k].source)]));
u.elements.push(makeElement('target', null, [makeText(obj.resources[nsName][k].target)]));
u.elements.push(makeElement('source', null, makeValue(obj.resources[nsName][k].source, ElementTypes12)));
u.elements.push(makeElement('target', null, makeValue(obj.resources[nsName][k].target, ElementTypes12)));
if ('note' in obj.resources[nsName][k]) {

@@ -42,0 +44,0 @@ u.elements.push(makeElement('note', null, [makeText(obj.resources[nsName][k].note)]));

6

package.json
{
"name": "xliff",
"version": "3.0.1",
"version": "4.0.0",
"description": "xliff2js and js2xliff converter xliff utils",

@@ -10,3 +10,3 @@ "main": "index.js",

"dependencies": {
"xml-js": "1.6.2"
"xml-js": "1.6.7"
},

@@ -20,3 +20,3 @@ "devDependencies": {

"lint": "eslint .",
"test": "npm run lint && mocha test"
"test": "npm run lint && mocha test/**"
},

@@ -23,0 +23,0 @@ "keywords": [

@@ -19,3 +19,3 @@ [![travis](https://img.shields.io/travis/locize/xliff.svg)](https://travis-ci.org/locize/xliff) [![npm](https://img.shields.io/npm/v/xliff.svg)](https://npmjs.org/package/xliff)

Or you can direclty `require()` its functions:
Or you can directly `require()` its functions:

@@ -220,1 +220,248 @@ ```js

```
### Using Inline Elements
XLIFF 1.2 and 2.x support the use of a set of XML elements within `source` and `target` declarations. In general these "inline" tags exist to specify special elements within translation strings. For example, in XLIFF 1.2 the `<ph>..</ph>` element is used to define a "placeholder" such as for a variable value that is substituted at runtime, e.g.:
- String: "Hello there, {fullName}"
- XLIFF 1.2: `<source>Hello there, <ph>{fullName}</ph></source>`
In the standard case described previously, the `source` and `target` values are string instances. A `source` or `target` value can also be defined as an Array instance.
```
// Simple value:
"source": "Hello there"
// Value with multiple child elements:
"source": ["Hello ", "there"]
```
(Note that in this example there's no benefit from splitting the string into two strings wrapped in an array.)
When the `source` and `target` values are Array instances, the elements of the Array contain strings (representing plain text) or objects (representing XLIFF inline elements). The structure for those objects is described next.
#### Inline element object structure
An object representing an inline element has the following structure:
```
{
[<Element Type>]: {
"id": "<Value>",
"contents": "<Element Contents>",
"<Other Property 1>": "<Other Property 1 Value>",
...
"<Other Property N>": "<Other Property N Value>"
}
}
```
The parts are:
- `<Element Type>`: A string (used as a property name) indicating the element type.
- `id` property: The value of the XLIFF element's `id` attribute
- `contents` property: The contents of the XLIFF element, if supported. This value can be a string or array and is treated like the `source`/`target` values.
- All other properties: Map directly to attributes of the XLIFF element tag
Here's a real-world example:
```
{
"Span": {
"id": "dataType",
"contents": "{dataType}",
"ctype": "x-python-brace-param"
}
}
```
This maps to the following XLIFF inline element structure:
```
<ph id="dataType" ctype="x-python-brace-param">{dataType}</ph>
```
#### Full inline element example
The following code shows a full object structure for one type of XLIFF inline element (Generic span), and the corresponding XLIFF 1.2 and XLIFF 2.0 that it produces. For other examples of different element types, see [the inline element test fixtures](./test/fixtures/inline-elements)
##### Strings
```
key1:
source: "Hello {name}"
target: "Hallo {name}"
key2:
source: "An application to manipulate and process {doctype} documents."
target: "Eine Applikation um {doctype} Dokumente zu manipulieren und verarbeiten"
```
##### JSON
```
{
"resources": {
"namespace1": {
"key1": {
"source": [
"Hello ",
{
"GenericSpan": {
"id": "name",
"ctype": "x-python-brace-param",
"contents": "{name}"
}
}
],
"target": [
"Hallo ",
{
"GenericSpan": {
"id": "name",
"ctype": "x-python-brace-param",
"contents": "{name}"
}
}
]
},
"key2": {
"source": [
"An application to manipulate and process ",
{
"GenericSpan": {
"id": "doctype",
"ctype": "x-python-brace-param",
"contents": "{doctype}"
}
},
" documents"
],
"target": [
"Eine Applikation um ",
{
"GenericSpan": {
"id": "doctype",
"ctype": "x-python-brace-param",
"contents": "{doctype}"
}
},
" Dokumente zu manipulieren und verarbeiten"
]
}
}
},
"sourceLanguage": "en-US",
"targetLanguage": "de-CH"
}
```
##### XLIFF 1.2
```
<xliff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd" xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="namespace1" datatype="plaintext" source-language="en-US" target-language="de-CH">
<body>
<trans-unit id="key1">
<source>Hello
<g id="name" ctype="x-python-brace-param">{name}</g>
</source>
<target>Hallo
<g id="name" ctype="x-python-brace-param">{name}</g>
</target>
</trans-unit>
<trans-unit id="key2">
<source>An application to manipulate and process
<g id="doctype" ctype="x-python-brace-param">{doctype}</g> documents
</source>
<target>Eine Applikation um
<g id="doctype" ctype="x-python-brace-param">{doctype}</g> Dokumente zu manipulieren und verarbeiten
</target>
</trans-unit>
</body>
</file>
</xliff>
```
##### XLIFF 2.0
```
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="de-CH">
<file id="namespace1">
<unit id="key1">
<segment>
<source>Hello
<pc id="name" ctype="x-python-brace-param">{name}</pc>
</source>
<target>Hallo
<pc id="name" ctype="x-python-brace-param">{name}</pc>
</target>
</segment>
</unit>
<unit id="key2">
<segment>
<source>An application to manipulate and process
<pc id="doctype" ctype="x-python-brace-param">{doctype}</pc> documents
</source>
<target>Eine Applikation um
<pc id="doctype" ctype="x-python-brace-param">{doctype}</pc> Dokumente zu manipulieren und verarbeiten
</target>
</segment>
</unit>
</file>
</xliff>
```
#### Supported inline element types
XLIFF 1.2 and XLIFF 2.x define different sets of inline elements. However, the underlying semantics of many of the elements are the same and they can be mapped to each other. The supported element types are:
| Element type | Use Case | Representation (1.2) | Representation (2.0) |
| ------------ | -------- | -------------------- | -------------------- |
| **Generic** |
| Standalone | Standalone code | `<x/>` | `<ph/>` |
| GenericSpan | Well-formed spanning code | `<g></g>` | `<pc></pc>` |
| GenericSpanStart | Start marker of spanning code | `<bx/>` | `<sc/>` |
| GenericSpanEnd | End marker of spanning code | `<ex/>` | `<ec/>` |
| **Native code** (same as generic for 2.0) | | |
| Span | Well-formed spanning code | `<ph></ph>` | `<pc></pc>` |
| SpanStart | Start marker of spanning code | `<bpt></bpt>` | `<sc/>` |
| SpanEnd | End marker of spanning code | `<ept></ept>` | `<ec/>` |
Note that there are additional inline elements defined in the XLIFF specifications that are not supported by this library, and are not listed here.
These types are defined as constants in [inline-elements/ElementTypes.js](./inline-elements/ElementTypes.js)
Although both XLIFF versions define Generic element types, only XLIFF 1.2 defines Native element types. This library uses a "superset" approach to allow for compatibility between its data model and the different XLIFF versions. For example, an object representation of an XLIFF value that includes a `Span` (Native spanning code) is converted to a `<pc>..</pc>` element in XLIFF 2.0, even though XLIFF 2.0 doesn't technically support Native elements.
The rules for mapping between element types are as follows:
JS -> XLIFF 1.2
Elements are written as their corresponding types
JS -> XLIFF 2.0
Elements are written as their corresponding types. Native/generic types are mapped to the same XLIFF element type
XLIFF 1.2 -> JS
Elements are read as their corresponding types
XLIFF 2.0 -> JS
Elements are read as their corresponding (non-generic) types
As a result, you should be able to have "roundtrip" support for converting between JavaScript and XLIFF. The only exception is if an XLIFF 1.2 value is converted to JavaScript, then to XLIFF 2, then back to JavaScript, then to XLIFF 1.2. In that case the Native inline elements will be converted to XLIFF 1.2 Generic elements.
#### Helpers for creating inline element objects
If you need to create your own inline element objects to construct a `source` or `target` array, you can use the `[makeInlineElement](./inline-elements/makeInlineElement.js)` function.
For example, suppose you have this string:
> "Hello {name}"
You want to use it as a `source` value containing two parts -- the string "Hello " and a Generic Span element containing the placeholder variable "{name}", so that the end result (in XLIFF 1.2) should look like this:
```
<source>Hello
<g id="name" ctype="x-python-brace-param">{name}</g>
</source>
```
You can create this structure using the `makeInlineElement()` function with the following code:
```
// import or require makeInlineElements and ElementTypes
// signature: makeInlineElement(type, id, attributes, contents)
var attributesObj = { ctype: 'x-python-brace-param' };
var inlineElementObj = makeInlineElement(ElementTypes.GenericSpan, 'name', attributesObj, '{name}');
var source = [ 'Hello ', inlineElementObj ];
```
const convert = require('xml-js');
const ElementTypes12 = require('./inline-elements/ElementTypes12');
const extractValue = require('./xml-js/xmlToObject').extractValue;

@@ -10,6 +12,2 @@ function xliff12ToJs(str, cb) {

const extractValue = (valueElement) => {
return valueElement.type !== 'text' ? extractValue(valueElement.elements[0]) : valueElement.text;
};
var xmlObj;

@@ -46,3 +44,3 @@ try {

case 'note':
unit[element.name] = element.elements ? extractValue(element.elements[0]) : undefined;
unit[element.name] = extractValue(element.elements, ElementTypes12);
break;

@@ -49,0 +47,0 @@ }

const convert = require('xml-js');
const ElementTypes2 = require('./inline-elements/ElementTypes2');
const extractValue = require('./xml-js/xmlToObject').extractValue;

@@ -10,6 +12,2 @@ function xliffToJs(str, cb) {

const extractValue = (valueElement) => {
return valueElement.type !== 'text' ? extractValue(valueElement.elements[0]) : valueElement.text;
};
var xmlObj;

@@ -44,3 +42,3 @@ try {

case 'note':
unit[element.name] = element.elements ? extractValue(element.elements[0]) : undefined;
unit[element.name] = extractValue(element.elements, ElementTypes2);
break;

@@ -47,0 +45,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc