npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

i18n-transform

v2.1.3

Published

apply an i18n transform to a json object

Downloads

7

Readme

i18n-transform

Build Status NPM version Dependencies

apply i18n transforms to a json object.

Methods

.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.

Note

This module is designed to work with the accept-language-parser.

Installation:

npm install --save i18n-transform

Usage (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"
}

Usage (transformByField) :

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"
    }
}