Cross-platform utility library for detecting and retrieving the system locale from the operating system
npx @tessl/cli install tessl/npm-os-locale@6.0.0OS Locale is a cross-platform utility library for detecting and retrieving the system locale (language and region settings) from the operating system. It provides both asynchronous and synchronous APIs to access locale information by examining environment variables and executing system-specific commands when needed.
npm install os-localeimport { osLocale, osLocaleSync } from "os-locale";For CommonJS (requires dynamic import since this is an ES module):
const { osLocale, osLocaleSync } = await import("os-locale");import { osLocale, osLocaleSync } from "os-locale";
// Asynchronous (recommended)
const locale = await osLocale();
console.log(locale); // 'en-US'
// Synchronous
const localeSync = osLocaleSync();
console.log(localeSync); // 'en-US'
// With options
const envOnlyLocale = await osLocale({ spawn: false });
console.log(envOnlyLocale); // Locale from environment variables onlyGets the system locale asynchronously with optimal performance.
/**
* Get the system locale asynchronously
* @param options - Configuration options for locale detection
* @returns Promise resolving to the locale string (e.g., 'en-US')
* @example
* ```
* import {osLocale} from 'os-locale';
*
* console.log(await osLocale());
* //=> 'en-US'
* ```
*/
function osLocale(options?: Options): Promise<string>;Usage Examples:
import { osLocale } from "os-locale";
// Default behavior (checks environment variables and spawns system commands)
const locale = await osLocale();
console.log(locale); // 'en-US', 'fr-FR', 'ja-JP', etc.
// Environment variables only (no subprocess spawning)
const envLocale = await osLocale({ spawn: false });
console.log(envLocale); // Locale from LC_ALL, LC_MESSAGES, LANG, or LANGUAGEGets the system locale synchronously for simpler code patterns.
/**
* Get the system locale synchronously
* @param options - Configuration options for locale detection
* @returns The locale string (e.g., 'en-US')
* @example
* ```
* import {osLocaleSync} from 'os-locale';
*
* console.log(osLocaleSync());
* //=> 'en-US'
* ```
*/
function osLocaleSync(options?: Options): string;Usage Examples:
import { osLocaleSync } from "os-locale";
// Default synchronous behavior
const locale = osLocaleSync();
console.log(locale); // 'en-US'
// Environment variables only (synchronous)
const envLocale = osLocaleSync({ spawn: false });
console.log(envLocale); // Locale from environment variablesinterface Options {
/**
* Set to `false` to avoid spawning subprocesses and instead only resolve the locale from environment variables.
*
* @default true
*/
readonly spawn?: boolean;
}The library works across different operating systems with platform-specific detection methods:
wmic os get locale command to get LCID, then converts to locale using the lcid packagedefaults read -globalDomain AppleLocale command to get system localelocale command to parse system locale configurationLC_ALL, LC_MESSAGES, LANG, LANGUAGE)When spawn: false is set, only environment variables are checked, providing faster execution at the cost of reduced accuracy on some systems.
Both functions handle errors gracefully:
'en-US' as the default localeThe library includes internal caching based on the spawn option to avoid repeated system calls: