@@ -22,6 +22,7 @@ import { getSection } from "../wikipedia";
2222import * as WeakSpot from "../weak-spot" ;
2323import * as IPAddresses from "../../utils/ip-addresses" ;
2424import * as TestState from "../test-state" ;
25+ import { WordGenError } from "../words-generator" ;
2526
2627export type FunboxFunctions = {
2728 getWord ?: ( wordset ?: Wordset , wordIndex ?: number ) => string ;
@@ -647,9 +648,45 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
647648 } ,
648649 polyglot : {
649650 async withWords ( _words ) {
650- const promises = Config . customPolyglot . map ( JSONData . getLanguage ) ;
651+ const promises = Config . customPolyglot . map ( async ( language ) =>
652+ JSONData . getLanguage ( language ) . catch ( ( ) => {
653+ Notifications . add (
654+ `Failed to load language: ${ language } . It will be ignored.` ,
655+ 0
656+ ) ;
657+ return null ; // Return null for failed languages
658+ } )
659+ ) ;
660+
661+ const languages = ( await Promise . all ( promises ) ) . filter (
662+ ( lang ) => lang !== null
663+ ) ;
664+
665+ if ( languages . length === 0 ) {
666+ UpdateConfig . toggleFunbox ( "polyglot" ) ;
667+ throw new Error (
668+ `No valid languages found. Please check your polyglot languages config (${ Config . customPolyglot . join (
669+ ", "
670+ ) } ).`
671+ ) ;
672+ }
673+
674+ if ( languages . length === 1 ) {
675+ const lang = languages [ 0 ] as JSONData . LanguageObject ;
676+ UpdateConfig . setLanguage ( lang . name , true ) ;
677+ UpdateConfig . toggleFunbox ( "polyglot" , true ) ;
678+ Notifications . add (
679+ `Disabled polyglot funbox because only one valid language was found. Check your polyglot languages config (${ Config . customPolyglot . join (
680+ ", "
681+ ) } ).`,
682+ 0 ,
683+ {
684+ duration : 7 ,
685+ }
686+ ) ;
687+ throw new WordGenError ( "" ) ;
688+ }
651689
652- const languages = await Promise . all ( promises ) ;
653690 const wordSet = languages . flatMap ( ( it ) => it . words ) ;
654691 Arrays . shuffle ( wordSet ) ;
655692 return new Wordset ( wordSet ) ;
0 commit comments