Swiftpack.co - matsoftware/accept-language-parser as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by matsoftware.
matsoftware/accept-language-parser 1.1.0
HTTP Accept-Language parser in Swift
⭐️ 1
🕓 2 weeks ago
.package(url: "https://github.com/matsoftware/accept-language-parser.git", from: "1.1.0")

ALanguageParser

CircleCI codecov Codacy Badge Cocoapods platforms Size FOSSA Status License

Lightweight HTTP RFC-2616 Accept-Language parser in Swift.

ALanguageParser is the swift porting of the npm 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.

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

How to use

Parse

It returns the ordered list of accepted languages:

import ALanguageParser

let languages = ALanguageParser.parse("it-IT,zh-Hant-HK;q=0.5")

The result will be:

[
    AcceptedLanguage(code: "it",
                     quality: 1.0,
                     region: "IT",
                     script: nil),

    AcceptedLanguage(code: "zh",
                     quality: 0.5,
                     region: "HK",
                     script: "Hant")
]

Output is always sorted in quality order from highest -> lowest. As per the HTTP spec, omitting the quality value implies 1.0.

Pick

Extracts the first selected language from the ones available in the given accept language string or nil if not present.

let language = ALanguageParser.pick(["fr-CA", "fr-FR", "fr"],
                                    acceptLanguage: "en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8")

The output will be:

"fr-CA"

The function can be called with the loose parameter that allows partial matching on supported languages. For example:

let language = ALanguageParser.pick(["fr-CA", "fr-FR", "fr"],
                                    acceptLanguage: "en-GB,en-US;q=0.9,fr-CA;q=0.7,en;q=0.8",
                                    loose: true)

The output will be:

"fr"

In 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"].

Installation

Swift Package Manager

The 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:

.package(url: "https://github.com/matsoftware/accept-language-parser.git", from: "1.1.0")

Then in your target, please add the ALanguageParser dependency:

.product(name: "ALanguageParser", package: "accept-language-parser")

CocoaPods

Add the pod ALanguageParser to your Podfile:

pod 'ALanguageParser'

Run pod install and then open your workspace to launch Xcode.

Contributions

Please open an issue on GitHub or fork the repository to make changes.

Before raising any PR, please make sure that the tests are passing on the Linux platform.

Running the tests on Linux with Docker

You can use Docker to download the Swift image and perform tests on your MacOS/Windows machine.

Once Docker and its CLI are installed, you can get the image from Docker Hub by running:

docker pull swift

Once 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:

docker run -it --privileged --mount type=bind,source=$(pwd),target=/app swift /bin/bash

Finally, you can run the tests:

[email protected]:/app$ swift test

License

FOSSA Status

Author

Created by Mattia Campolese.

Twitter Follow

Please also check out swift code metrics, the code metrics analyzer for Swift projects.

GitHub

link
Stars: 2
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics