@lingo.dev/_locales 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1033 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ LOCALE_REGEX: () => LOCALE_REGEX,
24
+ getCountryName: () => getCountryName,
25
+ getLanguageCode: () => getLanguageCode,
26
+ getLanguageName: () => getLanguageName,
27
+ getRegionCode: () => getRegionCode,
28
+ getScriptCode: () => getScriptCode,
29
+ getScriptName: () => getScriptName,
30
+ isValidLanguageCode: () => isValidLanguageCode,
31
+ isValidLocale: () => isValidLocale,
32
+ isValidRegionCode: () => isValidRegionCode,
33
+ isValidScriptCode: () => isValidScriptCode,
34
+ parseLocale: () => parseLocale,
35
+ parseLocaleWithDetails: () => parseLocaleWithDetails
36
+ });
37
+ module.exports = __toCommonJS(src_exports);
38
+
39
+ // src/constants.ts
40
+ var LOCALE_REGEX = /^([a-z]{2,3})(?:[-_]([A-Za-z]{4}))?(?:[-_]([A-Z]{2}|[0-9]{3}))?$/;
41
+
42
+ // src/parser.ts
43
+ function normalizeLocaleCase(locale) {
44
+ const parts = locale.split(/[-_]/);
45
+ if (parts.length === 1) {
46
+ return parts[0].toLowerCase();
47
+ }
48
+ if (parts.length === 2) {
49
+ const language = parts[0].toLowerCase();
50
+ const region = parts[1].toUpperCase();
51
+ return `${language}-${region}`;
52
+ }
53
+ if (parts.length === 3) {
54
+ const language = parts[0].toLowerCase();
55
+ const script = parts[1];
56
+ const region = parts[2].toUpperCase();
57
+ return `${language}-${script}-${region}`;
58
+ }
59
+ return locale;
60
+ }
61
+ function parseLocale(locale) {
62
+ if (typeof locale !== "string") {
63
+ throw new Error("Locale must be a string");
64
+ }
65
+ if (!locale.trim()) {
66
+ throw new Error("Locale cannot be empty");
67
+ }
68
+ const normalizedLocale = normalizeLocaleCase(locale);
69
+ const match = normalizedLocale.match(LOCALE_REGEX);
70
+ if (!match) {
71
+ throw new Error(`Invalid locale format: ${locale}`);
72
+ }
73
+ const [, language, script, region] = match;
74
+ const components = {
75
+ language: language.toLowerCase()
76
+ };
77
+ if (script) {
78
+ components.script = script;
79
+ }
80
+ if (region) {
81
+ components.region = region.toUpperCase();
82
+ }
83
+ return components;
84
+ }
85
+ function parseLocaleWithDetails(locale) {
86
+ try {
87
+ const components = parseLocale(locale);
88
+ let delimiter = null;
89
+ if (locale.includes("-")) {
90
+ delimiter = "-";
91
+ } else if (locale.includes("_")) {
92
+ delimiter = "_";
93
+ }
94
+ return {
95
+ components,
96
+ delimiter,
97
+ isValid: true
98
+ };
99
+ } catch (error) {
100
+ return {
101
+ components: { language: "" },
102
+ delimiter: null,
103
+ isValid: false,
104
+ error: error instanceof Error ? error.message : "Unknown parsing error"
105
+ };
106
+ }
107
+ }
108
+ function getLanguageCode(locale) {
109
+ return parseLocale(locale).language;
110
+ }
111
+ function getScriptCode(locale) {
112
+ const components = parseLocale(locale);
113
+ return components.script || null;
114
+ }
115
+ function getRegionCode(locale) {
116
+ const components = parseLocale(locale);
117
+ return components.region || null;
118
+ }
119
+
120
+ // src/validation.ts
121
+ var VALID_LANGUAGE_CODES = /* @__PURE__ */ new Set([
122
+ "aa",
123
+ "ab",
124
+ "ae",
125
+ "af",
126
+ "ak",
127
+ "am",
128
+ "an",
129
+ "ar",
130
+ "as",
131
+ "av",
132
+ "ay",
133
+ "az",
134
+ "ba",
135
+ "be",
136
+ "bg",
137
+ "bh",
138
+ "bi",
139
+ "bm",
140
+ "bn",
141
+ "bo",
142
+ "br",
143
+ "bs",
144
+ "ca",
145
+ "ce",
146
+ "ch",
147
+ "co",
148
+ "cr",
149
+ "cs",
150
+ "cu",
151
+ "cv",
152
+ "cy",
153
+ "da",
154
+ "de",
155
+ "dv",
156
+ "dz",
157
+ "ee",
158
+ "el",
159
+ "en",
160
+ "eo",
161
+ "es",
162
+ "et",
163
+ "eu",
164
+ "fa",
165
+ "ff",
166
+ "fi",
167
+ "fj",
168
+ "fo",
169
+ "fr",
170
+ "fy",
171
+ "ga",
172
+ "gd",
173
+ "gl",
174
+ "gn",
175
+ "gu",
176
+ "gv",
177
+ "ha",
178
+ "he",
179
+ "hi",
180
+ "ho",
181
+ "hr",
182
+ "ht",
183
+ "hu",
184
+ "hy",
185
+ "hz",
186
+ "ia",
187
+ "id",
188
+ "ie",
189
+ "ig",
190
+ "ii",
191
+ "ik",
192
+ "io",
193
+ "is",
194
+ "it",
195
+ "iu",
196
+ "ja",
197
+ "jv",
198
+ "ka",
199
+ "kg",
200
+ "ki",
201
+ "kj",
202
+ "kk",
203
+ "kl",
204
+ "km",
205
+ "kn",
206
+ "ko",
207
+ "kr",
208
+ "ks",
209
+ "ku",
210
+ "kv",
211
+ "kw",
212
+ "ky",
213
+ "la",
214
+ "lb",
215
+ "lg",
216
+ "li",
217
+ "ln",
218
+ "lo",
219
+ "lt",
220
+ "lu",
221
+ "lv",
222
+ "mg",
223
+ "mh",
224
+ "mi",
225
+ "mk",
226
+ "ml",
227
+ "mn",
228
+ "mr",
229
+ "ms",
230
+ "mt",
231
+ "my",
232
+ "na",
233
+ "nb",
234
+ "nd",
235
+ "ne",
236
+ "ng",
237
+ "nl",
238
+ "nn",
239
+ "no",
240
+ "nr",
241
+ "nv",
242
+ "ny",
243
+ "oc",
244
+ "oj",
245
+ "om",
246
+ "or",
247
+ "os",
248
+ "pa",
249
+ "pi",
250
+ "pl",
251
+ "ps",
252
+ "pt",
253
+ "qu",
254
+ "rm",
255
+ "rn",
256
+ "ro",
257
+ "ru",
258
+ "rw",
259
+ "sa",
260
+ "sc",
261
+ "sd",
262
+ "se",
263
+ "sg",
264
+ "si",
265
+ "sk",
266
+ "sl",
267
+ "sm",
268
+ "sn",
269
+ "so",
270
+ "sq",
271
+ "sr",
272
+ "ss",
273
+ "st",
274
+ "su",
275
+ "sv",
276
+ "sw",
277
+ "ta",
278
+ "te",
279
+ "tg",
280
+ "th",
281
+ "ti",
282
+ "tk",
283
+ "tl",
284
+ "tn",
285
+ "to",
286
+ "tr",
287
+ "ts",
288
+ "tt",
289
+ "tw",
290
+ "ty",
291
+ "ug",
292
+ "uk",
293
+ "ur",
294
+ "uz",
295
+ "ve",
296
+ "vi",
297
+ "vo",
298
+ "wa",
299
+ "wo",
300
+ "xh",
301
+ "yi",
302
+ "yo",
303
+ "za",
304
+ "zh",
305
+ "zu"
306
+ ]);
307
+ var VALID_SCRIPT_CODES = /* @__PURE__ */ new Set([
308
+ "Adlm",
309
+ "Afak",
310
+ "Aghb",
311
+ "Ahom",
312
+ "Arab",
313
+ "Aran",
314
+ "Armi",
315
+ "Armn",
316
+ "Avst",
317
+ "Bali",
318
+ "Bamu",
319
+ "Bass",
320
+ "Batk",
321
+ "Beng",
322
+ "Bhks",
323
+ "Blis",
324
+ "Bopo",
325
+ "Brah",
326
+ "Brai",
327
+ "Bugi",
328
+ "Buhd",
329
+ "Cakm",
330
+ "Cans",
331
+ "Cari",
332
+ "Cham",
333
+ "Cher",
334
+ "Chrs",
335
+ "Cirt",
336
+ "Copt",
337
+ "Cpmn",
338
+ "Cprt",
339
+ "Cyrl",
340
+ "Cyrs",
341
+ "Deva",
342
+ "Diak",
343
+ "Dogr",
344
+ "Dsrt",
345
+ "Dupl",
346
+ "Egyd",
347
+ "Egyh",
348
+ "Egyp",
349
+ "Elba",
350
+ "Elym",
351
+ "Ethi",
352
+ "Gara",
353
+ "Gong",
354
+ "Gonm",
355
+ "Goth",
356
+ "Gran",
357
+ "Grek",
358
+ "Gujr",
359
+ "Guru",
360
+ "Hanb",
361
+ "Hang",
362
+ "Hani",
363
+ "Hano",
364
+ "Hans",
365
+ "Hant",
366
+ "Hatr",
367
+ "Hebr",
368
+ "Hira",
369
+ "Hluw",
370
+ "Hmng",
371
+ "Hmnp",
372
+ "Hrkt",
373
+ "Hung",
374
+ "Inds",
375
+ "Ital",
376
+ "Jamo",
377
+ "Java",
378
+ "Jpan",
379
+ "Jurc",
380
+ "Kali",
381
+ "Kana",
382
+ "Khar",
383
+ "Khmr",
384
+ "Khoj",
385
+ "Kits",
386
+ "Knda",
387
+ "Kore",
388
+ "Kpel",
389
+ "Kthi",
390
+ "Lana",
391
+ "Laoo",
392
+ "Latf",
393
+ "Latg",
394
+ "Latn",
395
+ "Leke",
396
+ "Lepc",
397
+ "Limb",
398
+ "Lina",
399
+ "Linb",
400
+ "Lisu",
401
+ "Loma",
402
+ "Lyci",
403
+ "Lydi",
404
+ "Mahj",
405
+ "Maka",
406
+ "Mand",
407
+ "Mani",
408
+ "Marc",
409
+ "Maya",
410
+ "Medf",
411
+ "Mend",
412
+ "Merc",
413
+ "Mero",
414
+ "Mlym",
415
+ "Modi",
416
+ "Mong",
417
+ "Moon",
418
+ "Mroo",
419
+ "Mtei",
420
+ "Mult",
421
+ "Mymr",
422
+ "Nand",
423
+ "Narb",
424
+ "Nbat",
425
+ "Newa",
426
+ "Nkgb",
427
+ "Nkoo",
428
+ "Nshu",
429
+ "Ogam",
430
+ "Olck",
431
+ "Orkh",
432
+ "Orya",
433
+ "Osge",
434
+ "Osma",
435
+ "Ougr",
436
+ "Palm",
437
+ "Pauc",
438
+ "Perm",
439
+ "Phag",
440
+ "Phli",
441
+ "Phlp",
442
+ "Phlv",
443
+ "Phnx",
444
+ "Plrd",
445
+ "Prti",
446
+ "Qaaa",
447
+ "Qabx",
448
+ "Rjng",
449
+ "Rohg",
450
+ "Roro",
451
+ "Runr",
452
+ "Samr",
453
+ "Sara",
454
+ "Sarb",
455
+ "Saur",
456
+ "Sgnw",
457
+ "Shaw",
458
+ "Shrd",
459
+ "Shui",
460
+ "Sidd",
461
+ "Sind",
462
+ "Sinh",
463
+ "Sogd",
464
+ "Sogo",
465
+ "Sora",
466
+ "Soyo",
467
+ "Sund",
468
+ "Sylo",
469
+ "Syrc",
470
+ "Syre",
471
+ "Syrj",
472
+ "Syrn",
473
+ "Tagb",
474
+ "Takr",
475
+ "Tale",
476
+ "Talu",
477
+ "Taml",
478
+ "Tang",
479
+ "Tavt",
480
+ "Telu",
481
+ "Teng",
482
+ "Tfng",
483
+ "Tglg",
484
+ "Thaa",
485
+ "Thai",
486
+ "Tibt",
487
+ "Tirh",
488
+ "Ugar",
489
+ "Vaii",
490
+ "Visp",
491
+ "Wara",
492
+ "Wcho",
493
+ "Wole",
494
+ "Xpeo",
495
+ "Xsux",
496
+ "Yezi",
497
+ "Yiii",
498
+ "Zanb",
499
+ "Zinh",
500
+ "Zmth",
501
+ "Zsye",
502
+ "Zsym",
503
+ "Zxxx",
504
+ "Zyyy",
505
+ "Zzzz"
506
+ ]);
507
+ var VALID_REGION_CODES = /* @__PURE__ */ new Set([
508
+ "AD",
509
+ "AE",
510
+ "AF",
511
+ "AG",
512
+ "AI",
513
+ "AL",
514
+ "AM",
515
+ "AO",
516
+ "AQ",
517
+ "AR",
518
+ "AS",
519
+ "AT",
520
+ "AU",
521
+ "AW",
522
+ "AX",
523
+ "AZ",
524
+ "BA",
525
+ "BB",
526
+ "BD",
527
+ "BE",
528
+ "BF",
529
+ "BG",
530
+ "BH",
531
+ "BI",
532
+ "BJ",
533
+ "BL",
534
+ "BM",
535
+ "BN",
536
+ "BO",
537
+ "BQ",
538
+ "BR",
539
+ "BS",
540
+ "BT",
541
+ "BV",
542
+ "BW",
543
+ "BY",
544
+ "BZ",
545
+ "CA",
546
+ "CC",
547
+ "CD",
548
+ "CF",
549
+ "CG",
550
+ "CH",
551
+ "CI",
552
+ "CK",
553
+ "CL",
554
+ "CM",
555
+ "CN",
556
+ "CO",
557
+ "CR",
558
+ "CU",
559
+ "CV",
560
+ "CW",
561
+ "CX",
562
+ "CY",
563
+ "CZ",
564
+ "DE",
565
+ "DJ",
566
+ "DK",
567
+ "DM",
568
+ "DO",
569
+ "DZ",
570
+ "EC",
571
+ "EE",
572
+ "EG",
573
+ "EH",
574
+ "ER",
575
+ "ES",
576
+ "ET",
577
+ "FI",
578
+ "FJ",
579
+ "FK",
580
+ "FM",
581
+ "FO",
582
+ "FR",
583
+ "GA",
584
+ "GB",
585
+ "GD",
586
+ "GE",
587
+ "GF",
588
+ "GG",
589
+ "GH",
590
+ "GI",
591
+ "GL",
592
+ "GM",
593
+ "GN",
594
+ "GP",
595
+ "GQ",
596
+ "GR",
597
+ "GS",
598
+ "GT",
599
+ "GU",
600
+ "GW",
601
+ "GY",
602
+ "HK",
603
+ "HM",
604
+ "HN",
605
+ "HR",
606
+ "HT",
607
+ "HU",
608
+ "ID",
609
+ "IE",
610
+ "IL",
611
+ "IM",
612
+ "IN",
613
+ "IO",
614
+ "IQ",
615
+ "IR",
616
+ "IS",
617
+ "IT",
618
+ "JE",
619
+ "JM",
620
+ "JO",
621
+ "JP",
622
+ "KE",
623
+ "KG",
624
+ "KH",
625
+ "KI",
626
+ "KM",
627
+ "KN",
628
+ "KP",
629
+ "KR",
630
+ "KW",
631
+ "KY",
632
+ "KZ",
633
+ "LA",
634
+ "LB",
635
+ "LC",
636
+ "LI",
637
+ "LK",
638
+ "LR",
639
+ "LS",
640
+ "LT",
641
+ "LU",
642
+ "LV",
643
+ "LY",
644
+ "MA",
645
+ "MC",
646
+ "MD",
647
+ "ME",
648
+ "MF",
649
+ "MG",
650
+ "MH",
651
+ "MK",
652
+ "ML",
653
+ "MM",
654
+ "MN",
655
+ "MO",
656
+ "MP",
657
+ "MQ",
658
+ "MR",
659
+ "MS",
660
+ "MT",
661
+ "MU",
662
+ "MV",
663
+ "MW",
664
+ "MX",
665
+ "MY",
666
+ "MZ",
667
+ "NA",
668
+ "NC",
669
+ "NE",
670
+ "NF",
671
+ "NG",
672
+ "NI",
673
+ "NL",
674
+ "NO",
675
+ "NP",
676
+ "NR",
677
+ "NU",
678
+ "NZ",
679
+ "OM",
680
+ "PA",
681
+ "PE",
682
+ "PF",
683
+ "PG",
684
+ "PH",
685
+ "PK",
686
+ "PL",
687
+ "PM",
688
+ "PN",
689
+ "PR",
690
+ "PS",
691
+ "PT",
692
+ "PW",
693
+ "PY",
694
+ "QA",
695
+ "RE",
696
+ "RO",
697
+ "RS",
698
+ "RU",
699
+ "RW",
700
+ "SA",
701
+ "SB",
702
+ "SC",
703
+ "SD",
704
+ "SE",
705
+ "SG",
706
+ "SH",
707
+ "SI",
708
+ "SJ",
709
+ "SK",
710
+ "SL",
711
+ "SM",
712
+ "SN",
713
+ "SO",
714
+ "SR",
715
+ "SS",
716
+ "ST",
717
+ "SV",
718
+ "SX",
719
+ "SY",
720
+ "SZ",
721
+ "TC",
722
+ "TD",
723
+ "TF",
724
+ "TG",
725
+ "TH",
726
+ "TJ",
727
+ "TK",
728
+ "TL",
729
+ "TM",
730
+ "TN",
731
+ "TO",
732
+ "TR",
733
+ "TT",
734
+ "TV",
735
+ "TW",
736
+ "TZ",
737
+ "UA",
738
+ "UG",
739
+ "UM",
740
+ "US",
741
+ "UY",
742
+ "UZ",
743
+ "VA",
744
+ "VC",
745
+ "VE",
746
+ "VG",
747
+ "VI",
748
+ "VN",
749
+ "VU",
750
+ "WF",
751
+ "WS",
752
+ "YE",
753
+ "YT",
754
+ "ZA",
755
+ "ZM",
756
+ "ZW"
757
+ ]);
758
+ var VALID_NUMERIC_REGION_CODES = /* @__PURE__ */ new Set([
759
+ "001",
760
+ "002",
761
+ "003",
762
+ "005",
763
+ "009",
764
+ "010",
765
+ "011",
766
+ "013",
767
+ "014",
768
+ "015",
769
+ "017",
770
+ "018",
771
+ "019",
772
+ "021",
773
+ "029",
774
+ "030",
775
+ "034",
776
+ "035",
777
+ "039",
778
+ "053",
779
+ "054",
780
+ "057",
781
+ "061",
782
+ "142",
783
+ "143",
784
+ "145",
785
+ "150",
786
+ "151",
787
+ "154",
788
+ "155",
789
+ "202",
790
+ "419",
791
+ "AC",
792
+ "BL",
793
+ "BQ",
794
+ "BV",
795
+ "CP",
796
+ "CW",
797
+ "DG",
798
+ "EA",
799
+ "EU",
800
+ "EZ",
801
+ "FK",
802
+ "FO",
803
+ "GF",
804
+ "GG",
805
+ "GI",
806
+ "GL",
807
+ "GP",
808
+ "GS",
809
+ "GU",
810
+ "HM",
811
+ "IC",
812
+ "IM",
813
+ "IO",
814
+ "JE",
815
+ "KY",
816
+ "MF",
817
+ "MH",
818
+ "MO",
819
+ "MP",
820
+ "MQ",
821
+ "MS",
822
+ "NC",
823
+ "NF",
824
+ "PF",
825
+ "PM",
826
+ "PN",
827
+ "PR",
828
+ "PS",
829
+ "RE",
830
+ "SH",
831
+ "SJ",
832
+ "SX",
833
+ "TC",
834
+ "TF",
835
+ "TK",
836
+ "TL",
837
+ "UM",
838
+ "VA",
839
+ "VC",
840
+ "VG",
841
+ "VI",
842
+ "WF",
843
+ "YT"
844
+ ]);
845
+ function isValidLocale(locale) {
846
+ if (typeof locale !== "string" || !locale.trim()) {
847
+ return false;
848
+ }
849
+ try {
850
+ const match = locale.match(LOCALE_REGEX);
851
+ if (!match) {
852
+ return false;
853
+ }
854
+ const [, language, script, region] = match;
855
+ if (!isValidLanguageCode(language)) {
856
+ return false;
857
+ }
858
+ if (script && !isValidScriptCode(script)) {
859
+ return false;
860
+ }
861
+ if (region && !isValidRegionCode(region)) {
862
+ return false;
863
+ }
864
+ return true;
865
+ } catch {
866
+ return false;
867
+ }
868
+ }
869
+ function isValidLanguageCode(code) {
870
+ if (typeof code !== "string" || !code.trim()) {
871
+ return false;
872
+ }
873
+ return VALID_LANGUAGE_CODES.has(code.toLowerCase());
874
+ }
875
+ function isValidScriptCode(code) {
876
+ if (typeof code !== "string" || !code.trim()) {
877
+ return false;
878
+ }
879
+ return VALID_SCRIPT_CODES.has(code);
880
+ }
881
+ function isValidRegionCode(code) {
882
+ if (typeof code !== "string" || !code.trim()) {
883
+ return false;
884
+ }
885
+ const upperCode = code.toUpperCase();
886
+ return VALID_REGION_CODES.has(upperCode) || VALID_NUMERIC_REGION_CODES.has(upperCode);
887
+ }
888
+
889
+ // src/names/loader.ts
890
+ var CLDR_BASE_URL = process.env.CLDR_BASE_URL || "https://raw.githubusercontent.com/unicode-org/cldr-json/main/cldr-json/cldr-localenames-full/main";
891
+ var cache = /* @__PURE__ */ new Map();
892
+ async function loadTerritoryNames(displayLanguage) {
893
+ const cacheKey = `territories-${displayLanguage}`;
894
+ if (cache.has(cacheKey)) {
895
+ return cache.get(cacheKey);
896
+ }
897
+ try {
898
+ const response = await fetch(
899
+ `${CLDR_BASE_URL}/${displayLanguage}/territories.json`
900
+ );
901
+ if (!response.ok) {
902
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
903
+ }
904
+ const data = await response.json();
905
+ const territories = data?.main?.[displayLanguage]?.localeDisplayNames?.territories || {};
906
+ cache.set(cacheKey, territories);
907
+ return territories;
908
+ } catch (error) {
909
+ if (displayLanguage !== "en") {
910
+ console.warn(
911
+ `Failed to load territory names for ${displayLanguage}, falling back to English`
912
+ );
913
+ return loadTerritoryNames("en");
914
+ }
915
+ throw new Error(
916
+ `Failed to load territory names for ${displayLanguage}: ${error}`
917
+ );
918
+ }
919
+ }
920
+ async function loadLanguageNames(displayLanguage) {
921
+ const cacheKey = `languages-${displayLanguage}`;
922
+ if (cache.has(cacheKey)) {
923
+ return cache.get(cacheKey);
924
+ }
925
+ try {
926
+ const response = await fetch(
927
+ `${CLDR_BASE_URL}/${displayLanguage}/languages.json`
928
+ );
929
+ if (!response.ok) {
930
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
931
+ }
932
+ const data = await response.json();
933
+ const languages = data?.main?.[displayLanguage]?.localeDisplayNames?.languages || {};
934
+ cache.set(cacheKey, languages);
935
+ return languages;
936
+ } catch (error) {
937
+ if (displayLanguage !== "en") {
938
+ console.warn(
939
+ `Failed to load language names for ${displayLanguage}, falling back to English`
940
+ );
941
+ return loadLanguageNames("en");
942
+ }
943
+ throw new Error(
944
+ `Failed to load language names for ${displayLanguage}: ${error}`
945
+ );
946
+ }
947
+ }
948
+ async function loadScriptNames(displayLanguage) {
949
+ const cacheKey = `scripts-${displayLanguage}`;
950
+ if (cache.has(cacheKey)) {
951
+ return cache.get(cacheKey);
952
+ }
953
+ try {
954
+ const response = await fetch(
955
+ `${CLDR_BASE_URL}/${displayLanguage}/scripts.json`
956
+ );
957
+ if (!response.ok) {
958
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
959
+ }
960
+ const data = await response.json();
961
+ const scripts = data?.main?.[displayLanguage]?.localeDisplayNames?.scripts || {};
962
+ const enhancedScripts = { ...scripts };
963
+ if (scripts["Hans-alt-stand-alone"]) {
964
+ enhancedScripts.Hans = scripts["Hans-alt-stand-alone"];
965
+ }
966
+ if (scripts["Hant-alt-stand-alone"]) {
967
+ enhancedScripts.Hant = scripts["Hant-alt-stand-alone"];
968
+ }
969
+ cache.set(cacheKey, enhancedScripts);
970
+ return enhancedScripts;
971
+ } catch (error) {
972
+ if (displayLanguage !== "en") {
973
+ console.warn(
974
+ `Failed to load script names for ${displayLanguage}, falling back to English`
975
+ );
976
+ return loadScriptNames("en");
977
+ }
978
+ throw new Error(
979
+ `Failed to load script names for ${displayLanguage}: ${error}`
980
+ );
981
+ }
982
+ }
983
+
984
+ // src/names/index.ts
985
+ async function getCountryName(countryCode, displayLanguage = "en") {
986
+ if (!countryCode) {
987
+ throw new Error("Country code is required");
988
+ }
989
+ const territories = await loadTerritoryNames(displayLanguage);
990
+ const name = territories[countryCode.toUpperCase()];
991
+ if (!name) {
992
+ throw new Error(`Country code "${countryCode}" not found`);
993
+ }
994
+ return name;
995
+ }
996
+ async function getLanguageName(languageCode, displayLanguage = "en") {
997
+ if (!languageCode) {
998
+ throw new Error("Language code is required");
999
+ }
1000
+ const languages = await loadLanguageNames(displayLanguage);
1001
+ const name = languages[languageCode.toLowerCase()];
1002
+ if (!name) {
1003
+ throw new Error(`Language code "${languageCode}" not found`);
1004
+ }
1005
+ return name;
1006
+ }
1007
+ async function getScriptName(scriptCode, displayLanguage = "en") {
1008
+ if (!scriptCode) {
1009
+ throw new Error("Script code is required");
1010
+ }
1011
+ const scripts = await loadScriptNames(displayLanguage);
1012
+ const name = scripts[scriptCode];
1013
+ if (!name) {
1014
+ throw new Error(`Script code "${scriptCode}" not found`);
1015
+ }
1016
+ return name;
1017
+ }
1018
+ // Annotate the CommonJS export names for ESM import in node:
1019
+ 0 && (module.exports = {
1020
+ LOCALE_REGEX,
1021
+ getCountryName,
1022
+ getLanguageCode,
1023
+ getLanguageName,
1024
+ getRegionCode,
1025
+ getScriptCode,
1026
+ getScriptName,
1027
+ isValidLanguageCode,
1028
+ isValidLocale,
1029
+ isValidRegionCode,
1030
+ isValidScriptCode,
1031
+ parseLocale,
1032
+ parseLocaleWithDetails
1033
+ });