Skip to main content
The getLanguage() function retrieves or creates a Language object that can be used with the lower-level formatting and parsing functions.

Import

import { getLanguage } from 'enhanced-ms';

Signature

function getLanguage(
  locale: Locale | LanguageDefinition
): Language
locale
Locale | LanguageDefinition
required
Either a locale string (e.g., 'en', 'de', 'ru') or a custom LanguageDefinition object.
return
Language
A Language object with parsing and formatting utilities

Usage

Get a built-in language

import { getLanguage } from 'enhanced-ms';

const english = getLanguage('en');
const german = getLanguage('de');
const russian = getLanguage('ru');

Use with formatting functions

The Language object is primarily used with formatMilliseconds() and parseDuration():
import { formatMilliseconds, getLanguage } from 'enhanced-ms';

const spanish = getLanguage('es');
formatMilliseconds(90061000, spanish)
// Returns: "1 día 1 hora 1 minuto 1 segundo"

Use with custom language definitions

You can pass a custom language definition directly:
import { getLanguage } from 'enhanced-ms';

const customLanguage = getLanguage({
  decimal: '.',
  and: 'and',
  units: {
    // ... unit definitions
  }
});

Language caching

The function uses an internal cache to avoid recreating language objects. When you call getLanguage() with the same locale multiple times, it returns the cached Language object:
const lang1 = getLanguage('en');
const lang2 = getLanguage('en');
// lang1 === lang2 (same object reference)
This means you can call getLanguage() without performance penalties.

Available locales

The following locale keys are supported:
  • 'en' - English
  • 'de' - German
  • 'ru' - Russian
  • 'mi' - Māori
  • 'es' - Spanish
  • 'nl' - Dutch
  • 'it' - Italian
  • 'fr' - French
  • 'cs' - Czech
  • 'pl' - Polish
  • 'zh-CN' - Chinese (Simplified)

When to use this function

Use getLanguage() when: For most use cases, the higher-level createMs() function is more convenient as it creates a function with a pre-configured language.

See also

Build docs developers (and LLMs) love