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