Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261import { LIMITS } from "./limits.ts";
// Full ISO 639-1 set (two-letter codes only). Display labels are generated// at render time via Intl.DisplayNames rather than hand-maintained, so this// list never needs curation/updates as new languages get added to the picker.const ISO_639_1_CODES = [ "aa", "ab", "ae", "af", "ak", "am", "an", "ar", "as", "av", "ay", "az", "ba", "be", "bg", "bh", "bi", "bm", "bn", "bo", "br", "bs", "ca", "ce", "ch", "co", "cr", "cs", "cu", "cv", "cy", "da", "de", "dv", "dz", "ee", "el", "en", "eo", "es", "et", "eu", "fa", "ff", "fi", "fj", "fo", "fr", "fy", "ga", "gd", "gl", "gn", "gu", "gv", "ha", "he", "hi", "ho", "hr", "ht", "hu", "hy", "hz", "ia", "id", "ie", "ig", "ii", "ik", "io", "is", "it", "iu", "ja", "jv", "ka", "kg", "ki", "kj", "kk", "kl", "km", "kn", "ko", "kr", "ks", "ku", "kv", "kw", "ky", "la", "lb", "lg", "li", "ln", "lo", "lt", "lu", "lv", "mg", "mh", "mi", "mk", "ml", "mn", "mr", "ms", "mt", "my", "na", "nb", "nd", "ne", "ng", "nl", "nn", "no", "nr", "nv", "ny", "oc", "oj", "om", "or", "os", "pa", "pi", "pl", "ps", "pt", "qu", "rm", "rn", "ro", "ru", "rw", "sa", "sc", "sd", "se", "sg", "si", "sk", "sl", "sm", "sn", "so", "sq", "sr", "ss", "st", "su", "sv", "sw", "ta", "te", "tg", "th", "ti", "tk", "tl", "tn", "to", "tr", "ts", "tt", "tw", "ty", "ug", "uk", "ur", "uz", "ve", "vi", "vo", "wa", "wo", "xh", "yi", "yo", "za", "zh", "zu",] as const;
// Curated set surfaced first in the language picker as a quick-pick group.const COMMON_LANGUAGE_CODES = [ "en", "fr", "es", "de", "pt", "ja", "zh", "ko", "ar", "eo",] as const;
// Wikis carry a free-form BCP-47 content-language tag (not limited to the// list above), so validate by shape rather than a fixed list. This is the// write-side half of the defense; views must still escape the value, since// records from third-party PDSes bypass this check entirely.const BCP47_TAG = /^[A-Za-z]{2,8}(-[A-Za-z0-9]{1,8})*$/;
export function isValidLanguageTag(tag: string): boolean { return tag.length <= LIMITS.wiki.language && BCP47_TAG.test(tag);}
export interface LanguageOption { code: string; label: string;}
// Groups the ISO 639-1 set for the picker: the curated common codes first (in// curated order), then the remainder sorted by localized display name. A single// Intl.DisplayNames instance resolves every label. Labels are for display only —// the option value is always the code, so submissions can't carry a free-form name.export function languageOptions(locale: string): { common: LanguageOption[]; all: LanguageOption[];} { let display: Intl.DisplayNames | null = null; try { display = new Intl.DisplayNames([locale, "en"], { type: "language" }); } catch { display = null; } const label = (code: string): string => { let name: string; try { name = display?.of(code) ?? code; } catch { name = code; } // Some locales (e.g. French) return language names lowercase. Uppercase // the first letter so menu items read consistently in every UI locale. const [first, ...rest] = [...name]; return first ? first.toLocaleUpperCase(locale) + rest.join("") : name; };
const common = COMMON_LANGUAGE_CODES.map((code) => ({ code, label: label(code), }));
const commonSet = new Set<string>(COMMON_LANGUAGE_CODES); const all = ISO_639_1_CODES.filter((code) => !commonSet.has(code)) .map((code) => ({ code, label: label(code) })) .sort((a, b) => a.label.localeCompare(b.label, locale));
return { common, all };}