Utility FunctionsLocales

getLocaleName

API reference for the standalone getLocaleName function

Overview

The standalone getLocaleName function retrieves the display name of a locale code without requiring a GT class instance. It uses the Intl.DisplayNames API to provide localised locale names for any valid BCP-47 locale code.

import { getLocaleName } from 'generaltranslation';

const name = getLocaleName('fr-CA', 'en');
console.log(name); // "French (Canada)"

Reference

Parameters

Prop

Type

Parameter descriptions

ParameterDescription
localeBCP‑47 locale code to get the display name for
defaultLocaleLocale to use for localising the display name (defaults to 'en')
customMappingOptional custom mapping for locale codes and names

Returns

string – The localised display name of the locale.


Behaviour

Display Language Resolution

The function localises names in this order:

  1. defaultLocale parameter (if provided)
  2. Library default locale ('en')

Custom Mapping Integration

  • Custom mappings are checked first for both locale codes and names
  • Supports alias resolution and custom display names
  • Falls back to standard Intl.DisplayNames for unmapped codes

Name Resolution Strategy

  1. Custom mapping name (highest priority)
  2. Intl.DisplayNames in the default locale
  3. Intl.DisplayNames in the library default (‘en’)
  4. The locale code itself (fallback)

Examples

import { getLocaleName } from 'generaltranslation';

// English display names
console.log(getLocaleName('es', 'en')); // "Spanish (Spain)"
console.log(getLocaleName('ja', 'en')); // "Japanese (Japan)"
console.log(getLocaleName('zh', 'en')); // "Chinese (China)"

Building locale options

import { getLocaleName, getLocaleEmoji } from 'generaltranslation';

function buildLocaleOptions(
  supportedLocales: string[],
  displayLocale: string = 'en'
) {
  return supportedLocales.map(locale => ({
    value: locale,
    label: getLocaleName(locale, displayLocale),
    emoji: getLocaleEmoji(locale)
  }));
}

const options = buildLocaleOptions([
  'en',
  'es',
  'fr',
  'de',
  'ja'
], 'en');

console.log(options);
// [
//   { value: 'en', label: 'English (United States)', emoji: '🇺🇸' },
//   { value: 'es', label: 'Spanish (Spain)', emoji: '🇪🇸' },
//   ...
// ]

Notes

  • Custom mappings take precedence over standard Intl.DisplayNames
  • Returns the locale code itself if no display name can be determined
  • The display locale parameter determines the language of the returned name

Next steps

How is this guide?

getLocaleName