Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coffee -> ES6 #40

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/*
gulpfile.js
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "airbnb",
"rules": {
"func-names": ["error", "never"],
"no-underscore-dangle": ["error", { "allowAfterThis": true }],
"prefer-arrow-callback": 0
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
*.log
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: node_js
node_js:
- 0.8
- "4"
- "node"
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 Florian Lorrain

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 changes: 0 additions & 20 deletions LICENSE.txt

This file was deleted.

90 changes: 49 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,56 @@ locale is a [node.js][node] module for negotiating HTTP locales for incoming bro

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.

**Credits to [jed](https://github.com/jed) who passed the ownership of the package.**
Sources should be compatible with Node >= 0.8 but the dev environment needs Node >= 4.

Examples
--------

### For the node.js HTTP module
```javascript
var http = require("http")
, locale = require("locale")
, supported = new locale.Locales(["en", "en_US", "ja"])
const http = require('http');
const locale = require('locale');
const 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"})
http.createServer(function (req, res) {
const 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)
`You asked for: ${req.headers['accept-language']}
We support: ${supported}
Our default is: ${locale.Locale.default}
The best match is: ' + ${locales.best(supported)}`
);
}).listen(8000);
```

### For Connect/Express
```javascript
var http = require("http")
, express = require("express")
, locale = require("locale")
, supported = ["en", "en_US", "ja"]
, app = express()
const express = require('express');
const locale = require('locale');
const supported = ['en', 'en_US', 'ja'];
const app = express.createServer(locale(supported));

app.use(locale(supported))

app.get("/", function(req, res) {
res.header("Content-Type", "text/plain")
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)
`You asked for: ${req.headers['accept-language']}
We support: ${supported}
Our default is: ${locale.Locale.default}
The best match is: ${req.locale}`
);
});

app.listen(8000);
```

Install
-------
```bash
$ npm install locale
```

$ 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.)
Note - the package has no dependencies.

API
---
Expand All @@ -81,19 +79,29 @@ The Locales constructor takes a string compliant with the [`Accept-Language` HTT

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 exact match exists (i.e. language+country) then it will fallback to `language` if supported, or if the language isn't supported it will return the default locale.

supported = new locale.Locales(['en', 'en_US'], 'en');
(new locale.Locales('en')).best(supported).toString(); // 'en'
(new locale.Locales('en_GB')).best(supported).toString(); // 'en'
(new locale.Locales('en_US')).best(supported).toString(); // 'en_US'
(new locale.Locales('jp')).best(supported); // supported.default || locale.Locale["default"]
```javascript
supported = new locale.Locales(['en', 'en_US'], 'en');
(new locale.Locales('en')).best(supported).toString(); // 'en'
(new locale.Locales('en_GB')).best(supported).toString(); // 'en'
(new locale.Locales('en_US')).best(supported).toString(); // 'en_US'
(new locale.Locales('jp')).best(supported); // supported.default || locale.Locale["default"]
```

Contributing
---
1. Fork this repo and clone it locally.
2. Make sure you're using Node >= 4
3. Run `npm install` to install dev dependencies.
4. Run `npm test` and `npm build` and check no error pops up.
5. Now you're ready to rumble! You can use the default `gulp` task to watch and run tests while you're making changes.

Sources are ES2015 compliant and transpiled via Babel.

Copyright
---------
This project is licensed under the MIT license. For more information see LICENSE.md.

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

Send any questions or comments [here](http://twitter.com/jedschmidt).
Credits to [jed](https://github.com/jed) who was the initial maintainer of the package.

[node]: http://nodejs.org
[express]: http://expressjs.com
Expand Down
Loading