pax_global_header00006660000000000000000000000064134777624110014526gustar00rootroot0000000000000052 comment=966ff9d03ea8c7911fd71e57fb60f042ceaea73e os-locale-4.0.0/000077500000000000000000000000001347776241100134055ustar00rootroot00000000000000os-locale-4.0.0/.editorconfig000066400000000000000000000002571347776241100160660ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 os-locale-4.0.0/.gitattributes000066400000000000000000000000231347776241100162730ustar00rootroot00000000000000* text=auto eol=lf os-locale-4.0.0/.github/000077500000000000000000000000001347776241100147455ustar00rootroot00000000000000os-locale-4.0.0/.github/funding.yml000066400000000000000000000001631347776241100171220ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/os-locale custom: https://sindresorhus.com/donate os-locale-4.0.0/.github/security.md000066400000000000000000000002631347776241100171370ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. os-locale-4.0.0/.gitignore000066400000000000000000000000271347776241100153740ustar00rootroot00000000000000node_modules yarn.lock os-locale-4.0.0/.npmrc000066400000000000000000000000231347776241100145200ustar00rootroot00000000000000package-lock=false os-locale-4.0.0/.travis.yml000066400000000000000000000000651347776241100155170ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' os-locale-4.0.0/index.d.ts000066400000000000000000000013511347776241100153060ustar00rootroot00000000000000declare namespace osLocale { interface Options { /** Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables. @default true */ readonly spawn?: boolean; } } declare const osLocale: { /** Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)). @returns The locale. @example ``` import osLocale = require('os-locale'); (async () => { console.log(await osLocale()); //=> 'en-US' })(); ``` */ (options?: osLocale.Options): Promise; /** Synchronously get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software)). @returns The locale. */ sync(options?: osLocale.Options): string; }; export = osLocale; os-locale-4.0.0/index.js000066400000000000000000000054011347776241100150520ustar00rootroot00000000000000'use strict'; const execa = require('execa'); const lcid = require('lcid'); const mem = require('mem'); const defaultOptions = {spawn: true}; const defaultLocale = 'en-US'; function getEnvLocale(env = process.env) { return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE; } function parseLocale(string) { const env = string.split('\n').reduce((env, definition) => { const [key, value] = definition.split('='); env[key] = value.replace(/^"|"$/g, ''); return env; }, {}); return getEnvLocale(env); } function getLocale(string) { return (string && string.replace(/[.:].*/, '')); } async function getLocales() { return execa.stdout('locale', ['-a']); } function getLocalesSync() { return execa.sync('locale', ['-a']).stdout; } function getSupportedLocale(locale, locales = '') { return locales.includes(locale) ? locale : defaultLocale; } async function getAppleLocale() { const results = await Promise.all([ execa.stdout('defaults', ['read', '-globalDomain', 'AppleLocale']), getLocales() ]); return getSupportedLocale(results[0], results[1]); } function getAppleLocaleSync() { return getSupportedLocale( execa.sync('defaults', ['read', '-globalDomain', 'AppleLocale']).stdout, getLocalesSync() ); } async function getUnixLocale() { if (process.platform === 'darwin') { return getAppleLocale(); } return getLocale(parseLocale(await execa.stdout('locale'))); } function getUnixLocaleSync() { if (process.platform === 'darwin') { return getAppleLocaleSync(); } return getLocale(parseLocale(execa.sync('locale').stdout)); } async function getWinLocale() { const stdout = await execa.stdout('wmic', ['os', 'get', 'locale']); const lcidCode = parseInt(stdout.replace('Locale', ''), 16); return lcid.from(lcidCode); } function getWinLocaleSync() { const {stdout} = execa.sync('wmic', ['os', 'get', 'locale']); const lcidCode = parseInt(stdout.replace('Locale', ''), 16); return lcid.from(lcidCode); } const osLocale = mem(async (options = defaultOptions) => { let locale; try { const envLocale = getEnvLocale(); if (envLocale || options.spawn === false) { locale = getLocale(envLocale); } else if (process.platform === 'win32') { locale = await getWinLocale(); } else { locale = await getUnixLocale(); } } catch (_) {} return (locale || defaultLocale).replace(/_/, '-'); }, {cachePromiseRejection: false}); module.exports = osLocale; module.exports.sync = mem((options = defaultOptions) => { let locale; try { const envLocale = getEnvLocale(); if (envLocale || options.spawn === false) { locale = getLocale(envLocale); } else if (process.platform === 'win32') { locale = getWinLocaleSync(); } else { locale = getUnixLocaleSync(); } } catch (_) {} return (locale || defaultLocale).replace(/_/, '-'); }); os-locale-4.0.0/index.test-d.ts000066400000000000000000000003721347776241100162650ustar00rootroot00000000000000import {expectType} from 'tsd'; import osLocale = require('.'); expectType>(osLocale()); expectType>(osLocale({spawn: false})); expectType(osLocale.sync()); expectType(osLocale.sync({spawn: false})); os-locale-4.0.0/license000066400000000000000000000021251347776241100147520ustar00rootroot00000000000000MIT License 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-4.0.0/package.json000066400000000000000000000013301347776241100156700ustar00rootroot00000000000000{ "name": "os-locale", "version": "4.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": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "locale", "lang", "language", "system", "os", "string", "str", "user", "country", "id", "identifier", "region" ], "dependencies": { "execa": "^1.0.0", "lcid": "^3.0.0", "mem": "^5.0.0" }, "devDependencies": { "ava": "^2.1.0", "import-fresh": "^3.0.0", "tsd": "^0.7.2", "xo": "^0.24.0" } } os-locale-4.0.0/readme.md000066400000000000000000000026451347776241100151730ustar00rootroot00000000000000# 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 os-locale ``` ## Usage ```js const osLocale = require('os-locale'); (async () => { console.log(await osLocale()); //=> '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. ---
Get professional support for 'os-locale' with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
os-locale-4.0.0/test.js000066400000000000000000000041441347776241100147250ustar00rootroot00000000000000import execa from 'execa'; import test from 'ava'; import importFresh from 'import-fresh'; const envVars = ['LC_ALL', 'LANGUAGE', 'LANG', 'LC_MESSAGES']; const expectedFallback = 'en-US'; function unsetEnvVars(cache) { for (const envVar of envVars) { if (process.env[envVar]) { cache[envVar] = process.env[envVar]; delete process.env[envVar]; } } } function restoreEnvVars(cache) { for (const envVar of envVars) { if (cache[envVar]) { process.env[envVar] = cache[envVar]; } } } test('async', async t => { const locale = await importFresh('.')(); console.log('Locale identifier:', locale); t.true(locale.length > 1); t.true(locale.includes('-')); }); test('sync', t => { const locale = importFresh('.').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 importFresh('.')({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 = importFresh('.').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; } });