Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
i18n-transform
Advanced tools
apply i18n transforms to a json object.
.transform({source}, [languages]);
The source object to be transformed must include an i18n
field, with the following schema:
{
...
"i18n": [
{
...
"language": {
"code": "en", // two letter language code
"region": "GB" // iso8601 region code
},
...
},
...
],
...
}
The languages array should be an array of objects with the following schema:
[
{
"code": "en",
"region": "GB",
"quality": 1.0
}
]
If language selection fails (no matching languages), then the transform returns null.
.transformDestination({source object}, {destination object}, [languages], callback function(err));
This method does the same as the above, but it transforms the destination object with language fields from the source object. There is no return value from this method.
If language selection fails (no matching languages), the callback will contain an error.
.transformByField({source}, [languages], {fields});
This method applies the transform on a field by field basis as opposed to comparing a whole i18n
block like the transform
method does.
The fields
object should adhere to the following schema:
{
"required": [
"Name",
"Address"
],
"optional": [
"Description",
"DressCode"
]
}
For required
fields, if a language match fails, the field from the primary language will be used. Optional
fields
will only be returned if a language is explicitly matched.
The return value is different from transform
as it returns the following schema:
{
"translations": {
"Name": "The Fat Duck",
"DressCode": "Salle à manger formelle"
},
"localization": {
"Name": "en-GB" // ISO 639-1 code
"DressCode": "fr-FR" // ISO 639-1 code
}
}
This method will only return values that are specified within the fields
.
If the same field appears in both required
and optional
then the required
value takes precedent.
.transformByFieldDestination({source}, [languages], {fields}, callback function(err));
This method does the same as the above, but it transforms the destination object with the translations
& localization
fields.
There is no return value from this method.
If language selection fails (no matching languages), the callback will contain an error.
This module is designed to work with the accept-language-parser.
npm install --save i18n-transform
var transformer = require("i18n-transform");
var result = transformer.transform(
{
"id": 123,
"someinvariantfield": 45.45,
"i18n": [
{
"name": "the thing",
"somelocalisedfield": "local value",
"language": {
"code": "en",
"region": "GB"
},
"gb-specific-field": "some en-GB value"
},
{
"name": "the thang",
"somelocalisedfield": "local value 2",
"language": {
"code": "en",
"region": "US"
},
"us-specific-field": "some en-US value"
}
]
},
[
{
"code": "en",
"region": "GB",
"quality": 1.0
},
{
"code": "en",
"region": "US",
"quality": 0.8
}
]
);
output:
{
"id": 123,
"someinvariantfield": 45.45,
"name": "the thing",
"somelocalisedfield": "local value",
"language": {
"code": "en",
"region": "GB"
},
"gb-specific-field": "some en-GB value"
}
var transformer = require("i18n-transform");
var result = transformer.transformByField(
{
"id": 123,
"someinvariantfield": 45.45,
"i18n": [
{
"name": "the thing",
"somelocalisedfield": "local value",
"language": {
"code": "en",
"region": "GB"
},
"gb-specific-field": "some en-GB value"
},
{
"name": "the thang",
"somelocalisedfield": "local value 2",
"language": {
"code": "en",
"region": "US"
},
"us-specific-field": "some en-US value"
}
]
},
[
{
"code": "en",
"region": "GB",
"quality": 1.0
},
{
"code": "en",
"region": "US",
"quality": 0.8
}
],
{
"required":{
"name",
"somelocalisedfield",
},
"optional":{
"us-specific-field"
}
}
);
output:
{
"translations": {
"name": "the thing",
"somelocalisedfield": "local value",
"us-specific-field": "some en-US value"
},
"localization":{
"name": "en-GB",
"somelocalisedfield": "en-GB",
"us-specific-field": "en-US"
}
}
FAQs
apply an i18n transform to a json object
The npm package i18n-transform receives a total of 4 weekly downloads. As such, i18n-transform popularity was classified as not popular.
We found that i18n-transform demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.