mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 08:32:09 +01:00
Uses `Intl.DisplayNames` to display localized language names in the language pie chart, enhancing user experience and readability. Also converts country and language values from the CSV data to ISO codes for standardization and improved data handling. Adds tooltip to display ISO language code.
106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
import ISO6391 from "iso-639-1";
|
|
import countries from "i18n-iso-countries";
|
|
|
|
// Register locales for i18n-iso-countries
|
|
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
|
|
|
|
/**
|
|
* Get a human-readable language name from ISO 639-1 code
|
|
* @param code The ISO 639-1 language code
|
|
* @returns The language name or the original code if not found
|
|
*/
|
|
export function getLanguageName(code: string | null | undefined): string {
|
|
if (!code) return "Unknown";
|
|
|
|
// Handle invalid codes
|
|
if (code.length !== 2) return code;
|
|
|
|
// Try using ISO6391 library
|
|
try {
|
|
const name = ISO6391.getName(code);
|
|
if (name) return name;
|
|
} catch (e) {
|
|
console.error(`Error getting language name for code: ${code}`, e);
|
|
}
|
|
|
|
return code; // Return original code as fallback
|
|
}
|
|
|
|
/**
|
|
* Get a human-readable country name from ISO 3166-1 alpha-2 code
|
|
* @param code The ISO 3166-1 alpha-2 country code
|
|
* @returns The country name or the original code if not found
|
|
*/
|
|
export function getCountryName(code: string | null | undefined): string {
|
|
if (!code) return "Unknown";
|
|
|
|
// Handle invalid codes
|
|
if (code.length !== 2) return code;
|
|
|
|
// Try using i18n-iso-countries library
|
|
try {
|
|
const name = countries.getName(code, "en");
|
|
if (name) return name;
|
|
} catch (e) {
|
|
console.error(`Error getting country name for code: ${code}`, e);
|
|
}
|
|
|
|
return code; // Return original code as fallback
|
|
}
|
|
|
|
/**
|
|
* Client-side function to get localized language name using Intl.DisplayNames
|
|
* @param code The ISO 639-1 language code
|
|
* @param locale The locale to use (defaults to browser's locale)
|
|
* @returns The localized language name
|
|
*/
|
|
export function getLocalizedLanguageName(
|
|
code: string | null | undefined,
|
|
locale?: string,
|
|
): string {
|
|
if (typeof window === "undefined" || !code) return getLanguageName(code);
|
|
|
|
try {
|
|
// Check if Intl.DisplayNames is supported
|
|
if (typeof Intl !== "undefined" && "DisplayNames" in Intl) {
|
|
const userLocale = locale || navigator.language || "en";
|
|
const displayNames = new Intl.DisplayNames([userLocale], {
|
|
type: "language",
|
|
});
|
|
return displayNames.of(code) || getLanguageName(code);
|
|
}
|
|
} catch (e) {
|
|
console.error(`Error getting localized language name for code: ${code}`, e);
|
|
}
|
|
|
|
return getLanguageName(code);
|
|
}
|
|
|
|
/**
|
|
* Client-side function to get localized country name using Intl.DisplayNames
|
|
* @param code The ISO 3166-1 alpha-2 country code
|
|
* @param locale The locale to use (defaults to browser's locale)
|
|
* @returns The localized country name
|
|
*/
|
|
export function getLocalizedCountryName(
|
|
code: string | null | undefined,
|
|
locale?: string,
|
|
): string {
|
|
if (typeof window === "undefined" || !code) return getCountryName(code);
|
|
|
|
try {
|
|
// Check if Intl.DisplayNames is supported
|
|
if (typeof Intl !== "undefined" && "DisplayNames" in Intl) {
|
|
const userLocale = locale || navigator.language || "en";
|
|
const displayNames = new Intl.DisplayNames([userLocale], {
|
|
type: "region",
|
|
});
|
|
return displayNames.of(code) || getCountryName(code);
|
|
}
|
|
} catch (e) {
|
|
console.error(`Error getting localized country name for code: ${code}`, e);
|
|
}
|
|
|
|
return getCountryName(code);
|
|
}
|