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")
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:
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
ALanguageParser
Lightweight HTTP RFC-2616 Accept-Language parser in Swift.
ALanguageParseris 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:
The result will be:
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
nilif not present.The output will be:
The function can be called with the
looseparameter that allows partial matching on supported languages. For example:The output will be:
In loose mode the order of
supportedLanguagesmatters, 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.swiftfile:Then in your target, please add the
ALanguageParserdependency:CocoaPods
Add the pod
ALanguageParserto your Podfile:Run
pod installand 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:
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:
Finally, you can run the tests:
License
Author
Created by Mattia Campolese.
Please also check out swift code metrics, the code metrics analyzer for Swift projects.