{"id":15561264,"url":"https://github.com/matsoftware/accept-language-parser","last_synced_at":"2025-04-23T22:25:29.003Z","repository":{"id":56900658,"uuid":"267850181","full_name":"matsoftware/accept-language-parser","owner":"matsoftware","description":"HTTP Accept-Language parser in Swift","archived":false,"fork":false,"pushed_at":"2022-11-25T11:12:00.000Z","size":26,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-22T02:04:07.966Z","etag":null,"topics":["accept-language","accept-language-parser","i18n","kitura","swift","vapor-swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/matsoftware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"liberapay":"matsoftware"}},"created_at":"2020-05-29T12:17:25.000Z","updated_at":"2024-04-26T17:31:57.000Z","dependencies_parsed_at":"2022-08-21T02:20:44.035Z","dependency_job_id":null,"html_url":"https://github.com/matsoftware/accept-language-parser","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matsoftware%2Faccept-language-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matsoftware%2Faccept-language-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matsoftware%2Faccept-language-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matsoftware%2Faccept-language-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matsoftware","download_url":"https://codeload.github.com/matsoftware/accept-language-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242254174,"owners_count":20097531,"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","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":["accept-language","accept-language-parser","i18n","kitura","swift","vapor-swift"],"created_at":"2024-10-02T16:06:52.622Z","updated_at":"2025-03-06T17:31:48.803Z","avatar_url":"https://github.com/matsoftware.png","language":"Swift","readme":"# ALanguageParser\n\n[![CircleCI](https://circleci.com/gh/matsoftware/accept-language-parser.svg?style=shield)](https://circleci.com/gh/matsoftware/accept-language-parser) [![codecov](https://codecov.io/gh/matsoftware/accept-language-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/matsoftware/accept-language-parser) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/ede74115487a4462860e7d7ce9f14db8)](https://www.codacy.com/manual/matsoftware/accept-language-parser?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=matsoftware/accept-language-parser\u0026amp;utm_campaign=Badge_Grade) ![Cocoapods platforms](https://img.shields.io/cocoapods/p/ALanguageParser) [![Size](https://img.shields.io/github/languages/code-size/matsoftware/accept-language-parser)](Size) [![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmatsoftware%2Faccept-language-parser.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmatsoftware%2Faccept-language-parser?ref=badge_shield) [![License](https://img.shields.io/badge/license-MIT-blue.svg?x=1)](LICENSE)\n\n\u003cp align=\"center\"\u003e\nLightweight HTTP RFC-2616 \u003cb\u003e\u003ca href=\"https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4\" target=\"_blank\"\u003eAccept-Language\u003c/a\u003e\u003c/b\u003e parser in Swift.\n\u003c/p\u003e\n\n`ALanguageParser` is the swift porting of the npm [accept-language-parser](https://github.com/opentable/accept-language-parser) package by OpenTable.  It parses the accept-language header from an HTTP request and produces an array of language objects sorted by quality.\n\nIt has been designed to be used with Swift Server Side in mind (e.g. Vapor, Kitura, Perfect) but it can be used in any client side project.\n\n## How to use\n\n### Parse\n\nIt returns the ordered list of accepted languages:\n\n```swift\nimport ALanguageParser\n\nlet languages = ALanguageParser.parse(\"it-IT,zh-Hant-HK;q=0.5\")\n```\n\nThe result will be:\n```swift\n[\n    AcceptedLanguage(code: \"it\",\n                     quality: 1.0,\n                     region: \"IT\",\n                     script: nil),\n\n    AcceptedLanguage(code: \"zh\",\n                     quality: 0.5,\n                     region: \"HK\",\n                     script: \"Hant\")\n]\n```\n\nOutput is always sorted in quality order from highest -\u003e lowest. As per the HTTP spec, omitting the quality value implies `1.0`.\n\n### Pick\n\nExtracts the first selected language from the ones available in the given accept language string or `nil` if not present.\n\n```swift\nlet language = ALanguageParser.pick([\"fr-CA\", \"fr-FR\", \"fr\"],\n                                    acceptLanguage: \"en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8\")\n```\n\nThe output will be:\n```swift\n\"fr-CA\"\n```\n\nThe function can be called with the `loose` parameter that allows partial matching on supported languages. For example:\n\n```swift\nlet language = ALanguageParser.pick([\"fr-CA\", \"fr-FR\", \"fr\"],\n                                    acceptLanguage: \"en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8\",\n                                    loose: true)\n```\n\nThe output will be:\n```swift\n\"fr\"\n```\n\nIn loose mode the order of `supportedLanguages` matters, as it is the first partially matching language that is returned. It means that if you want to pick more specific langauge first, you should list it first as well, for example: `[\"fr-CA\", \"fr\"]`.\n\n## Installation\n\n### Swift Package Manager\n\nThe library, currently being developed in Swift 5.7, can be integrated by adding the following entry to your dependencies array in the `Package.swift` file:\n\n```swift\n.package(url: \"https://github.com/matsoftware/accept-language-parser.git\", from: \"1.1.0\")\n```\n\nThen in your target, please add the `ALanguageParser` dependency:\n\n```swift\n.product(name: \"ALanguageParser\", package: \"accept-language-parser\")\n```\n\n### CocoaPods\n\nAdd the pod `ALanguageParser` to your Podfile:\n\n```ruby\npod 'ALanguageParser'\n```\n\nRun `pod install` and then open your workspace to launch Xcode.\n\n## Contributions\n\nPlease open an issue on GitHub or fork the repository to make changes.\n\nBefore raising any PR, please make sure that the tests are passing on the _Linux_ platform. \n\n### Running the tests on Linux with Docker\n\nYou can use [Docker](https://docs.docker.com/get-docker/) to download the Swift image and perform tests on your MacOS/Windows machine.\n\nOnce Docker and its CLI are installed, you can get the image from [Docker Hub](https://hub.docker.com/_/swift) by running:\n\n```bash\ndocker pull swift\n```\n\nOnce the image has been download, from the root folder of your forked repository you can start the container in privileged mode and use a bind mount to let Docker access your folder:\n\n```bash\ndocker run -it --privileged --mount type=bind,source=$(pwd),target=/app swift /bin/bash\n```\n\nFinally, you can run the tests:\n```bash\nroot@c4706264baa1:/app$ swift test\n```\n\n## License\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmatsoftware%2Faccept-language-parser.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmatsoftware%2Faccept-language-parser?ref=badge_large)\n\n## Author\n\nCreated by [Mattia Campolese](https://www.linkedin.com/in/matcamp/).\n\n[![Twitter Follow](https://img.shields.io/twitter/follow/matsoftware?style=social)](https://twitter.com/matsoftware)\n\nPlease also check out [swift code metrics](https://github.com/matsoftware/swift-code-metrics), the code metrics analyzer for Swift projects.","funding_links":["https://liberapay.com/matsoftware"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatsoftware%2Faccept-language-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatsoftware%2Faccept-language-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatsoftware%2Faccept-language-parser/lists"}