diff options
Diffstat (limited to 'node_modules/os-locale')
-rw-r--r-- | node_modules/os-locale/index.js | 127 | ||||
-rw-r--r-- | node_modules/os-locale/license | 21 | ||||
-rw-r--r-- | node_modules/os-locale/package.json | 43 | ||||
-rw-r--r-- | node_modules/os-locale/readme.md | 47 |
4 files changed, 238 insertions, 0 deletions
diff --git a/node_modules/os-locale/index.js b/node_modules/os-locale/index.js new file mode 100644 index 000000000..2c8a0065d --- /dev/null +++ b/node_modules/os-locale/index.js @@ -0,0 +1,127 @@ +'use strict'; +var childProcess = require('child_process'); +var execFileSync = childProcess.execFileSync; +var lcid = require('lcid'); +var defaultOpts = {spawn: true}; +var cache; + +function fallback() { + cache = 'en_US'; + return cache; +} + +function getEnvLocale(env) { + env = env || process.env; + var ret = env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE; + cache = getLocale(ret); + return ret; +} + +function parseLocale(x) { + var env = x.split('\n').reduce(function (env, def) { + def = def.split('='); + env[def[0]] = def[1]; + return env; + }, {}); + return getEnvLocale(env); +} + +function getLocale(str) { + return (str && str.replace(/[.:].*/, '')) || fallback(); +} + +module.exports = function (opts, cb) { + if (typeof opts === 'function') { + cb = opts; + opts = defaultOpts; + } else { + opts = opts || defaultOpts; + } + + if (cache || getEnvLocale() || opts.spawn === false) { + setImmediate(cb, null, cache); + return; + } + + var getAppleLocale = function () { + childProcess.execFile('defaults', ['read', '-g', 'AppleLocale'], function (err, stdout) { + if (err) { + fallback(); + return; + } + + cache = stdout.trim() || fallback(); + cb(null, cache); + }); + }; + + if (process.platform === 'win32') { + childProcess.execFile('wmic', ['os', 'get', 'locale'], function (err, stdout) { + if (err) { + fallback(); + return; + } + + var lcidCode = parseInt(stdout.replace('Locale', ''), 16); + cache = lcid.from(lcidCode) || fallback(); + cb(null, cache); + }); + } else { + childProcess.execFile('locale', function (err, stdout) { + if (err) { + fallback(); + return; + } + + var res = parseLocale(stdout); + + if (!res && process.platform === 'darwin') { + getAppleLocale(); + return; + } + + cache = getLocale(res); + cb(null, cache); + }); + } +}; + +module.exports.sync = function (opts) { + opts = opts || defaultOpts; + + if (cache || getEnvLocale() || !execFileSync || opts.spawn === false) { + return cache; + } + + if (process.platform === 'win32') { + var stdout; + + try { + stdout = execFileSync('wmic', ['os', 'get', 'locale'], {encoding: 'utf8'}); + } catch (err) { + return fallback(); + } + + var lcidCode = parseInt(stdout.replace('Locale', ''), 16); + cache = lcid.from(lcidCode) || fallback(); + return cache; + } + + var res; + + try { + res = parseLocale(execFileSync('locale', {encoding: 'utf8'})); + } catch (err) {} + + if (!res && process.platform === 'darwin') { + try { + cache = execFileSync('defaults', ['read', '-g', 'AppleLocale'], {encoding: 'utf8'}).trim() || fallback(); + return cache; + } catch (err) { + return fallback(); + } + } + + cache = getLocale(res); + return cache; +}; diff --git a/node_modules/os-locale/license b/node_modules/os-locale/license new file mode 100644 index 000000000..654d0bfe9 --- /dev/null +++ b/node_modules/os-locale/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) + +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. diff --git a/node_modules/os-locale/package.json b/node_modules/os-locale/package.json new file mode 100644 index 000000000..934f57239 --- /dev/null +++ b/node_modules/os-locale/package.json @@ -0,0 +1,43 @@ +{ + "name": "os-locale", + "version": "1.4.0", + "description": "Get the system locale", + "license": "MIT", + "repository": "sindresorhus/os-locale", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "locale", + "lang", + "language", + "system", + "os", + "string", + "str", + "user", + "country", + "id", + "identifier", + "region" + ], + "dependencies": { + "lcid": "^1.0.0" + }, + "devDependencies": { + "ava": "*", + "require-uncached": "^1.0.2", + "xo": "*" + } +} diff --git a/node_modules/os-locale/readme.md b/node_modules/os-locale/readme.md new file mode 100644 index 000000000..b80a0bd0b --- /dev/null +++ b/node_modules/os-locale/readme.md @@ -0,0 +1,47 @@ +# os-locale [![Build Status](https://travis-ci.org/sindresorhus/os-locale.svg?branch=master)](https://travis-ci.org/sindresorhus/os-locale) + +> Get the system [locale](http://en.wikipedia.org/wiki/Locale) + +Useful for localizing your module or app. + +POSIX systems: The returned locale refers to the [`LC_MESSAGE`](http://www.gnu.org/software/libc/manual/html_node/Locale-Categories.html#Locale-Categories) category, suitable for selecting the language used in the user interface for message translation. + + +## Install + +``` +$ npm install --save os-locale +``` + + +## Usage + +```js +var osLocale = require('os-locale'); + +osLocale(function (err, locale) { + console.log(locale); + //=> 'en_US' +}); +``` + + +## API + +### osLocale([options], callback(error, locale)) + +### osLocale.sync([options]) + +Returns the locale. + +#### options.spawn + +Type: `boolean` +Default: `true` + +Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables. + + +## License + +MIT © [Sindre Sorhus](http://sindresorhus.com) |