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
| Parameter | Description |
|---|---|
locale | BCP‑47 locale code to get the display name for |
defaultLocale | Locale to use for localising the display name (defaults to 'en') |
customMapping | Optional 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:
defaultLocaleparameter (if provided)- 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
- Custom mapping name (highest priority)
- Intl.DisplayNames in the default locale
- Intl.DisplayNames in the library default (‘en’)
- 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
- Get the locale emoji with
getLocaleEmoji - Use the GT class method for stateful operations
getLocaleName - Learn about the
CustomMappingtype
How is this guide?