arabicfmt
Version:
Arabic-first formatting for numbers, currency, dates and bidirectional text across all 22 Arab League countries — with correct handling of the 2025–2026 Unicode currency-symbol transition (Saudi Riyal U+20C1, UAE Dirham U+20C3, Omani Rial U+20C4).
49 lines (46 loc) • 1.41 kB
JavaScript
;
// src/locale.ts
var DEFAULT_LOCALE = "ar";
function regionFromLocale(locale) {
const parts = locale.replace(/_/g, "-").split("-");
for (let i = 1; i < parts.length; i++) {
const part = parts[i];
if (/^[A-Za-z]{2}$/.test(part)) return part.toUpperCase();
if (/^\d{3}$/.test(part)) return part;
}
return void 0;
}
function withNumberingSystem(locale, ns) {
const IntlLocale = Intl.Locale;
if (IntlLocale) {
try {
return new IntlLocale(locale, { numberingSystem: ns }).toString();
} catch {
}
}
const base = locale.replace(/-u-.*$/i, "");
return `${base}-u-nu-${ns}`;
}
function detectLocale() {
if (typeof navigator !== "undefined") {
if (navigator.language) return navigator.language;
const nav = navigator;
if (Array.isArray(nav.languages) && nav.languages.length > 0) {
const first = nav.languages[0];
if (first) return first;
}
}
if (typeof process !== "undefined" && process.env) {
for (const key of ["LANG", "LANGUAGE", "LC_ALL", "LC_MESSAGES"]) {
const val = process.env[key];
if (val && val !== "C" && val !== "POSIX") {
return val.split(".")[0].replace(/_/g, "-");
}
}
}
return DEFAULT_LOCALE;
}
exports.DEFAULT_LOCALE = DEFAULT_LOCALE;
exports.detectLocale = detectLocale;
exports.regionFromLocale = regionFromLocale;
exports.withNumberingSystem = withNumberingSystem;