acceptlang

package module
v0.0.0-...-4f60163 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2014 License: MIT Imports: 7 Imported by: 3

README

acceptlang wercker status

Using the acceptlang handler you can automatically parse the Accept-Language HTTP header and expose it as an AcceptLanguages slice in your handler functions. The AcceptLanguages slice contains AcceptLanguage values, each of which represent a qualified (or unqualified) language. The values in the slice are sorted descending by qualification (the most qualified languages will have the lowest indexes in the slice).

Unqualified languages are interpreted as having the maximum qualification of 1, as defined in the HTTP/1.1 specification.

For more information:

Usage

Simply add a new handler function instance to your handler chain using the acceptlang.Languages() function as well as an AcceptLanguages dependency in your handler function. The AcceptLanguages dependency will be satisified by the handler.

For example:

package main

import (
	"fmt"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/acceptlang"
	"net/http"
)

func main() {
	m := martini.Classic()

	m.Get("/", acceptlang.Languages(), func(languages acceptlang.AcceptLanguages) string {
		return fmt.Sprintf("Languages: %s", languages)
	})

	http.ListenAndServe(":8090", m)
}

Authors

Documentation

Overview

Package acceptlang provides a Martini handler and primitives to parse the Accept-Language HTTP header values.

See the HTTP header fields specification for more details (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4).

Example

Use the handler to automatically parse the Accept-Language header and return the results as response:

m.Get("/", acceptlang.Languages(), func(languages acceptlang.AcceptLanguages) string {
    return fmt.Sprintf("Languages: %s", languages)
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Languages

func Languages() martini.Handler

Creates a new handler that parses the Accept-Language HTTP header.

The parsed structure is a slice of Accept-Language values stored in an AcceptLanguages instance, sorted based on the language qualifier.

Types

type AcceptLanguage

type AcceptLanguage struct {
	Language string
	Quality  float32
}

A single language from the Accept-Language HTTP header.

type AcceptLanguages

type AcceptLanguages []AcceptLanguage

A slice of sortable AcceptLanguage instances.

func (AcceptLanguages) Len

func (al AcceptLanguages) Len() int

Returns the total number of items in the slice. Implemented to satisfy sort.Interface.

func (AcceptLanguages) Less

func (al AcceptLanguages) Less(i, j int) bool

Determines whether or not the item at position i is "less than" the item at position j. Implemented to satisfy sort.Interface.

func (AcceptLanguages) String

func (al AcceptLanguages) String() string

Returns the parsed languages in a human readable fashion.

func (AcceptLanguages) Swap

func (al AcceptLanguages) Swap(i, j int)

Swaps the items at position i and j. Implemented to satisfy sort.Interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL