GithubHelp home page GithubHelp logo

locale's Introduction

locale

Build Status

locale is a node.js module for negotiating HTTP locales for incoming browser requests. It can be used as a standalone module for HTTP or as Express/Connect middleware, or as the server component for an in-browser gettext implementation like JED.

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

Examples

For the node.js HTTP module

var http = require("http")
  , locale = require("locale")
  , supported = new locale.Locales(["en", "en_US", "ja"])

http.createServer(function(req, res) {
  var locales = new locale.Locales(req.headers["accept-language"])
  res.writeHeader(200, {"Content-Type": "text/plain"})
  res.end(
    "You asked for: " + req.headers["accept-language"] + "\n" +
    "We support: " + supported + "\n" +
    "Our default is: " + locale.Locale["default"] + "\n" +
    "The best match is: " + locales.best(supported) + "\n"
  )
}).listen(8000)

For Connect/Express

var http = require("http")
  , express = require("express")
  , locale = require("locale")
  , supported = ["en", "en_US", "ja"]
  , app = express.createServer(locale(supported))

app.get("/", function(req, res) {
  res.header("Content-Type", "text/plain")
  res.send(
    "You asked for: " + req.headers["accept-language"] + "\n" +
    "We support: " + supported + "\n" +
    "Our default is: " + locale.Locale["default"] + "\n" +
    "The best match is: " + req.locale + "\n"
  )
})

app.listen(8000)

Install

$ npm install locale

(Note that although this repo is CoffeeScript, the actual npm library is pre-compiled to pure JavaScript and has no run-time dependencies.)

API

locale(supportedLocales)

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

new locale.Locale(languageTag)

The Locale constructor takes a language tag 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.

locale.Locale["default"]

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

locales = new locale.Locales(acceptLanguageHeader)

The Locales constructor takes a string compliant with the Accept-Language HTTP header, and returns a list of acceptible locales, optionally sorted in descending order by quality score.

locales.best([supportedLocales])

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

Copyright

Copyright (c) 2012 Jed Schmidt. See LICENSE.txt for details.

Send any questions or comments here.

locale's People

Contributors

jed avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.