{"id":22059229,"url":"https://github.com/opentable/i18n-transform","last_synced_at":"2025-07-24T00:31:42.125Z","repository":{"id":15563445,"uuid":"18298697","full_name":"opentable/i18n-transform","owner":"opentable","description":"apply i18n transforms to a json object","archived":true,"fork":false,"pushed_at":"2016-09-06T14:49:04.000Z","size":49,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-06-04T22:39:39.738Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/opentable.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-31T15:48:42.000Z","updated_at":"2025-03-24T09:48:20.000Z","dependencies_parsed_at":"2022-09-26T20:31:03.852Z","dependency_job_id":null,"html_url":"https://github.com/opentable/i18n-transform","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/opentable/i18n-transform","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentable%2Fi18n-transform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentable%2Fi18n-transform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentable%2Fi18n-transform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentable%2Fi18n-transform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opentable","download_url":"https://codeload.github.com/opentable/i18n-transform/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opentable%2Fi18n-transform/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266774717,"owners_count":23982246,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-30T17:27:41.468Z","updated_at":"2025-07-24T00:31:41.772Z","avatar_url":"https://github.com/opentable.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# i18n-transform\n[![Build Status](https://travis-ci.org/opentable/i18n-transform.png?branch=master)](https://travis-ci.org/opentable/i18n-transform) [![NPM version](https://badge.fury.io/js/i18n-transform.png)](http://badge.fury.io/js/i18n-transform) ![Dependencies](https://david-dm.org/opentable/i18n-transform.png)\n---\n\napply i18n transforms to a json object.\n\n### Methods\n\n#### `.transform({source}, [languages]);`\n\nThe source object to be transformed must include an `i18n` field, with the following schema:\n\n```\n{\n    ...\n    \"i18n\": [\n        {\n            ...\n            \"language\": {\n                \"code\": \"en\",  // two letter language code\n                \"region\": \"GB\" // iso8601 region code\n            },\n            ...\n        },\n        ...\n    ],\n    ...\n}\n```\n\nThe languages array should be an array of objects with the following schema:\n\n```\n[\n    {\n        \"code\": \"en\",\n        \"region\": \"GB\",\n        \"quality\": 1.0\n    }\n]\n```\n\nIf language selection fails (no matching languages), then the transform returns null.\n\n#### `.transformDestination({source object}, {destination object}, [languages], callback function(err));`\n\nThis method does the same as the above, but it transforms the destination object with language fields from the source object.\nThere is no return value from this method.\n\nIf language selection fails (no matching languages), the callback will contain an error.\n\n\n#### `.transformByField({source}, [languages], {fields});`\n\nThis method applies the transform on a field by field basis as opposed to comparing a whole `i18n`\nblock like the `transform` method does.\n\nThe `fields` object should adhere to the following schema:\n\n```\n{\n    \"required\": [\n        \"Name\",\n        \"Address\"\n    ],\n\n    \"optional\": [\n        \"Description\",\n        \"DressCode\"\n    ]\n}\n```\n\nFor `required` fields, if a language match fails, the field from the primary language will be used. `Optional` fields\nwill only be returned if a language is explicitly matched.\n\nThe return value is different from `transform` as it returns the following schema:\n\n```\n{\n    \"translations\": {\n        \"Name\": \"The Fat Duck\",\n        \"DressCode\": \"Salle à manger formelle\"\n    },\n    \"localization\": {\n        \"Name\": \"en-GB\" // ISO 639-1 code\n        \"DressCode\": \"fr-FR\" // ISO 639-1 code\n    }\n}\n```\n\n- This method will only return values that are specified within the `fields`.\n\n- If the same field appears in both `required` and `optional` then the `required` value takes precedent.\n\n#### `.transformByFieldDestination({source}, [languages], {fields}, callback function(err));`\n\nThis method does the same as the above, but it transforms the destination object with the `translations` \u0026 `localization` fields.\n\nThere is no return value from this method.\n\nIf language selection fails (no matching languages), the callback will contain an error.\n\n##### Note\n\nThis module is designed to work with the [accept-language-parser](https://github.com/opentable/accept-language-parser).\n\n### Installation:\n\n```\nnpm install --save i18n-transform\n```\n\n### Usage (transform) :\n\n```\nvar transformer = require(\"i18n-transform\");\n\nvar result = transformer.transform(\n    {\n        \"id\": 123,\n        \"someinvariantfield\": 45.45,\n        \"i18n\": [\n            {\n                \"name\": \"the thing\",\n                \"somelocalisedfield\": \"local value\",\n                \"language\": {\n                    \"code\": \"en\",\n                    \"region\": \"GB\"\n                },\n                \"gb-specific-field\": \"some en-GB value\"\n            },\n            {\n                \"name\": \"the thang\",\n                \"somelocalisedfield\": \"local value 2\",\n                \"language\": {\n                    \"code\": \"en\",\n                    \"region\": \"US\"\n                },\n                \"us-specific-field\": \"some en-US value\"\n            }\n        ]\n    },\n    [\n        {\n            \"code\": \"en\",\n            \"region\": \"GB\",\n            \"quality\": 1.0\n        },\n        {\n            \"code\": \"en\",\n            \"region\": \"US\",\n            \"quality\": 0.8\n        }\n    ]\n);\n```\n\noutput:\n\n```\n{\n    \"id\": 123,\n    \"someinvariantfield\": 45.45,\n    \"name\": \"the thing\",\n    \"somelocalisedfield\": \"local value\",\n    \"language\": {\n      \"code\": \"en\",\n      \"region\": \"GB\"\n    },\n    \"gb-specific-field\": \"some en-GB value\"\n}\n```\n### Usage (transformByField) :\n\n```\nvar transformer = require(\"i18n-transform\");\n\nvar result = transformer.transformByField(\n    {\n        \"id\": 123,\n        \"someinvariantfield\": 45.45,\n        \"i18n\": [\n            {\n                \"name\": \"the thing\",\n                \"somelocalisedfield\": \"local value\",\n                \"language\": {\n                    \"code\": \"en\",\n                    \"region\": \"GB\"\n                },\n                \"gb-specific-field\": \"some en-GB value\"\n            },\n            {\n                \"name\": \"the thang\",\n                \"somelocalisedfield\": \"local value 2\",\n                \"language\": {\n                    \"code\": \"en\",\n                    \"region\": \"US\"\n                },\n                \"us-specific-field\": \"some en-US value\"\n            }\n        ]\n    },\n    [\n        {\n            \"code\": \"en\",\n            \"region\": \"GB\",\n            \"quality\": 1.0\n        },\n        {\n            \"code\": \"en\",\n            \"region\": \"US\",\n            \"quality\": 0.8\n        }\n    ],\n    {\n        \"required\":{\n            \"name\",\n            \"somelocalisedfield\",\n        },\n        \"optional\":{\n            \"us-specific-field\"\n        }\n    }\n);\n```\n\noutput:\n\n```\n{\n    \"translations\": {\n        \"name\": \"the thing\",\n        \"somelocalisedfield\": \"local value\",\n        \"us-specific-field\": \"some en-US value\"\n    },\n    \"localization\":{\n        \"name\": \"en-GB\",\n        \"somelocalisedfield\": \"en-GB\",\n        \"us-specific-field\": \"en-US\"\n    }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentable%2Fi18n-transform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopentable%2Fi18n-transform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopentable%2Fi18n-transform/lists"}