{"id":13836153,"url":"https://github.com/lependu/fastify-language-parser","last_synced_at":"2025-08-01T12:04:59.731Z","repository":{"id":37801001,"uuid":"131334787","full_name":"lependu/fastify-language-parser","owner":"lependu","description":"A Fastify plugin to parse request language","archived":false,"fork":false,"pushed_at":"2023-03-20T14:56:51.000Z","size":99,"stargazers_count":5,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-27T23:47:49.935Z","etag":null,"topics":["fastify-plugin","i18n","nodejs"],"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/lependu.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-04-27T19:02:47.000Z","updated_at":"2022-12-16T14:41:34.000Z","dependencies_parsed_at":"2024-04-12T11:01:53.435Z","dependency_job_id":"7a9d5a18-ea69-4dd8-afbc-a20108c7ba68","html_url":"https://github.com/lependu/fastify-language-parser","commit_stats":{"total_commits":63,"total_committers":5,"mean_commits":12.6,"dds":0.1428571428571429,"last_synced_commit":"a85c48bba9ee1d5f5ea917901e68bef5157cf6c7"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/lependu/fastify-language-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lependu%2Ffastify-language-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lependu%2Ffastify-language-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lependu%2Ffastify-language-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lependu%2Ffastify-language-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lependu","download_url":"https://codeload.github.com/lependu/fastify-language-parser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lependu%2Ffastify-language-parser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267578003,"owners_count":24110351,"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-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["fastify-plugin","i18n","nodejs"],"created_at":"2024-08-04T15:00:36.942Z","updated_at":"2025-08-01T12:04:59.703Z","avatar_url":"https://github.com/lependu.png","language":"JavaScript","readme":"# fastify-language-parser\n\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)\n[![Build Status](https://travis-ci.org/lependu/fastify-language-parser.svg?branch=master)](https://travis-ci.org/lependu/fastify-language-parser)\n[![Known Vulnerabilities](https://snyk.io/test/github/lependu/fastify-language-parser/badge.svg)](https://snyk.io/test/github/lependu/fastify-language-parser)\n[![Coverage Status](https://coveralls.io/repos/github/lependu/fastify-language-parser/badge.svg?branch=master)](https://coveralls.io/github/lependu/fastify-language-parser?branch=master)\n![npm (scoped)](https://img.shields.io/npm/v/fastify-language-parser/latest)\n![npm](https://img.shields.io/npm/dm/fastify-language-parser.svg)\n\nLanguage parser plugin for [fastify](https://github.com/fastify/fastify)\n\nIt decorates `req` object with `detectedLng` and adds `preHandler` hook for those language parsers which you specified in `order` option. Supports `cookie`, `header`, `query`, `path` and `session` parser.\n\n\n## Install\n```\n$ npm i --save fastify-language-parser\n```\n\n## Versions\n\nThe plugin supports the following `Fastify` versions. Please refer to the corresponding branch in PR and issues.\n\nVersion | Branch | Fastify | End of Support\n--------|--------|---------|---------------  \n1.x | [1.x](https://github.com/lependu/fastify-language-parser/tree/1.x) | [1.x](https://github.com/fastify/fastify/tree/1.x) | EOL\n2.x | [2.x](https://github.com/lependu/fastify-language-parser/tree/2.x) | [2.x](https://github.com/fastify/fastify/tree/2.x) | TBD  \n3.x | [master](https://github.com/lependu/fastify-language-parser/tree/master) | [3.x](https://github.com/fastify/fastify/tree/master) | TBD  \n  \n## Example\n```js\nconst fastify = require('fastify')()\nconst LP = require('fastify-language-parser')\n\nfastify\n  .register(LP, { order: ['query'] })\n  .after(err =\u003e {\n    if (err) throw err\n    fastify.get('/', (req, reply) =\u003e {\n      // GET /?lng=de will set req.detectedLng to 'de'\n      reply.send({ detectedLng: req.detectedLng })\n    })\n  })\n```\n\nIf you need different set of parsers for different routes, [scope](https://www.fastify.io/docs/latest/Plugins-Guide/) the routes and register this plugin in each scope where it needed.\n\n\n## API\n\n### General options\n\nname | type | default | description\n-----|------|---------|------------\n**`fallbackLng`** | `{String}` | `'en'` | The default value of the `req.detectedLng` decorator.\n**`order`** | `{Array}` | `[]` | Order and from where language should be detected. The last item wins. Supported values are: `cookie` `header` `path` `query` `session`.\n**`supportedLngs`** | `{Array}` | `['en']` | Use this option to filter the parsed language code. If it is an empty array parsers will skip the filter step. The order of items only counts when you use `header` parser.\n\n### Parser specific options and notes\n#### header parser\nUnder the hood it uses [accept-language-parser](https://github.com/opentable/accept-language-parser). If `supportedLngs` does not contain any item it uses the [parse](https://github.com/opentable/accept-language-parser#parserparseacceptlanguageheader), otherwise the [pick](https://github.com/opentable/accept-language-parser) method. Latter is the only case when the order in `supportedLngs` array makes any difference, because parser will pass it to the pick method. If the pick method returns no value than the `detectedLng` decorator will not change. It parses `req.headers['accept-language']` value which provided by fastify and normally you don't need to change, but you can using the `headerDecorator` and `headerKey` options.\n\n#### query \u0026 path parsers\nParses specified keys from `req.query` and `req.params` decorators which provided by Fastify's core [Request](https://www.fastify.io/docs/latest/Request/) object. Ususally you don't need to change them, but you can using the `pathDecorator` and `queryDecorator` options.\n\nname | type | default | description\n-----|------|---------|------------\n**`pathDecorator`** | `{String}` | `'params'` | The object key which contains the params matching the URL.\n**`pathKey`** | `{String}` | `lng` | The object key which contains the actual language within the `req[pathDecorator]` object.\n**`queryDecorator`** | `{String}` | `'query'` | The object key which contains the parsed querystring.\n**`queryKey`** | `{String}` | `lng` | The object key which contains the actual language within the querystring.\n\n#### cookie \u0026 session parsers\nIf you intend to use these parsers you need to register [fastify-cookie](https://github.com/fastify/fastify-cookie) or [fastify-session](https://github.com/SerayaEryn/fastify-session) plugin respectively. This plugin does not retrieves or sets any cookie or session value but looks for the `req[decorator][key]` value specified in options. The fastify-cookie plugin provides `req.cookies` decorator by defult. If you use fastify-session plugin you need to set the `req[sessionDecorator][sessionKey]` before this plugin, which reflects your session store state changes and provides the language code value.\n\nname | type | default | description\n-----|------|---------|------------\n**`cookieDecorator`** | `{String}` | `'cookies'` |  Looks for the key in\u003cbr /\u003e `req[cookieDecorator]`.\n**`cookieKey`** | `{String}` | `'fastifyLanguageParser'` | Parses the value of\u003cbr /\u003e `req[cookieDecorator][cookieKey]`.\n**`sessionDecorator`** | `{String}` | `'sessions'` | Looks for the key in\u003cbr /\u003e `req[sessionDecorator]`.\n**`sessionKey`** | `{String}` | `'fastifyLanguageParser'` | Parses the value of\u003cbr /\u003e `req[sessionDecorator][sessionKey]`.\n\n## License\nLicensed under [MIT](./LICENSE).\n","funding_links":[],"categories":["\u003ch2 align=\"center\"\u003eAwesome Fastify\u003c/h2\u003e"],"sub_categories":["\u003ch2 align=\"center\"\u003eEcosystem\u003c/h2\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flependu%2Ffastify-language-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flependu%2Ffastify-language-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flependu%2Ffastify-language-parser/lists"}