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 localized 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

Parameters Description

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

Returns

string - The localized display name of the locale.


Behavior

Display Language Resolution

The function localizes names using this priority:

  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 default locale
  3. Intl.DisplayNames in library default ('en')
  4. 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
  • Display locale parameter determines the language of the returned name

Next Steps

How is this guide?

getLocaleName