pax_global_header00006660000000000000000000000064130130620000014473gustar00rootroot0000000000000052 comment=b978314bf27271cd600c4dd550aa61563cf7a52e os-locale-2.0.0/000077500000000000000000000000001301306200000133505ustar00rootroot00000000000000os-locale-2.0.0/.editorconfig000066400000000000000000000002761301306200000160320ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 os-locale-2.0.0/.gitattributes000066400000000000000000000000351301306200000162410ustar00rootroot00000000000000* text=auto *.js text eol=lf os-locale-2.0.0/.gitignore000066400000000000000000000000151301306200000153340ustar00rootroot00000000000000node_modules os-locale-2.0.0/.travis.yml000066400000000000000000000000671301306200000154640ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' os-locale-2.0.0/index.js000066400000000000000000000044021301306200000150150ustar00rootroot00000000000000'use strict'; const execa = require('execa'); const lcid = require('lcid'); const mem = require('mem'); const defaultOpts = {spawn: true}; const defaultLocale = 'en_US'; function getEnvLocale(env) { env = env || process.env; return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE; } function parseLocale(x) { const env = x.split('\n').reduce((env, def) => { def = def.split('='); env[def[0]] = def[1].replace(/^"|"$/g, ''); return env; }, {}); return getEnvLocale(env); } function getLocale(str) { return (str && str.replace(/[.:].*/, '')); } function getAppleLocale() { return execa.stdout('defaults', ['read', '-g', 'AppleLocale']); } function getAppleLocaleSync() { return execa.sync('defaults', ['read', '-g', 'AppleLocale']).stdout; } function getUnixLocale() { if (process.platform === 'darwin') { return getAppleLocale(); } return execa.stdout('locale') .then(stdout => getLocale(parseLocale(stdout))); } function getUnixLocaleSync() { if (process.platform === 'darwin') { return getAppleLocaleSync(); } return getLocale(parseLocale(execa.sync('locale').stdout)); } function getWinLocale() { return execa.stdout('wmic', ['os', 'get', 'locale']) .then(stdout => { const lcidCode = parseInt(stdout.replace('Locale', ''), 16); return lcid.from(lcidCode); }); } function getWinLocaleSync() { const stdout = execa.sync('wmic', ['os', 'get', 'locale']).stdout; const lcidCode = parseInt(stdout.replace('Locale', ''), 16); return lcid.from(lcidCode); } module.exports = mem(opts => { opts = opts || defaultOpts; const envLocale = getEnvLocale(); let thenable; if (envLocale || opts.spawn === false) { thenable = Promise.resolve(getLocale(envLocale)); } else if (process.platform === 'win32') { thenable = getWinLocale(); } else { thenable = getUnixLocale(); } return thenable.then(locale => locale || defaultLocale) .catch(() => defaultLocale); }); module.exports.sync = mem(opts => { opts = opts || defaultOpts; const envLocale = getEnvLocale(); let res; if (envLocale || opts.spawn === false) { res = getLocale(envLocale); } else { try { if (process.platform === 'win32') { res = getWinLocaleSync(); } else { res = getUnixLocaleSync(); } } catch (err) {} } return res || defaultLocale; }); os-locale-2.0.0/license000066400000000000000000000021371301306200000147200ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (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. os-locale-2.0.0/package.json000066400000000000000000000014141301306200000156360ustar00rootroot00000000000000{ "name": "os-locale", "version": "2.0.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": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "locale", "lang", "language", "system", "os", "string", "str", "user", "country", "id", "identifier", "region" ], "dependencies": { "execa": "^0.5.0", "lcid": "^1.0.0", "mem": "^1.1.0" }, "devDependencies": { "ava": "*", "require-uncached": "^1.0.2", "xo": "*" }, "xo": { "esnext": true } } os-locale-2.0.0/readme.md000066400000000000000000000020701301306200000151260ustar00rootroot00000000000000# 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](https://en.wikipedia.org/wiki/Locale_(computer_software)) 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 const osLocale = require('os-locale'); osLocale.then(locale => { console.log(locale); //=> 'en_US' }); ``` ## API ### osLocale([options]) Returns a `Promise` for the locale. ### osLocale.sync([options]) Returns the locale. #### options Type: `Object` ##### 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](https://sindresorhus.com) os-locale-2.0.0/test.js000066400000000000000000000041741301306200000146730ustar00rootroot00000000000000import execa from 'execa'; import test from 'ava'; import requireUncached from 'require-uncached'; const envVars = ['LC_ALL', 'LANGUAGE', 'LANG', 'LC_MESSAGES']; const expectedFallback = 'en_US'; function unsetEnvVars(cache) { envVars.forEach(envVar => { if (process.env[envVar]) { cache[envVar] = process.env[envVar]; delete process.env[envVar]; } }); } function restoreEnvVars(cache) { envVars.forEach(envVar => { if (cache[envVar]) { process.env[envVar] = cache[envVar]; } }); } test('async', async t => { const locale = await requireUncached('./')(); console.log('Locale identifier:', locale); t.true(locale.length > 1); t.true(locale.includes('_')); }); test('sync', t => { const locale = requireUncached('./').sync(); console.log('Locale identifier:', locale); t.true(locale.length > 1); t.true(locale.includes('_')); }); test('async without spawn', async t => { const beforeTest = {}; // unset env vars and cache for restoration unsetEnvVars(beforeTest); // mock execa.stdout beforeTest.stdout = execa.stdout; execa.stdout = () => new Promise(resolve => resolve('spawn_NOTALLOWED')); // callback to restore env vars and undo mock const afterTest = () => { restoreEnvVars(beforeTest); execa.stdout = beforeTest.execaStdout; }; // test async method const locale = await requireUncached('./')({spawn: false}); console.log('Locale identifier:', locale); afterTest(); t.is(locale, expectedFallback, 'Locale did not match expected fallback'); t.not(locale, 'spawn_NOTALLOWED', 'Attempted to spawn subprocess'); }); test('sync without spawn', t => { const beforeTest = {}; // unset env vars and cache for restoration unsetEnvVars(beforeTest); // mock execa.sync beforeTest.execaSync = execa.sync; execa.sync = () => { t.false('Attempted to spawn subprocess'); }; // test sync method const locale = requireUncached('./').sync({spawn: false}); console.log('Locale identifier:', locale); t.is(locale, expectedFallback, 'Locale did not match expected fallback'); // restore env vars and undo mock restoreEnvVars(beforeTest); if (beforeTest.execaSync) { execa.sync = beforeTest.execaSync; } });