UNPKG

3.9 kBMarkdownView Raw
1locale
2======
3
4[![Build Status](https://secure.travis-ci.org/jed/locale.png)](http://travis-ci.org/jed/locale)
5
6locale is a [node.js][node] module for negotiating HTTP locales for incoming browser requests. It can be used as a standalone module for HTTP or as [Express][express]/[Connect][connect] middleware, or as the server component for an in-browser gettext implementation like [JED][JED].
7
8It works like this: you (optionally) tell it the languages you support, and it figures out the best one to use for each incoming request from a browser. So if you support `en`, `en_US`, `ja`, `kr`, and `zh_TW`, and a request comes in that accepts `en_UK` or `en`, locale will figure out that `en` is the best language to use.
9
10Examples
11--------
12
13### For the node.js HTTP module
14```javascript
15var http = require("http")
16 , locale = require("locale")
17 , supported = new locale.Locales(["en", "en_US", "ja"])
18
19http.createServer(function(req, res) {
20 var locales = new locale.Locales(req.headers["accept-language"])
21 res.writeHeader(200, {"Content-Type": "text/plain"})
22 res.end(
23 "You asked for: " + req.headers["accept-language"] + "\n" +
24 "We support: " + supported + "\n" +
25 "Our default is: " + locale.Locale["default"] + "\n" +
26 "The best match is: " + locales.best(supported) + "\n"
27 )
28}).listen(8000)
29```
30
31### For Connect/Express
32```javascript
33var http = require("http")
34 , express = require("express")
35 , locale = require("locale")
36 , supported = ["en", "en_US", "ja"]
37 , app = express.createServer(locale(supported))
38
39app.get("/", function(req, res) {
40 res.header("Content-Type", "text/plain")
41 res.send(
42 "You asked for: " + req.headers["accept-language"] + "\n" +
43 "We support: " + supported + "\n" +
44 "Our default is: " + locale.Locale["default"] + "\n" +
45 "The best match is: " + req.locale + "\n"
46 )
47})
48
49app.listen(8000)
50```
51
52Install
53-------
54
55 $ npm install locale
56
57(Note that although this repo is CoffeeScript, the actual npm library is pre-compiled to pure JavaScript and has no run-time dependencies.)
58
59API
60---
61
62### locale(supportedLocales)
63
64This module exports a function that can be used as Express/Connect middleware. It takes one argument, a list of supported locales, and adds a `locale` property to incoming HTTP requests, reflecting the most appropriate locale determined using the `best` method described below.
65
66### new locale.Locale(languageTag)
67
68The Locale constructor takes a [language tag][langtag] string consisting of an ISO-639 language abbreviation and optional two-letter ISO-3166 country code, and returns an object with a `language` property containing the former and a `country` property containing the latter.
69
70### locale.Locale["default"]
71
72The default locale for the environment, as parsed from `process.env.LANG`. This is used as the fallback when the best language is calculated from the intersection of requested and supported locales.
73
74### locales = new locale.Locales(acceptLanguageHeader)
75
76The Locales constructor takes a string compliant with the [`Accept-Language` HTTP header][header], and returns a list of acceptible locales, optionally sorted in descending order by quality score.
77
78### locales.best([supportedLocales])
79
80This method takes the target locale and compares it against the optionally provided list of supported locales, and returns the most appropriate locale based on the quality scores of the target locale. If no match exists, the default locale is returned.
81
82Copyright
83---------
84
85Copyright (c) 2012 Jed Schmidt. See LICENSE.txt for details.
86
87Send any questions or comments [here](http://twitter.com/jedschmidt).
88
89[node]: http://nodejs.org
90[express]: http://expressjs.com
91[JED]: http://slexaxton.github.com/Jed
92[connect]: http://senchalabs.github.com/connect
93[langtag]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.10
94[header]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
\No newline at end of file