- Documentation
- Packages
- intlayer
- getLocaleName
Documentation: getLocaleName Function in intlayer
Description:
The getLocaleName function returns the localized name of a given locale (targetLocale) in the display locale (displayLocale). If no targetLocale is provided, it returns the name of the displayLocale in its own language.
Parameters:
displayLocale: Locales
- Description: The locale in which the name of the target locale will be displayed.
- Type: Enum or string representing valid locales.
targetLocale?: Locales
- Description: The locale whose name is to be localized.
- Type: Optional. Enum or string representing valid locales.
Returns:
- Type: string
- Description: The localized name of the targetLocale in the displayLocale, or the displayLocale's own name if targetLocale is not provided. If no translation is found, it returns "Unknown locale".
Example Usage:
typescript
1import { Locales, getLocaleName } from "intlayer";
2
3getLocaleName(Locales.ENGLISH); // Output: "English"
4getLocaleName(Locales.ENGLISH, Locales.FRENCH); // Output: "Anglais"
5getLocaleName(Locales.ENGLISH, Locales.ESPANOL); // Output: "Inglés"
6getLocaleName(Locales.ENGLISH, Locales.ENGLISH); // Output: "English"
7
8getLocaleName(Locales.FRENCH); // Output: "Français"
9getLocaleName(Locales.FRENCH, Locales.FRENCH); // Output: "Français"
10getLocaleName(Locales.FRENCH, Locales.ESPANOL); // Output: "Francés"
11getLocaleName(Locales.FRENCH, Locales.ENGLISH); // Output: "French"
12
13getLocaleName(Locales.CHINESE); // Output: "中文"
14getLocaleName(Locales.CHINESE, Locales.FRENCH); // Output: "Chinois"
15getLocaleName(Locales.CHINESE, Locales.ESPANOL); // Output: "Chino"
16getLocaleName(Locales.CHINESE, Locales.ENGLISH); // Output: "Chinese"
17
18getLocaleName("unknown-locale"); // Output: "Unknown locale"
Edge Cases:
- No targetLocale provided:
- The function defaults to returning the displayLocale's own name.
- Missing translations:
- If localeNameTranslations does not contain an entry for the targetLocale or the specific displayLocale, the function falls back to the ownLocalesName or returns "Unknown locale".
If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentation