vue-i18n.global.js 239 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547
  1. /*!
  2. * vue-i18n v9.8.0
  3. * (c) 2023 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. var VueI18n = (function (exports, vue) {
  7. 'use strict';
  8. /**
  9. * Original Utilities
  10. * written by kazuya kawaguchi
  11. */
  12. const inBrowser = typeof window !== 'undefined';
  13. let mark;
  14. let measure;
  15. {
  16. const perf = inBrowser && window.performance;
  17. if (perf &&
  18. perf.mark &&
  19. perf.measure &&
  20. perf.clearMarks &&
  21. // @ts-ignore browser compat
  22. perf.clearMeasures) {
  23. mark = (tag) => {
  24. perf.mark(tag);
  25. };
  26. measure = (name, startTag, endTag) => {
  27. perf.measure(name, startTag, endTag);
  28. perf.clearMarks(startTag);
  29. perf.clearMarks(endTag);
  30. };
  31. }
  32. }
  33. const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
  34. /* eslint-disable */
  35. function format$1(message, ...args) {
  36. if (args.length === 1 && isObject(args[0])) {
  37. args = args[0];
  38. }
  39. if (!args || !args.hasOwnProperty) {
  40. args = {};
  41. }
  42. return message.replace(RE_ARGS, (match, identifier) => {
  43. return args.hasOwnProperty(identifier) ? args[identifier] : '';
  44. });
  45. }
  46. const makeSymbol = (name, shareable = false) => !shareable ? Symbol(name) : Symbol.for(name);
  47. const generateFormatCacheKey = (locale, key, source) => friendlyJSONstringify({ l: locale, k: key, s: source });
  48. const friendlyJSONstringify = (json) => JSON.stringify(json)
  49. .replace(/\u2028/g, '\\u2028')
  50. .replace(/\u2029/g, '\\u2029')
  51. .replace(/\u0027/g, '\\u0027');
  52. const isNumber = (val) => typeof val === 'number' && isFinite(val);
  53. const isDate = (val) => toTypeString(val) === '[object Date]';
  54. const isRegExp = (val) => toTypeString(val) === '[object RegExp]';
  55. const isEmptyObject = (val) => isPlainObject(val) && Object.keys(val).length === 0;
  56. const assign = Object.assign;
  57. let _globalThis;
  58. const getGlobalThis = () => {
  59. // prettier-ignore
  60. return (_globalThis ||
  61. (_globalThis =
  62. typeof globalThis !== 'undefined'
  63. ? globalThis
  64. : typeof self !== 'undefined'
  65. ? self
  66. : typeof window !== 'undefined'
  67. ? window
  68. : typeof global !== 'undefined'
  69. ? global
  70. : {}));
  71. };
  72. function escapeHtml(rawText) {
  73. return rawText
  74. .replace(/</g, '&lt;')
  75. .replace(/>/g, '&gt;')
  76. .replace(/"/g, '&quot;')
  77. .replace(/'/g, '&apos;');
  78. }
  79. const hasOwnProperty = Object.prototype.hasOwnProperty;
  80. function hasOwn(obj, key) {
  81. return hasOwnProperty.call(obj, key);
  82. }
  83. /* eslint-enable */
  84. /**
  85. * Useful Utilities By Evan you
  86. * Modified by kazuya kawaguchi
  87. * MIT License
  88. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
  89. * https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
  90. */
  91. const isArray = Array.isArray;
  92. const isFunction = (val) => typeof val === 'function';
  93. const isString = (val) => typeof val === 'string';
  94. const isBoolean = (val) => typeof val === 'boolean';
  95. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  96. const isObject = (val) => val !== null && typeof val === 'object';
  97. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  98. const isPromise = (val) => {
  99. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  100. };
  101. const objectToString = Object.prototype.toString;
  102. const toTypeString = (value) => objectToString.call(value);
  103. const isPlainObject = (val) => {
  104. if (!isObject(val))
  105. return false;
  106. const proto = Object.getPrototypeOf(val);
  107. return proto === null || proto.constructor === Object;
  108. };
  109. // for converting list and named values to displayed strings.
  110. const toDisplayString = (val) => {
  111. return val == null
  112. ? ''
  113. : isArray(val) || (isPlainObject(val) && val.toString === objectToString)
  114. ? JSON.stringify(val, null, 2)
  115. : String(val);
  116. };
  117. function join(items, separator = '') {
  118. return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '');
  119. }
  120. const RANGE = 2;
  121. function generateCodeFrame(source, start = 0, end = source.length) {
  122. const lines = source.split(/\r?\n/);
  123. let count = 0;
  124. const res = [];
  125. for (let i = 0; i < lines.length; i++) {
  126. count += lines[i].length + 1;
  127. if (count >= start) {
  128. for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
  129. if (j < 0 || j >= lines.length)
  130. continue;
  131. const line = j + 1;
  132. res.push(`${line}${' '.repeat(3 - String(line).length)}| ${lines[j]}`);
  133. const lineLength = lines[j].length;
  134. if (j === i) {
  135. // push underline
  136. const pad = start - (count - lineLength) + 1;
  137. const length = Math.max(1, end > count ? lineLength - pad : end - start);
  138. res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length));
  139. }
  140. else if (j > i) {
  141. if (end > count) {
  142. const length = Math.max(Math.min(end - count, lineLength), 1);
  143. res.push(` | ` + '^'.repeat(length));
  144. }
  145. count += lineLength + 1;
  146. }
  147. }
  148. break;
  149. }
  150. }
  151. return res.join('\n');
  152. }
  153. function incrementer(code) {
  154. let current = code;
  155. return () => ++current;
  156. }
  157. function warn(msg, err) {
  158. if (typeof console !== 'undefined') {
  159. console.warn(`[intlify] ` + msg);
  160. /* istanbul ignore if */
  161. if (err) {
  162. console.warn(err.stack);
  163. }
  164. }
  165. }
  166. /**
  167. * Event emitter, forked from the below:
  168. * - original repository url: https://github.com/developit/mitt
  169. * - code url: https://github.com/developit/mitt/blob/master/src/index.ts
  170. * - author: Jason Miller (https://github.com/developit)
  171. * - license: MIT
  172. */
  173. /**
  174. * Create a event emitter
  175. *
  176. * @returns An event emitter
  177. */
  178. function createEmitter() {
  179. const events = new Map();
  180. const emitter = {
  181. events,
  182. on(event, handler) {
  183. const handlers = events.get(event);
  184. const added = handlers && handlers.push(handler);
  185. if (!added) {
  186. events.set(event, [handler]);
  187. }
  188. },
  189. off(event, handler) {
  190. const handlers = events.get(event);
  191. if (handlers) {
  192. handlers.splice(handlers.indexOf(handler) >>> 0, 1);
  193. }
  194. },
  195. emit(event, payload) {
  196. (events.get(event) || [])
  197. .slice()
  198. .map(handler => handler(payload));
  199. (events.get('*') || [])
  200. .slice()
  201. .map(handler => handler(event, payload));
  202. }
  203. };
  204. return emitter;
  205. }
  206. const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
  207. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  208. function deepCopy(src, des) {
  209. // src and des should both be objects, and none of them can be a array
  210. if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
  211. throw new Error('Invalid value');
  212. }
  213. for (const key in src) {
  214. if (hasOwn(src, key)) {
  215. if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
  216. // replace with src[key] when:
  217. // src[key] or des[key] is not an object, or
  218. // src[key] or des[key] is an array
  219. des[key] = src[key];
  220. }
  221. else {
  222. // src[key] and des[key] are both objects, merge them
  223. deepCopy(src[key], des[key]);
  224. }
  225. }
  226. }
  227. }
  228. function createPosition(line, column, offset) {
  229. return { line, column, offset };
  230. }
  231. function createLocation(start, end, source) {
  232. const loc = { start, end };
  233. if (source != null) {
  234. loc.source = source;
  235. }
  236. return loc;
  237. }
  238. const CompileErrorCodes = {
  239. // tokenizer error codes
  240. EXPECTED_TOKEN: 1,
  241. INVALID_TOKEN_IN_PLACEHOLDER: 2,
  242. UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3,
  243. UNKNOWN_ESCAPE_SEQUENCE: 4,
  244. INVALID_UNICODE_ESCAPE_SEQUENCE: 5,
  245. UNBALANCED_CLOSING_BRACE: 6,
  246. UNTERMINATED_CLOSING_BRACE: 7,
  247. EMPTY_PLACEHOLDER: 8,
  248. NOT_ALLOW_NEST_PLACEHOLDER: 9,
  249. INVALID_LINKED_FORMAT: 10,
  250. // parser error codes
  251. MUST_HAVE_MESSAGES_IN_PLURAL: 11,
  252. UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
  253. UNEXPECTED_EMPTY_LINKED_KEY: 13,
  254. UNEXPECTED_LEXICAL_ANALYSIS: 14,
  255. // generator error codes
  256. UNHANDLED_CODEGEN_NODE_TYPE: 15,
  257. // minifier error codes
  258. UNHANDLED_MINIFIER_NODE_TYPE: 16,
  259. // Special value for higher-order compilers to pick up the last code
  260. // to avoid collision of error codes. This should always be kept as the last
  261. // item.
  262. __EXTEND_POINT__: 17
  263. };
  264. /** @internal */
  265. const errorMessages$2 = {
  266. // tokenizer error messages
  267. [CompileErrorCodes.EXPECTED_TOKEN]: `Expected token: '{0}'`,
  268. [CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]: `Invalid token in placeholder: '{0}'`,
  269. [CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]: `Unterminated single quote in placeholder`,
  270. [CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]: `Unknown escape sequence: \\{0}`,
  271. [CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]: `Invalid unicode escape sequence: {0}`,
  272. [CompileErrorCodes.UNBALANCED_CLOSING_BRACE]: `Unbalanced closing brace`,
  273. [CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]: `Unterminated closing brace`,
  274. [CompileErrorCodes.EMPTY_PLACEHOLDER]: `Empty placeholder`,
  275. [CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]: `Not allowed nest placeholder`,
  276. [CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
  277. // parser error messages
  278. [CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
  279. [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
  280. [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
  281. [CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`,
  282. // generator error messages
  283. [CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]: `unhandled codegen node type: '{0}'`,
  284. // minimizer error messages
  285. [CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]: `unhandled mimifier node type: '{0}'`
  286. };
  287. function createCompileError(code, loc, options = {}) {
  288. const { domain, messages, args } = options;
  289. const msg = format$1((messages || errorMessages$2)[code] || '', ...(args || []))
  290. ;
  291. const error = new SyntaxError(String(msg));
  292. error.code = code;
  293. if (loc) {
  294. error.location = loc;
  295. }
  296. error.domain = domain;
  297. return error;
  298. }
  299. /** @internal */
  300. function defaultOnError(error) {
  301. throw error;
  302. }
  303. const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
  304. const detectHtmlTag = (source) => RE_HTML_TAG.test(source);
  305. const CHAR_SP = ' ';
  306. const CHAR_CR = '\r';
  307. const CHAR_LF = '\n';
  308. const CHAR_LS = String.fromCharCode(0x2028);
  309. const CHAR_PS = String.fromCharCode(0x2029);
  310. function createScanner(str) {
  311. const _buf = str;
  312. let _index = 0;
  313. let _line = 1;
  314. let _column = 1;
  315. let _peekOffset = 0;
  316. const isCRLF = (index) => _buf[index] === CHAR_CR && _buf[index + 1] === CHAR_LF;
  317. const isLF = (index) => _buf[index] === CHAR_LF;
  318. const isPS = (index) => _buf[index] === CHAR_PS;
  319. const isLS = (index) => _buf[index] === CHAR_LS;
  320. const isLineEnd = (index) => isCRLF(index) || isLF(index) || isPS(index) || isLS(index);
  321. const index = () => _index;
  322. const line = () => _line;
  323. const column = () => _column;
  324. const peekOffset = () => _peekOffset;
  325. const charAt = (offset) => isCRLF(offset) || isPS(offset) || isLS(offset) ? CHAR_LF : _buf[offset];
  326. const currentChar = () => charAt(_index);
  327. const currentPeek = () => charAt(_index + _peekOffset);
  328. function next() {
  329. _peekOffset = 0;
  330. if (isLineEnd(_index)) {
  331. _line++;
  332. _column = 0;
  333. }
  334. if (isCRLF(_index)) {
  335. _index++;
  336. }
  337. _index++;
  338. _column++;
  339. return _buf[_index];
  340. }
  341. function peek() {
  342. if (isCRLF(_index + _peekOffset)) {
  343. _peekOffset++;
  344. }
  345. _peekOffset++;
  346. return _buf[_index + _peekOffset];
  347. }
  348. function reset() {
  349. _index = 0;
  350. _line = 1;
  351. _column = 1;
  352. _peekOffset = 0;
  353. }
  354. function resetPeek(offset = 0) {
  355. _peekOffset = offset;
  356. }
  357. function skipToPeek() {
  358. const target = _index + _peekOffset;
  359. // eslint-disable-next-line no-unmodified-loop-condition
  360. while (target !== _index) {
  361. next();
  362. }
  363. _peekOffset = 0;
  364. }
  365. return {
  366. index,
  367. line,
  368. column,
  369. peekOffset,
  370. charAt,
  371. currentChar,
  372. currentPeek,
  373. next,
  374. peek,
  375. reset,
  376. resetPeek,
  377. skipToPeek
  378. };
  379. }
  380. const EOF = undefined;
  381. const DOT = '.';
  382. const LITERAL_DELIMITER = "'";
  383. const ERROR_DOMAIN$3 = 'tokenizer';
  384. function createTokenizer(source, options = {}) {
  385. const location = options.location !== false;
  386. const _scnr = createScanner(source);
  387. const currentOffset = () => _scnr.index();
  388. const currentPosition = () => createPosition(_scnr.line(), _scnr.column(), _scnr.index());
  389. const _initLoc = currentPosition();
  390. const _initOffset = currentOffset();
  391. const _context = {
  392. currentType: 14 /* TokenTypes.EOF */,
  393. offset: _initOffset,
  394. startLoc: _initLoc,
  395. endLoc: _initLoc,
  396. lastType: 14 /* TokenTypes.EOF */,
  397. lastOffset: _initOffset,
  398. lastStartLoc: _initLoc,
  399. lastEndLoc: _initLoc,
  400. braceNest: 0,
  401. inLinked: false,
  402. text: ''
  403. };
  404. const context = () => _context;
  405. const { onError } = options;
  406. function emitError(code, pos, offset, ...args) {
  407. const ctx = context();
  408. pos.column += offset;
  409. pos.offset += offset;
  410. if (onError) {
  411. const loc = location ? createLocation(ctx.startLoc, pos) : null;
  412. const err = createCompileError(code, loc, {
  413. domain: ERROR_DOMAIN$3,
  414. args
  415. });
  416. onError(err);
  417. }
  418. }
  419. function getToken(context, type, value) {
  420. context.endLoc = currentPosition();
  421. context.currentType = type;
  422. const token = { type };
  423. if (location) {
  424. token.loc = createLocation(context.startLoc, context.endLoc);
  425. }
  426. if (value != null) {
  427. token.value = value;
  428. }
  429. return token;
  430. }
  431. const getEndToken = (context) => getToken(context, 14 /* TokenTypes.EOF */);
  432. function eat(scnr, ch) {
  433. if (scnr.currentChar() === ch) {
  434. scnr.next();
  435. return ch;
  436. }
  437. else {
  438. emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
  439. return '';
  440. }
  441. }
  442. function peekSpaces(scnr) {
  443. let buf = '';
  444. while (scnr.currentPeek() === CHAR_SP || scnr.currentPeek() === CHAR_LF) {
  445. buf += scnr.currentPeek();
  446. scnr.peek();
  447. }
  448. return buf;
  449. }
  450. function skipSpaces(scnr) {
  451. const buf = peekSpaces(scnr);
  452. scnr.skipToPeek();
  453. return buf;
  454. }
  455. function isIdentifierStart(ch) {
  456. if (ch === EOF) {
  457. return false;
  458. }
  459. const cc = ch.charCodeAt(0);
  460. return ((cc >= 97 && cc <= 122) || // a-z
  461. (cc >= 65 && cc <= 90) || // A-Z
  462. cc === 95 // _
  463. );
  464. }
  465. function isNumberStart(ch) {
  466. if (ch === EOF) {
  467. return false;
  468. }
  469. const cc = ch.charCodeAt(0);
  470. return cc >= 48 && cc <= 57; // 0-9
  471. }
  472. function isNamedIdentifierStart(scnr, context) {
  473. const { currentType } = context;
  474. if (currentType !== 2 /* TokenTypes.BraceLeft */) {
  475. return false;
  476. }
  477. peekSpaces(scnr);
  478. const ret = isIdentifierStart(scnr.currentPeek());
  479. scnr.resetPeek();
  480. return ret;
  481. }
  482. function isListIdentifierStart(scnr, context) {
  483. const { currentType } = context;
  484. if (currentType !== 2 /* TokenTypes.BraceLeft */) {
  485. return false;
  486. }
  487. peekSpaces(scnr);
  488. const ch = scnr.currentPeek() === '-' ? scnr.peek() : scnr.currentPeek();
  489. const ret = isNumberStart(ch);
  490. scnr.resetPeek();
  491. return ret;
  492. }
  493. function isLiteralStart(scnr, context) {
  494. const { currentType } = context;
  495. if (currentType !== 2 /* TokenTypes.BraceLeft */) {
  496. return false;
  497. }
  498. peekSpaces(scnr);
  499. const ret = scnr.currentPeek() === LITERAL_DELIMITER;
  500. scnr.resetPeek();
  501. return ret;
  502. }
  503. function isLinkedDotStart(scnr, context) {
  504. const { currentType } = context;
  505. if (currentType !== 8 /* TokenTypes.LinkedAlias */) {
  506. return false;
  507. }
  508. peekSpaces(scnr);
  509. const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
  510. scnr.resetPeek();
  511. return ret;
  512. }
  513. function isLinkedModifierStart(scnr, context) {
  514. const { currentType } = context;
  515. if (currentType !== 9 /* TokenTypes.LinkedDot */) {
  516. return false;
  517. }
  518. peekSpaces(scnr);
  519. const ret = isIdentifierStart(scnr.currentPeek());
  520. scnr.resetPeek();
  521. return ret;
  522. }
  523. function isLinkedDelimiterStart(scnr, context) {
  524. const { currentType } = context;
  525. if (!(currentType === 8 /* TokenTypes.LinkedAlias */ ||
  526. currentType === 12 /* TokenTypes.LinkedModifier */)) {
  527. return false;
  528. }
  529. peekSpaces(scnr);
  530. const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
  531. scnr.resetPeek();
  532. return ret;
  533. }
  534. function isLinkedReferStart(scnr, context) {
  535. const { currentType } = context;
  536. if (currentType !== 10 /* TokenTypes.LinkedDelimiter */) {
  537. return false;
  538. }
  539. const fn = () => {
  540. const ch = scnr.currentPeek();
  541. if (ch === "{" /* TokenChars.BraceLeft */) {
  542. return isIdentifierStart(scnr.peek());
  543. }
  544. else if (ch === "@" /* TokenChars.LinkedAlias */ ||
  545. ch === "%" /* TokenChars.Modulo */ ||
  546. ch === "|" /* TokenChars.Pipe */ ||
  547. ch === ":" /* TokenChars.LinkedDelimiter */ ||
  548. ch === "." /* TokenChars.LinkedDot */ ||
  549. ch === CHAR_SP ||
  550. !ch) {
  551. return false;
  552. }
  553. else if (ch === CHAR_LF) {
  554. scnr.peek();
  555. return fn();
  556. }
  557. else {
  558. // other characters
  559. return isIdentifierStart(ch);
  560. }
  561. };
  562. const ret = fn();
  563. scnr.resetPeek();
  564. return ret;
  565. }
  566. function isPluralStart(scnr) {
  567. peekSpaces(scnr);
  568. const ret = scnr.currentPeek() === "|" /* TokenChars.Pipe */;
  569. scnr.resetPeek();
  570. return ret;
  571. }
  572. function detectModuloStart(scnr) {
  573. const spaces = peekSpaces(scnr);
  574. const ret = scnr.currentPeek() === "%" /* TokenChars.Modulo */ &&
  575. scnr.peek() === "{" /* TokenChars.BraceLeft */;
  576. scnr.resetPeek();
  577. return {
  578. isModulo: ret,
  579. hasSpace: spaces.length > 0
  580. };
  581. }
  582. function isTextStart(scnr, reset = true) {
  583. const fn = (hasSpace = false, prev = '', detectModulo = false) => {
  584. const ch = scnr.currentPeek();
  585. if (ch === "{" /* TokenChars.BraceLeft */) {
  586. return prev === "%" /* TokenChars.Modulo */ ? false : hasSpace;
  587. }
  588. else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
  589. return prev === "%" /* TokenChars.Modulo */ ? true : hasSpace;
  590. }
  591. else if (ch === "%" /* TokenChars.Modulo */) {
  592. scnr.peek();
  593. return fn(hasSpace, "%" /* TokenChars.Modulo */, true);
  594. }
  595. else if (ch === "|" /* TokenChars.Pipe */) {
  596. return prev === "%" /* TokenChars.Modulo */ || detectModulo
  597. ? true
  598. : !(prev === CHAR_SP || prev === CHAR_LF);
  599. }
  600. else if (ch === CHAR_SP) {
  601. scnr.peek();
  602. return fn(true, CHAR_SP, detectModulo);
  603. }
  604. else if (ch === CHAR_LF) {
  605. scnr.peek();
  606. return fn(true, CHAR_LF, detectModulo);
  607. }
  608. else {
  609. return true;
  610. }
  611. };
  612. const ret = fn();
  613. reset && scnr.resetPeek();
  614. return ret;
  615. }
  616. function takeChar(scnr, fn) {
  617. const ch = scnr.currentChar();
  618. if (ch === EOF) {
  619. return EOF;
  620. }
  621. if (fn(ch)) {
  622. scnr.next();
  623. return ch;
  624. }
  625. return null;
  626. }
  627. function takeIdentifierChar(scnr) {
  628. const closure = (ch) => {
  629. const cc = ch.charCodeAt(0);
  630. return ((cc >= 97 && cc <= 122) || // a-z
  631. (cc >= 65 && cc <= 90) || // A-Z
  632. (cc >= 48 && cc <= 57) || // 0-9
  633. cc === 95 || // _
  634. cc === 36 // $
  635. );
  636. };
  637. return takeChar(scnr, closure);
  638. }
  639. function takeDigit(scnr) {
  640. const closure = (ch) => {
  641. const cc = ch.charCodeAt(0);
  642. return cc >= 48 && cc <= 57; // 0-9
  643. };
  644. return takeChar(scnr, closure);
  645. }
  646. function takeHexDigit(scnr) {
  647. const closure = (ch) => {
  648. const cc = ch.charCodeAt(0);
  649. return ((cc >= 48 && cc <= 57) || // 0-9
  650. (cc >= 65 && cc <= 70) || // A-F
  651. (cc >= 97 && cc <= 102)); // a-f
  652. };
  653. return takeChar(scnr, closure);
  654. }
  655. function getDigits(scnr) {
  656. let ch = '';
  657. let num = '';
  658. while ((ch = takeDigit(scnr))) {
  659. num += ch;
  660. }
  661. return num;
  662. }
  663. function readModulo(scnr) {
  664. skipSpaces(scnr);
  665. const ch = scnr.currentChar();
  666. if (ch !== "%" /* TokenChars.Modulo */) {
  667. emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
  668. }
  669. scnr.next();
  670. return "%" /* TokenChars.Modulo */;
  671. }
  672. function readText(scnr) {
  673. let buf = '';
  674. while (true) {
  675. const ch = scnr.currentChar();
  676. if (ch === "{" /* TokenChars.BraceLeft */ ||
  677. ch === "}" /* TokenChars.BraceRight */ ||
  678. ch === "@" /* TokenChars.LinkedAlias */ ||
  679. ch === "|" /* TokenChars.Pipe */ ||
  680. !ch) {
  681. break;
  682. }
  683. else if (ch === "%" /* TokenChars.Modulo */) {
  684. if (isTextStart(scnr)) {
  685. buf += ch;
  686. scnr.next();
  687. }
  688. else {
  689. break;
  690. }
  691. }
  692. else if (ch === CHAR_SP || ch === CHAR_LF) {
  693. if (isTextStart(scnr)) {
  694. buf += ch;
  695. scnr.next();
  696. }
  697. else if (isPluralStart(scnr)) {
  698. break;
  699. }
  700. else {
  701. buf += ch;
  702. scnr.next();
  703. }
  704. }
  705. else {
  706. buf += ch;
  707. scnr.next();
  708. }
  709. }
  710. return buf;
  711. }
  712. function readNamedIdentifier(scnr) {
  713. skipSpaces(scnr);
  714. let ch = '';
  715. let name = '';
  716. while ((ch = takeIdentifierChar(scnr))) {
  717. name += ch;
  718. }
  719. if (scnr.currentChar() === EOF) {
  720. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  721. }
  722. return name;
  723. }
  724. function readListIdentifier(scnr) {
  725. skipSpaces(scnr);
  726. let value = '';
  727. if (scnr.currentChar() === '-') {
  728. scnr.next();
  729. value += `-${getDigits(scnr)}`;
  730. }
  731. else {
  732. value += getDigits(scnr);
  733. }
  734. if (scnr.currentChar() === EOF) {
  735. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  736. }
  737. return value;
  738. }
  739. function readLiteral(scnr) {
  740. skipSpaces(scnr);
  741. eat(scnr, `\'`);
  742. let ch = '';
  743. let literal = '';
  744. const fn = (x) => x !== LITERAL_DELIMITER && x !== CHAR_LF;
  745. while ((ch = takeChar(scnr, fn))) {
  746. if (ch === '\\') {
  747. literal += readEscapeSequence(scnr);
  748. }
  749. else {
  750. literal += ch;
  751. }
  752. }
  753. const current = scnr.currentChar();
  754. if (current === CHAR_LF || current === EOF) {
  755. emitError(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER, currentPosition(), 0);
  756. // TODO: Is it correct really?
  757. if (current === CHAR_LF) {
  758. scnr.next();
  759. eat(scnr, `\'`);
  760. }
  761. return literal;
  762. }
  763. eat(scnr, `\'`);
  764. return literal;
  765. }
  766. function readEscapeSequence(scnr) {
  767. const ch = scnr.currentChar();
  768. switch (ch) {
  769. case '\\':
  770. case `\'`:
  771. scnr.next();
  772. return `\\${ch}`;
  773. case 'u':
  774. return readUnicodeEscapeSequence(scnr, ch, 4);
  775. case 'U':
  776. return readUnicodeEscapeSequence(scnr, ch, 6);
  777. default:
  778. emitError(CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE, currentPosition(), 0, ch);
  779. return '';
  780. }
  781. }
  782. function readUnicodeEscapeSequence(scnr, unicode, digits) {
  783. eat(scnr, unicode);
  784. let sequence = '';
  785. for (let i = 0; i < digits; i++) {
  786. const ch = takeHexDigit(scnr);
  787. if (!ch) {
  788. emitError(CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE, currentPosition(), 0, `\\${unicode}${sequence}${scnr.currentChar()}`);
  789. break;
  790. }
  791. sequence += ch;
  792. }
  793. return `\\${unicode}${sequence}`;
  794. }
  795. function readInvalidIdentifier(scnr) {
  796. skipSpaces(scnr);
  797. let ch = '';
  798. let identifiers = '';
  799. const closure = (ch) => ch !== "{" /* TokenChars.BraceLeft */ &&
  800. ch !== "}" /* TokenChars.BraceRight */ &&
  801. ch !== CHAR_SP &&
  802. ch !== CHAR_LF;
  803. while ((ch = takeChar(scnr, closure))) {
  804. identifiers += ch;
  805. }
  806. return identifiers;
  807. }
  808. function readLinkedModifier(scnr) {
  809. let ch = '';
  810. let name = '';
  811. while ((ch = takeIdentifierChar(scnr))) {
  812. name += ch;
  813. }
  814. return name;
  815. }
  816. function readLinkedRefer(scnr) {
  817. const fn = (detect = false, buf) => {
  818. const ch = scnr.currentChar();
  819. if (ch === "{" /* TokenChars.BraceLeft */ ||
  820. ch === "%" /* TokenChars.Modulo */ ||
  821. ch === "@" /* TokenChars.LinkedAlias */ ||
  822. ch === "|" /* TokenChars.Pipe */ ||
  823. ch === "(" /* TokenChars.ParenLeft */ ||
  824. ch === ")" /* TokenChars.ParenRight */ ||
  825. !ch) {
  826. return buf;
  827. }
  828. else if (ch === CHAR_SP) {
  829. return buf;
  830. }
  831. else if (ch === CHAR_LF || ch === DOT) {
  832. buf += ch;
  833. scnr.next();
  834. return fn(detect, buf);
  835. }
  836. else {
  837. buf += ch;
  838. scnr.next();
  839. return fn(true, buf);
  840. }
  841. };
  842. return fn(false, '');
  843. }
  844. function readPlural(scnr) {
  845. skipSpaces(scnr);
  846. const plural = eat(scnr, "|" /* TokenChars.Pipe */);
  847. skipSpaces(scnr);
  848. return plural;
  849. }
  850. // TODO: We need refactoring of token parsing ...
  851. function readTokenInPlaceholder(scnr, context) {
  852. let token = null;
  853. const ch = scnr.currentChar();
  854. switch (ch) {
  855. case "{" /* TokenChars.BraceLeft */:
  856. if (context.braceNest >= 1) {
  857. emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
  858. }
  859. scnr.next();
  860. token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
  861. skipSpaces(scnr);
  862. context.braceNest++;
  863. return token;
  864. case "}" /* TokenChars.BraceRight */:
  865. if (context.braceNest > 0 &&
  866. context.currentType === 2 /* TokenTypes.BraceLeft */) {
  867. emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
  868. }
  869. scnr.next();
  870. token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
  871. context.braceNest--;
  872. context.braceNest > 0 && skipSpaces(scnr);
  873. if (context.inLinked && context.braceNest === 0) {
  874. context.inLinked = false;
  875. }
  876. return token;
  877. case "@" /* TokenChars.LinkedAlias */:
  878. if (context.braceNest > 0) {
  879. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  880. }
  881. token = readTokenInLinked(scnr, context) || getEndToken(context);
  882. context.braceNest = 0;
  883. return token;
  884. default:
  885. let validNamedIdentifier = true;
  886. let validListIdentifier = true;
  887. let validLiteral = true;
  888. if (isPluralStart(scnr)) {
  889. if (context.braceNest > 0) {
  890. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  891. }
  892. token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
  893. // reset
  894. context.braceNest = 0;
  895. context.inLinked = false;
  896. return token;
  897. }
  898. if (context.braceNest > 0 &&
  899. (context.currentType === 5 /* TokenTypes.Named */ ||
  900. context.currentType === 6 /* TokenTypes.List */ ||
  901. context.currentType === 7 /* TokenTypes.Literal */)) {
  902. emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
  903. context.braceNest = 0;
  904. return readToken(scnr, context);
  905. }
  906. if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
  907. token = getToken(context, 5 /* TokenTypes.Named */, readNamedIdentifier(scnr));
  908. skipSpaces(scnr);
  909. return token;
  910. }
  911. if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
  912. token = getToken(context, 6 /* TokenTypes.List */, readListIdentifier(scnr));
  913. skipSpaces(scnr);
  914. return token;
  915. }
  916. if ((validLiteral = isLiteralStart(scnr, context))) {
  917. token = getToken(context, 7 /* TokenTypes.Literal */, readLiteral(scnr));
  918. skipSpaces(scnr);
  919. return token;
  920. }
  921. if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
  922. // TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
  923. token = getToken(context, 13 /* TokenTypes.InvalidPlace */, readInvalidIdentifier(scnr));
  924. emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
  925. skipSpaces(scnr);
  926. return token;
  927. }
  928. break;
  929. }
  930. return token;
  931. }
  932. // TODO: We need refactoring of token parsing ...
  933. function readTokenInLinked(scnr, context) {
  934. const { currentType } = context;
  935. let token = null;
  936. const ch = scnr.currentChar();
  937. if ((currentType === 8 /* TokenTypes.LinkedAlias */ ||
  938. currentType === 9 /* TokenTypes.LinkedDot */ ||
  939. currentType === 12 /* TokenTypes.LinkedModifier */ ||
  940. currentType === 10 /* TokenTypes.LinkedDelimiter */) &&
  941. (ch === CHAR_LF || ch === CHAR_SP)) {
  942. emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
  943. }
  944. switch (ch) {
  945. case "@" /* TokenChars.LinkedAlias */:
  946. scnr.next();
  947. token = getToken(context, 8 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
  948. context.inLinked = true;
  949. return token;
  950. case "." /* TokenChars.LinkedDot */:
  951. skipSpaces(scnr);
  952. scnr.next();
  953. return getToken(context, 9 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
  954. case ":" /* TokenChars.LinkedDelimiter */:
  955. skipSpaces(scnr);
  956. scnr.next();
  957. return getToken(context, 10 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
  958. default:
  959. if (isPluralStart(scnr)) {
  960. token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
  961. // reset
  962. context.braceNest = 0;
  963. context.inLinked = false;
  964. return token;
  965. }
  966. if (isLinkedDotStart(scnr, context) ||
  967. isLinkedDelimiterStart(scnr, context)) {
  968. skipSpaces(scnr);
  969. return readTokenInLinked(scnr, context);
  970. }
  971. if (isLinkedModifierStart(scnr, context)) {
  972. skipSpaces(scnr);
  973. return getToken(context, 12 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
  974. }
  975. if (isLinkedReferStart(scnr, context)) {
  976. skipSpaces(scnr);
  977. if (ch === "{" /* TokenChars.BraceLeft */) {
  978. // scan the placeholder
  979. return readTokenInPlaceholder(scnr, context) || token;
  980. }
  981. else {
  982. return getToken(context, 11 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
  983. }
  984. }
  985. if (currentType === 8 /* TokenTypes.LinkedAlias */) {
  986. emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
  987. }
  988. context.braceNest = 0;
  989. context.inLinked = false;
  990. return readToken(scnr, context);
  991. }
  992. }
  993. // TODO: We need refactoring of token parsing ...
  994. function readToken(scnr, context) {
  995. let token = { type: 14 /* TokenTypes.EOF */ };
  996. if (context.braceNest > 0) {
  997. return readTokenInPlaceholder(scnr, context) || getEndToken(context);
  998. }
  999. if (context.inLinked) {
  1000. return readTokenInLinked(scnr, context) || getEndToken(context);
  1001. }
  1002. const ch = scnr.currentChar();
  1003. switch (ch) {
  1004. case "{" /* TokenChars.BraceLeft */:
  1005. return readTokenInPlaceholder(scnr, context) || getEndToken(context);
  1006. case "}" /* TokenChars.BraceRight */:
  1007. emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
  1008. scnr.next();
  1009. return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
  1010. case "@" /* TokenChars.LinkedAlias */:
  1011. return readTokenInLinked(scnr, context) || getEndToken(context);
  1012. default:
  1013. if (isPluralStart(scnr)) {
  1014. token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
  1015. // reset
  1016. context.braceNest = 0;
  1017. context.inLinked = false;
  1018. return token;
  1019. }
  1020. const { isModulo, hasSpace } = detectModuloStart(scnr);
  1021. if (isModulo) {
  1022. return hasSpace
  1023. ? getToken(context, 0 /* TokenTypes.Text */, readText(scnr))
  1024. : getToken(context, 4 /* TokenTypes.Modulo */, readModulo(scnr));
  1025. }
  1026. if (isTextStart(scnr)) {
  1027. return getToken(context, 0 /* TokenTypes.Text */, readText(scnr));
  1028. }
  1029. break;
  1030. }
  1031. return token;
  1032. }
  1033. function nextToken() {
  1034. const { currentType, offset, startLoc, endLoc } = _context;
  1035. _context.lastType = currentType;
  1036. _context.lastOffset = offset;
  1037. _context.lastStartLoc = startLoc;
  1038. _context.lastEndLoc = endLoc;
  1039. _context.offset = currentOffset();
  1040. _context.startLoc = currentPosition();
  1041. if (_scnr.currentChar() === EOF) {
  1042. return getToken(_context, 14 /* TokenTypes.EOF */);
  1043. }
  1044. return readToken(_scnr, _context);
  1045. }
  1046. return {
  1047. nextToken,
  1048. currentOffset,
  1049. currentPosition,
  1050. context
  1051. };
  1052. }
  1053. const ERROR_DOMAIN$2 = 'parser';
  1054. // Backslash backslash, backslash quote, uHHHH, UHHHHHH.
  1055. const KNOWN_ESCAPES = /(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;
  1056. function fromEscapeSequence(match, codePoint4, codePoint6) {
  1057. switch (match) {
  1058. case `\\\\`:
  1059. return `\\`;
  1060. case `\\\'`:
  1061. return `\'`;
  1062. default: {
  1063. const codePoint = parseInt(codePoint4 || codePoint6, 16);
  1064. if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
  1065. return String.fromCodePoint(codePoint);
  1066. }
  1067. // invalid ...
  1068. // Replace them with U+FFFD REPLACEMENT CHARACTER.
  1069. return '�';
  1070. }
  1071. }
  1072. }
  1073. function createParser(options = {}) {
  1074. const location = options.location !== false;
  1075. const { onError } = options;
  1076. function emitError(tokenzer, code, start, offset, ...args) {
  1077. const end = tokenzer.currentPosition();
  1078. end.offset += offset;
  1079. end.column += offset;
  1080. if (onError) {
  1081. const loc = location ? createLocation(start, end) : null;
  1082. const err = createCompileError(code, loc, {
  1083. domain: ERROR_DOMAIN$2,
  1084. args
  1085. });
  1086. onError(err);
  1087. }
  1088. }
  1089. function startNode(type, offset, loc) {
  1090. const node = { type };
  1091. if (location) {
  1092. node.start = offset;
  1093. node.end = offset;
  1094. node.loc = { start: loc, end: loc };
  1095. }
  1096. return node;
  1097. }
  1098. function endNode(node, offset, pos, type) {
  1099. if (type) {
  1100. node.type = type;
  1101. }
  1102. if (location) {
  1103. node.end = offset;
  1104. if (node.loc) {
  1105. node.loc.end = pos;
  1106. }
  1107. }
  1108. }
  1109. function parseText(tokenizer, value) {
  1110. const context = tokenizer.context();
  1111. const node = startNode(3 /* NodeTypes.Text */, context.offset, context.startLoc);
  1112. node.value = value;
  1113. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1114. return node;
  1115. }
  1116. function parseList(tokenizer, index) {
  1117. const context = tokenizer.context();
  1118. const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
  1119. const node = startNode(5 /* NodeTypes.List */, offset, loc);
  1120. node.index = parseInt(index, 10);
  1121. tokenizer.nextToken(); // skip brach right
  1122. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1123. return node;
  1124. }
  1125. function parseNamed(tokenizer, key) {
  1126. const context = tokenizer.context();
  1127. const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
  1128. const node = startNode(4 /* NodeTypes.Named */, offset, loc);
  1129. node.key = key;
  1130. tokenizer.nextToken(); // skip brach right
  1131. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1132. return node;
  1133. }
  1134. function parseLiteral(tokenizer, value) {
  1135. const context = tokenizer.context();
  1136. const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
  1137. const node = startNode(9 /* NodeTypes.Literal */, offset, loc);
  1138. node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
  1139. tokenizer.nextToken(); // skip brach right
  1140. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1141. return node;
  1142. }
  1143. function parseLinkedModifier(tokenizer) {
  1144. const token = tokenizer.nextToken();
  1145. const context = tokenizer.context();
  1146. const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
  1147. const node = startNode(8 /* NodeTypes.LinkedModifier */, offset, loc);
  1148. if (token.type !== 12 /* TokenTypes.LinkedModifier */) {
  1149. // empty modifier
  1150. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
  1151. node.value = '';
  1152. endNode(node, offset, loc);
  1153. return {
  1154. nextConsumeToken: token,
  1155. node
  1156. };
  1157. }
  1158. // check token
  1159. if (token.value == null) {
  1160. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1161. }
  1162. node.value = token.value || '';
  1163. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1164. return {
  1165. node
  1166. };
  1167. }
  1168. function parseLinkedKey(tokenizer, value) {
  1169. const context = tokenizer.context();
  1170. const node = startNode(7 /* NodeTypes.LinkedKey */, context.offset, context.startLoc);
  1171. node.value = value;
  1172. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1173. return node;
  1174. }
  1175. function parseLinked(tokenizer) {
  1176. const context = tokenizer.context();
  1177. const linkedNode = startNode(6 /* NodeTypes.Linked */, context.offset, context.startLoc);
  1178. let token = tokenizer.nextToken();
  1179. if (token.type === 9 /* TokenTypes.LinkedDot */) {
  1180. const parsed = parseLinkedModifier(tokenizer);
  1181. linkedNode.modifier = parsed.node;
  1182. token = parsed.nextConsumeToken || tokenizer.nextToken();
  1183. }
  1184. // asset check token
  1185. if (token.type !== 10 /* TokenTypes.LinkedDelimiter */) {
  1186. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1187. }
  1188. token = tokenizer.nextToken();
  1189. // skip brace left
  1190. if (token.type === 2 /* TokenTypes.BraceLeft */) {
  1191. token = tokenizer.nextToken();
  1192. }
  1193. switch (token.type) {
  1194. case 11 /* TokenTypes.LinkedKey */:
  1195. if (token.value == null) {
  1196. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1197. }
  1198. linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
  1199. break;
  1200. case 5 /* TokenTypes.Named */:
  1201. if (token.value == null) {
  1202. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1203. }
  1204. linkedNode.key = parseNamed(tokenizer, token.value || '');
  1205. break;
  1206. case 6 /* TokenTypes.List */:
  1207. if (token.value == null) {
  1208. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1209. }
  1210. linkedNode.key = parseList(tokenizer, token.value || '');
  1211. break;
  1212. case 7 /* TokenTypes.Literal */:
  1213. if (token.value == null) {
  1214. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1215. }
  1216. linkedNode.key = parseLiteral(tokenizer, token.value || '');
  1217. break;
  1218. default:
  1219. // empty key
  1220. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
  1221. const nextContext = tokenizer.context();
  1222. const emptyLinkedKeyNode = startNode(7 /* NodeTypes.LinkedKey */, nextContext.offset, nextContext.startLoc);
  1223. emptyLinkedKeyNode.value = '';
  1224. endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
  1225. linkedNode.key = emptyLinkedKeyNode;
  1226. endNode(linkedNode, nextContext.offset, nextContext.startLoc);
  1227. return {
  1228. nextConsumeToken: token,
  1229. node: linkedNode
  1230. };
  1231. }
  1232. endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
  1233. return {
  1234. node: linkedNode
  1235. };
  1236. }
  1237. function parseMessage(tokenizer) {
  1238. const context = tokenizer.context();
  1239. const startOffset = context.currentType === 1 /* TokenTypes.Pipe */
  1240. ? tokenizer.currentOffset()
  1241. : context.offset;
  1242. const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
  1243. ? context.endLoc
  1244. : context.startLoc;
  1245. const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
  1246. node.items = [];
  1247. let nextToken = null;
  1248. do {
  1249. const token = nextToken || tokenizer.nextToken();
  1250. nextToken = null;
  1251. switch (token.type) {
  1252. case 0 /* TokenTypes.Text */:
  1253. if (token.value == null) {
  1254. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1255. }
  1256. node.items.push(parseText(tokenizer, token.value || ''));
  1257. break;
  1258. case 6 /* TokenTypes.List */:
  1259. if (token.value == null) {
  1260. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1261. }
  1262. node.items.push(parseList(tokenizer, token.value || ''));
  1263. break;
  1264. case 5 /* TokenTypes.Named */:
  1265. if (token.value == null) {
  1266. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1267. }
  1268. node.items.push(parseNamed(tokenizer, token.value || ''));
  1269. break;
  1270. case 7 /* TokenTypes.Literal */:
  1271. if (token.value == null) {
  1272. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
  1273. }
  1274. node.items.push(parseLiteral(tokenizer, token.value || ''));
  1275. break;
  1276. case 8 /* TokenTypes.LinkedAlias */:
  1277. const parsed = parseLinked(tokenizer);
  1278. node.items.push(parsed.node);
  1279. nextToken = parsed.nextConsumeToken || null;
  1280. break;
  1281. }
  1282. } while (context.currentType !== 14 /* TokenTypes.EOF */ &&
  1283. context.currentType !== 1 /* TokenTypes.Pipe */);
  1284. // adjust message node loc
  1285. const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
  1286. ? context.lastOffset
  1287. : tokenizer.currentOffset();
  1288. const endLoc = context.currentType === 1 /* TokenTypes.Pipe */
  1289. ? context.lastEndLoc
  1290. : tokenizer.currentPosition();
  1291. endNode(node, endOffset, endLoc);
  1292. return node;
  1293. }
  1294. function parsePlural(tokenizer, offset, loc, msgNode) {
  1295. const context = tokenizer.context();
  1296. let hasEmptyMessage = msgNode.items.length === 0;
  1297. const node = startNode(1 /* NodeTypes.Plural */, offset, loc);
  1298. node.cases = [];
  1299. node.cases.push(msgNode);
  1300. do {
  1301. const msg = parseMessage(tokenizer);
  1302. if (!hasEmptyMessage) {
  1303. hasEmptyMessage = msg.items.length === 0;
  1304. }
  1305. node.cases.push(msg);
  1306. } while (context.currentType !== 14 /* TokenTypes.EOF */);
  1307. if (hasEmptyMessage) {
  1308. emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
  1309. }
  1310. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1311. return node;
  1312. }
  1313. function parseResource(tokenizer) {
  1314. const context = tokenizer.context();
  1315. const { offset, startLoc } = context;
  1316. const msgNode = parseMessage(tokenizer);
  1317. if (context.currentType === 14 /* TokenTypes.EOF */) {
  1318. return msgNode;
  1319. }
  1320. else {
  1321. return parsePlural(tokenizer, offset, startLoc, msgNode);
  1322. }
  1323. }
  1324. function parse(source) {
  1325. const tokenizer = createTokenizer(source, assign({}, options));
  1326. const context = tokenizer.context();
  1327. const node = startNode(0 /* NodeTypes.Resource */, context.offset, context.startLoc);
  1328. if (location && node.loc) {
  1329. node.loc.source = source;
  1330. }
  1331. node.body = parseResource(tokenizer);
  1332. if (options.onCacheKey) {
  1333. node.cacheKey = options.onCacheKey(source);
  1334. }
  1335. // assert whether achieved to EOF
  1336. if (context.currentType !== 14 /* TokenTypes.EOF */) {
  1337. emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
  1338. }
  1339. endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
  1340. return node;
  1341. }
  1342. return { parse };
  1343. }
  1344. function getTokenCaption(token) {
  1345. if (token.type === 14 /* TokenTypes.EOF */) {
  1346. return 'EOF';
  1347. }
  1348. const name = (token.value || '').replace(/\r?\n/gu, '\\n');
  1349. return name.length > 10 ? name.slice(0, 9) + '…' : name;
  1350. }
  1351. function createTransformer(ast, options = {} // eslint-disable-line
  1352. ) {
  1353. const _context = {
  1354. ast,
  1355. helpers: new Set()
  1356. };
  1357. const context = () => _context;
  1358. const helper = (name) => {
  1359. _context.helpers.add(name);
  1360. return name;
  1361. };
  1362. return { context, helper };
  1363. }
  1364. function traverseNodes(nodes, transformer) {
  1365. for (let i = 0; i < nodes.length; i++) {
  1366. traverseNode(nodes[i], transformer);
  1367. }
  1368. }
  1369. function traverseNode(node, transformer) {
  1370. // TODO: if we need pre-hook of transform, should be implemented to here
  1371. switch (node.type) {
  1372. case 1 /* NodeTypes.Plural */:
  1373. traverseNodes(node.cases, transformer);
  1374. transformer.helper("plural" /* HelperNameMap.PLURAL */);
  1375. break;
  1376. case 2 /* NodeTypes.Message */:
  1377. traverseNodes(node.items, transformer);
  1378. break;
  1379. case 6 /* NodeTypes.Linked */:
  1380. const linked = node;
  1381. traverseNode(linked.key, transformer);
  1382. transformer.helper("linked" /* HelperNameMap.LINKED */);
  1383. transformer.helper("type" /* HelperNameMap.TYPE */);
  1384. break;
  1385. case 5 /* NodeTypes.List */:
  1386. transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
  1387. transformer.helper("list" /* HelperNameMap.LIST */);
  1388. break;
  1389. case 4 /* NodeTypes.Named */:
  1390. transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
  1391. transformer.helper("named" /* HelperNameMap.NAMED */);
  1392. break;
  1393. }
  1394. // TODO: if we need post-hook of transform, should be implemented to here
  1395. }
  1396. // transform AST
  1397. function transform(ast, options = {} // eslint-disable-line
  1398. ) {
  1399. const transformer = createTransformer(ast);
  1400. transformer.helper("normalize" /* HelperNameMap.NORMALIZE */);
  1401. // traverse
  1402. ast.body && traverseNode(ast.body, transformer);
  1403. // set meta information
  1404. const context = transformer.context();
  1405. ast.helpers = Array.from(context.helpers);
  1406. }
  1407. function optimize(ast) {
  1408. const body = ast.body;
  1409. if (body.type === 2 /* NodeTypes.Message */) {
  1410. optimizeMessageNode(body);
  1411. }
  1412. else {
  1413. body.cases.forEach(c => optimizeMessageNode(c));
  1414. }
  1415. return ast;
  1416. }
  1417. function optimizeMessageNode(message) {
  1418. if (message.items.length === 1) {
  1419. const item = message.items[0];
  1420. if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
  1421. message.static = item.value;
  1422. delete item.value; // optimization for size
  1423. }
  1424. }
  1425. else {
  1426. const values = [];
  1427. for (let i = 0; i < message.items.length; i++) {
  1428. const item = message.items[i];
  1429. if (!(item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */)) {
  1430. break;
  1431. }
  1432. if (item.value == null) {
  1433. break;
  1434. }
  1435. values.push(item.value);
  1436. }
  1437. if (values.length === message.items.length) {
  1438. message.static = join(values);
  1439. for (let i = 0; i < message.items.length; i++) {
  1440. const item = message.items[i];
  1441. if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
  1442. delete item.value; // optimization for size
  1443. }
  1444. }
  1445. }
  1446. }
  1447. }
  1448. const ERROR_DOMAIN$1 = 'minifier';
  1449. /* eslint-disable @typescript-eslint/no-explicit-any */
  1450. function minify(node) {
  1451. node.t = node.type;
  1452. switch (node.type) {
  1453. case 0 /* NodeTypes.Resource */:
  1454. const resource = node;
  1455. minify(resource.body);
  1456. resource.b = resource.body;
  1457. delete resource.body;
  1458. break;
  1459. case 1 /* NodeTypes.Plural */:
  1460. const plural = node;
  1461. const cases = plural.cases;
  1462. for (let i = 0; i < cases.length; i++) {
  1463. minify(cases[i]);
  1464. }
  1465. plural.c = cases;
  1466. delete plural.cases;
  1467. break;
  1468. case 2 /* NodeTypes.Message */:
  1469. const message = node;
  1470. const items = message.items;
  1471. for (let i = 0; i < items.length; i++) {
  1472. minify(items[i]);
  1473. }
  1474. message.i = items;
  1475. delete message.items;
  1476. if (message.static) {
  1477. message.s = message.static;
  1478. delete message.static;
  1479. }
  1480. break;
  1481. case 3 /* NodeTypes.Text */:
  1482. case 9 /* NodeTypes.Literal */:
  1483. case 8 /* NodeTypes.LinkedModifier */:
  1484. case 7 /* NodeTypes.LinkedKey */:
  1485. const valueNode = node;
  1486. if (valueNode.value) {
  1487. valueNode.v = valueNode.value;
  1488. delete valueNode.value;
  1489. }
  1490. break;
  1491. case 6 /* NodeTypes.Linked */:
  1492. const linked = node;
  1493. minify(linked.key);
  1494. linked.k = linked.key;
  1495. delete linked.key;
  1496. if (linked.modifier) {
  1497. minify(linked.modifier);
  1498. linked.m = linked.modifier;
  1499. delete linked.modifier;
  1500. }
  1501. break;
  1502. case 5 /* NodeTypes.List */:
  1503. const list = node;
  1504. list.i = list.index;
  1505. delete list.index;
  1506. break;
  1507. case 4 /* NodeTypes.Named */:
  1508. const named = node;
  1509. named.k = named.key;
  1510. delete named.key;
  1511. break;
  1512. default:
  1513. {
  1514. throw createCompileError(CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE, null, {
  1515. domain: ERROR_DOMAIN$1,
  1516. args: [node.type]
  1517. });
  1518. }
  1519. }
  1520. delete node.type;
  1521. }
  1522. /* eslint-enable @typescript-eslint/no-explicit-any */
  1523. const ERROR_DOMAIN = 'parser';
  1524. function createCodeGenerator(ast, options) {
  1525. const { sourceMap, filename, breakLineCode, needIndent: _needIndent } = options;
  1526. const location = options.location !== false;
  1527. const _context = {
  1528. filename,
  1529. code: '',
  1530. column: 1,
  1531. line: 1,
  1532. offset: 0,
  1533. map: undefined,
  1534. breakLineCode,
  1535. needIndent: _needIndent,
  1536. indentLevel: 0
  1537. };
  1538. if (location && ast.loc) {
  1539. _context.source = ast.loc.source;
  1540. }
  1541. const context = () => _context;
  1542. function push(code, node) {
  1543. _context.code += code;
  1544. }
  1545. function _newline(n, withBreakLine = true) {
  1546. const _breakLineCode = withBreakLine ? breakLineCode : '';
  1547. push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
  1548. }
  1549. function indent(withNewLine = true) {
  1550. const level = ++_context.indentLevel;
  1551. withNewLine && _newline(level);
  1552. }
  1553. function deindent(withNewLine = true) {
  1554. const level = --_context.indentLevel;
  1555. withNewLine && _newline(level);
  1556. }
  1557. function newline() {
  1558. _newline(_context.indentLevel);
  1559. }
  1560. const helper = (key) => `_${key}`;
  1561. const needIndent = () => _context.needIndent;
  1562. return {
  1563. context,
  1564. push,
  1565. indent,
  1566. deindent,
  1567. newline,
  1568. helper,
  1569. needIndent
  1570. };
  1571. }
  1572. function generateLinkedNode(generator, node) {
  1573. const { helper } = generator;
  1574. generator.push(`${helper("linked" /* HelperNameMap.LINKED */)}(`);
  1575. generateNode(generator, node.key);
  1576. if (node.modifier) {
  1577. generator.push(`, `);
  1578. generateNode(generator, node.modifier);
  1579. generator.push(`, _type`);
  1580. }
  1581. else {
  1582. generator.push(`, undefined, _type`);
  1583. }
  1584. generator.push(`)`);
  1585. }
  1586. function generateMessageNode(generator, node) {
  1587. const { helper, needIndent } = generator;
  1588. generator.push(`${helper("normalize" /* HelperNameMap.NORMALIZE */)}([`);
  1589. generator.indent(needIndent());
  1590. const length = node.items.length;
  1591. for (let i = 0; i < length; i++) {
  1592. generateNode(generator, node.items[i]);
  1593. if (i === length - 1) {
  1594. break;
  1595. }
  1596. generator.push(', ');
  1597. }
  1598. generator.deindent(needIndent());
  1599. generator.push('])');
  1600. }
  1601. function generatePluralNode(generator, node) {
  1602. const { helper, needIndent } = generator;
  1603. if (node.cases.length > 1) {
  1604. generator.push(`${helper("plural" /* HelperNameMap.PLURAL */)}([`);
  1605. generator.indent(needIndent());
  1606. const length = node.cases.length;
  1607. for (let i = 0; i < length; i++) {
  1608. generateNode(generator, node.cases[i]);
  1609. if (i === length - 1) {
  1610. break;
  1611. }
  1612. generator.push(', ');
  1613. }
  1614. generator.deindent(needIndent());
  1615. generator.push(`])`);
  1616. }
  1617. }
  1618. function generateResource(generator, node) {
  1619. if (node.body) {
  1620. generateNode(generator, node.body);
  1621. }
  1622. else {
  1623. generator.push('null');
  1624. }
  1625. }
  1626. function generateNode(generator, node) {
  1627. const { helper } = generator;
  1628. switch (node.type) {
  1629. case 0 /* NodeTypes.Resource */:
  1630. generateResource(generator, node);
  1631. break;
  1632. case 1 /* NodeTypes.Plural */:
  1633. generatePluralNode(generator, node);
  1634. break;
  1635. case 2 /* NodeTypes.Message */:
  1636. generateMessageNode(generator, node);
  1637. break;
  1638. case 6 /* NodeTypes.Linked */:
  1639. generateLinkedNode(generator, node);
  1640. break;
  1641. case 8 /* NodeTypes.LinkedModifier */:
  1642. generator.push(JSON.stringify(node.value), node);
  1643. break;
  1644. case 7 /* NodeTypes.LinkedKey */:
  1645. generator.push(JSON.stringify(node.value), node);
  1646. break;
  1647. case 5 /* NodeTypes.List */:
  1648. generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
  1649. break;
  1650. case 4 /* NodeTypes.Named */:
  1651. generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
  1652. break;
  1653. case 9 /* NodeTypes.Literal */:
  1654. generator.push(JSON.stringify(node.value), node);
  1655. break;
  1656. case 3 /* NodeTypes.Text */:
  1657. generator.push(JSON.stringify(node.value), node);
  1658. break;
  1659. default:
  1660. {
  1661. throw createCompileError(CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE, null, {
  1662. domain: ERROR_DOMAIN,
  1663. args: [node.type]
  1664. });
  1665. }
  1666. }
  1667. }
  1668. // generate code from AST
  1669. const generate = (ast, options = {} // eslint-disable-line
  1670. ) => {
  1671. const mode = isString(options.mode) ? options.mode : 'normal';
  1672. const filename = isString(options.filename)
  1673. ? options.filename
  1674. : 'message.intl';
  1675. const sourceMap = !!options.sourceMap;
  1676. // prettier-ignore
  1677. const breakLineCode = options.breakLineCode != null
  1678. ? options.breakLineCode
  1679. : mode === 'arrow'
  1680. ? ';'
  1681. : '\n';
  1682. const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
  1683. const helpers = ast.helpers || [];
  1684. const generator = createCodeGenerator(ast, {
  1685. mode,
  1686. filename,
  1687. sourceMap,
  1688. breakLineCode,
  1689. needIndent
  1690. });
  1691. generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
  1692. generator.indent(needIndent);
  1693. if (helpers.length > 0) {
  1694. generator.push(`const { ${join(helpers.map(s => `${s}: _${s}`), ', ')} } = ctx`);
  1695. generator.newline();
  1696. }
  1697. generator.push(`return `);
  1698. generateNode(generator, ast);
  1699. generator.deindent(needIndent);
  1700. generator.push(`}`);
  1701. delete ast.helpers;
  1702. const { code, map } = generator.context();
  1703. return {
  1704. ast,
  1705. code,
  1706. map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
  1707. };
  1708. };
  1709. function baseCompile$1(source, options = {}) {
  1710. const assignedOptions = assign({}, options);
  1711. const jit = !!assignedOptions.jit;
  1712. const enalbeMinify = !!assignedOptions.minify;
  1713. const enambeOptimize = assignedOptions.optimize == null ? true : assignedOptions.optimize;
  1714. // parse source codes
  1715. const parser = createParser(assignedOptions);
  1716. const ast = parser.parse(source);
  1717. if (!jit) {
  1718. // transform ASTs
  1719. transform(ast, assignedOptions);
  1720. // generate javascript codes
  1721. return generate(ast, assignedOptions);
  1722. }
  1723. else {
  1724. // optimize ASTs
  1725. enambeOptimize && optimize(ast);
  1726. // minimize ASTs
  1727. enalbeMinify && minify(ast);
  1728. // In JIT mode, no ast transform, no code generation.
  1729. return { ast, code: '' };
  1730. }
  1731. }
  1732. const pathStateMachine = [];
  1733. pathStateMachine[0 /* States.BEFORE_PATH */] = {
  1734. ["w" /* PathCharTypes.WORKSPACE */]: [0 /* States.BEFORE_PATH */],
  1735. ["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
  1736. ["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
  1737. ["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
  1738. };
  1739. pathStateMachine[1 /* States.IN_PATH */] = {
  1740. ["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */],
  1741. ["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */],
  1742. ["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
  1743. ["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
  1744. };
  1745. pathStateMachine[2 /* States.BEFORE_IDENT */] = {
  1746. ["w" /* PathCharTypes.WORKSPACE */]: [2 /* States.BEFORE_IDENT */],
  1747. ["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
  1748. ["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */]
  1749. };
  1750. pathStateMachine[3 /* States.IN_IDENT */] = {
  1751. ["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
  1752. ["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
  1753. ["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */, 1 /* Actions.PUSH */],
  1754. ["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */, 1 /* Actions.PUSH */],
  1755. ["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */, 1 /* Actions.PUSH */],
  1756. ["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */, 1 /* Actions.PUSH */]
  1757. };
  1758. pathStateMachine[4 /* States.IN_SUB_PATH */] = {
  1759. ["'" /* PathCharTypes.SINGLE_QUOTE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */],
  1760. ["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */],
  1761. ["[" /* PathCharTypes.LEFT_BRACKET */]: [
  1762. 4 /* States.IN_SUB_PATH */,
  1763. 2 /* Actions.INC_SUB_PATH_DEPTH */
  1764. ],
  1765. ["]" /* PathCharTypes.RIGHT_BRACKET */]: [1 /* States.IN_PATH */, 3 /* Actions.PUSH_SUB_PATH */],
  1766. ["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
  1767. ["l" /* PathCharTypes.ELSE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */]
  1768. };
  1769. pathStateMachine[5 /* States.IN_SINGLE_QUOTE */] = {
  1770. ["'" /* PathCharTypes.SINGLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
  1771. ["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
  1772. ["l" /* PathCharTypes.ELSE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */]
  1773. };
  1774. pathStateMachine[6 /* States.IN_DOUBLE_QUOTE */] = {
  1775. ["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
  1776. ["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
  1777. ["l" /* PathCharTypes.ELSE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */]
  1778. };
  1779. /**
  1780. * Check if an expression is a literal value.
  1781. */
  1782. const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
  1783. function isLiteral(exp) {
  1784. return literalValueRE.test(exp);
  1785. }
  1786. /**
  1787. * Strip quotes from a string
  1788. */
  1789. function stripQuotes(str) {
  1790. const a = str.charCodeAt(0);
  1791. const b = str.charCodeAt(str.length - 1);
  1792. return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str;
  1793. }
  1794. /**
  1795. * Determine the type of a character in a keypath.
  1796. */
  1797. function getPathCharType(ch) {
  1798. if (ch === undefined || ch === null) {
  1799. return "o" /* PathCharTypes.END_OF_FAIL */;
  1800. }
  1801. const code = ch.charCodeAt(0);
  1802. switch (code) {
  1803. case 0x5b: // [
  1804. case 0x5d: // ]
  1805. case 0x2e: // .
  1806. case 0x22: // "
  1807. case 0x27: // '
  1808. return ch;
  1809. case 0x5f: // _
  1810. case 0x24: // $
  1811. case 0x2d: // -
  1812. return "i" /* PathCharTypes.IDENT */;
  1813. case 0x09: // Tab (HT)
  1814. case 0x0a: // Newline (LF)
  1815. case 0x0d: // Return (CR)
  1816. case 0xa0: // No-break space (NBSP)
  1817. case 0xfeff: // Byte Order Mark (BOM)
  1818. case 0x2028: // Line Separator (LS)
  1819. case 0x2029: // Paragraph Separator (PS)
  1820. return "w" /* PathCharTypes.WORKSPACE */;
  1821. }
  1822. return "i" /* PathCharTypes.IDENT */;
  1823. }
  1824. /**
  1825. * Format a subPath, return its plain form if it is
  1826. * a literal string or number. Otherwise prepend the
  1827. * dynamic indicator (*).
  1828. */
  1829. function formatSubPath(path) {
  1830. const trimmed = path.trim();
  1831. // invalid leading 0
  1832. if (path.charAt(0) === '0' && isNaN(parseInt(path))) {
  1833. return false;
  1834. }
  1835. return isLiteral(trimmed)
  1836. ? stripQuotes(trimmed)
  1837. : "*" /* PathCharTypes.ASTARISK */ + trimmed;
  1838. }
  1839. /**
  1840. * Parse a string path into an array of segments
  1841. */
  1842. function parse(path) {
  1843. const keys = [];
  1844. let index = -1;
  1845. let mode = 0 /* States.BEFORE_PATH */;
  1846. let subPathDepth = 0;
  1847. let c;
  1848. let key; // eslint-disable-line
  1849. let newChar;
  1850. let type;
  1851. let transition;
  1852. let action;
  1853. let typeMap;
  1854. const actions = [];
  1855. actions[0 /* Actions.APPEND */] = () => {
  1856. if (key === undefined) {
  1857. key = newChar;
  1858. }
  1859. else {
  1860. key += newChar;
  1861. }
  1862. };
  1863. actions[1 /* Actions.PUSH */] = () => {
  1864. if (key !== undefined) {
  1865. keys.push(key);
  1866. key = undefined;
  1867. }
  1868. };
  1869. actions[2 /* Actions.INC_SUB_PATH_DEPTH */] = () => {
  1870. actions[0 /* Actions.APPEND */]();
  1871. subPathDepth++;
  1872. };
  1873. actions[3 /* Actions.PUSH_SUB_PATH */] = () => {
  1874. if (subPathDepth > 0) {
  1875. subPathDepth--;
  1876. mode = 4 /* States.IN_SUB_PATH */;
  1877. actions[0 /* Actions.APPEND */]();
  1878. }
  1879. else {
  1880. subPathDepth = 0;
  1881. if (key === undefined) {
  1882. return false;
  1883. }
  1884. key = formatSubPath(key);
  1885. if (key === false) {
  1886. return false;
  1887. }
  1888. else {
  1889. actions[1 /* Actions.PUSH */]();
  1890. }
  1891. }
  1892. };
  1893. function maybeUnescapeQuote() {
  1894. const nextChar = path[index + 1];
  1895. if ((mode === 5 /* States.IN_SINGLE_QUOTE */ &&
  1896. nextChar === "'" /* PathCharTypes.SINGLE_QUOTE */) ||
  1897. (mode === 6 /* States.IN_DOUBLE_QUOTE */ &&
  1898. nextChar === "\"" /* PathCharTypes.DOUBLE_QUOTE */)) {
  1899. index++;
  1900. newChar = '\\' + nextChar;
  1901. actions[0 /* Actions.APPEND */]();
  1902. return true;
  1903. }
  1904. }
  1905. while (mode !== null) {
  1906. index++;
  1907. c = path[index];
  1908. if (c === '\\' && maybeUnescapeQuote()) {
  1909. continue;
  1910. }
  1911. type = getPathCharType(c);
  1912. typeMap = pathStateMachine[mode];
  1913. transition = typeMap[type] || typeMap["l" /* PathCharTypes.ELSE */] || 8 /* States.ERROR */;
  1914. // check parse error
  1915. if (transition === 8 /* States.ERROR */) {
  1916. return;
  1917. }
  1918. mode = transition[0];
  1919. if (transition[1] !== undefined) {
  1920. action = actions[transition[1]];
  1921. if (action) {
  1922. newChar = c;
  1923. if (action() === false) {
  1924. return;
  1925. }
  1926. }
  1927. }
  1928. // check parse finish
  1929. if (mode === 7 /* States.AFTER_PATH */) {
  1930. return keys;
  1931. }
  1932. }
  1933. }
  1934. // path token cache
  1935. const cache = new Map();
  1936. /**
  1937. * key-value message resolver
  1938. *
  1939. * @remarks
  1940. * Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
  1941. *
  1942. * @param obj - A target object to be resolved with path
  1943. * @param path - A {@link Path | path} to resolve the value of message
  1944. *
  1945. * @returns A resolved {@link PathValue | path value}
  1946. *
  1947. * @VueI18nGeneral
  1948. */
  1949. function resolveWithKeyValue(obj, path) {
  1950. return isObject(obj) ? obj[path] : null;
  1951. }
  1952. /**
  1953. * message resolver
  1954. *
  1955. * @remarks
  1956. * Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
  1957. *
  1958. * @param obj - A target object to be resolved with path
  1959. * @param path - A {@link Path | path} to resolve the value of message
  1960. *
  1961. * @returns A resolved {@link PathValue | path value}
  1962. *
  1963. * @VueI18nGeneral
  1964. */
  1965. function resolveValue(obj, path) {
  1966. // check object
  1967. if (!isObject(obj)) {
  1968. return null;
  1969. }
  1970. // parse path
  1971. let hit = cache.get(path);
  1972. if (!hit) {
  1973. hit = parse(path);
  1974. if (hit) {
  1975. cache.set(path, hit);
  1976. }
  1977. }
  1978. // check hit
  1979. if (!hit) {
  1980. return null;
  1981. }
  1982. // resolve path value
  1983. const len = hit.length;
  1984. let last = obj;
  1985. let i = 0;
  1986. while (i < len) {
  1987. const val = last[hit[i]];
  1988. if (val === undefined) {
  1989. return null;
  1990. }
  1991. if (isFunction(last)) {
  1992. return null;
  1993. }
  1994. last = val;
  1995. i++;
  1996. }
  1997. return last;
  1998. }
  1999. const DEFAULT_MODIFIER = (str) => str;
  2000. const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
  2001. const DEFAULT_MESSAGE_DATA_TYPE = 'text';
  2002. const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : join(values);
  2003. const DEFAULT_INTERPOLATE = toDisplayString;
  2004. function pluralDefault(choice, choicesLength) {
  2005. choice = Math.abs(choice);
  2006. if (choicesLength === 2) {
  2007. // prettier-ignore
  2008. return choice
  2009. ? choice > 1
  2010. ? 1
  2011. : 0
  2012. : 1;
  2013. }
  2014. return choice ? Math.min(choice, 2) : 0;
  2015. }
  2016. function getPluralIndex(options) {
  2017. // prettier-ignore
  2018. const index = isNumber(options.pluralIndex)
  2019. ? options.pluralIndex
  2020. : -1;
  2021. // prettier-ignore
  2022. return options.named && (isNumber(options.named.count) || isNumber(options.named.n))
  2023. ? isNumber(options.named.count)
  2024. ? options.named.count
  2025. : isNumber(options.named.n)
  2026. ? options.named.n
  2027. : index
  2028. : index;
  2029. }
  2030. function normalizeNamed(pluralIndex, props) {
  2031. if (!props.count) {
  2032. props.count = pluralIndex;
  2033. }
  2034. if (!props.n) {
  2035. props.n = pluralIndex;
  2036. }
  2037. }
  2038. function createMessageContext(options = {}) {
  2039. const locale = options.locale;
  2040. const pluralIndex = getPluralIndex(options);
  2041. const pluralRule = isObject(options.pluralRules) &&
  2042. isString(locale) &&
  2043. isFunction(options.pluralRules[locale])
  2044. ? options.pluralRules[locale]
  2045. : pluralDefault;
  2046. const orgPluralRule = isObject(options.pluralRules) &&
  2047. isString(locale) &&
  2048. isFunction(options.pluralRules[locale])
  2049. ? pluralDefault
  2050. : undefined;
  2051. const plural = (messages) => {
  2052. return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
  2053. };
  2054. const _list = options.list || [];
  2055. const list = (index) => _list[index];
  2056. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2057. const _named = options.named || {};
  2058. isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
  2059. const named = (key) => _named[key];
  2060. function message(key) {
  2061. // prettier-ignore
  2062. const msg = isFunction(options.messages)
  2063. ? options.messages(key)
  2064. : isObject(options.messages)
  2065. ? options.messages[key]
  2066. : false;
  2067. return !msg
  2068. ? options.parent
  2069. ? options.parent.message(key) // resolve from parent messages
  2070. : DEFAULT_MESSAGE
  2071. : msg;
  2072. }
  2073. const _modifier = (name) => options.modifiers
  2074. ? options.modifiers[name]
  2075. : DEFAULT_MODIFIER;
  2076. const normalize = isPlainObject(options.processor) && isFunction(options.processor.normalize)
  2077. ? options.processor.normalize
  2078. : DEFAULT_NORMALIZE;
  2079. const interpolate = isPlainObject(options.processor) &&
  2080. isFunction(options.processor.interpolate)
  2081. ? options.processor.interpolate
  2082. : DEFAULT_INTERPOLATE;
  2083. const type = isPlainObject(options.processor) && isString(options.processor.type)
  2084. ? options.processor.type
  2085. : DEFAULT_MESSAGE_DATA_TYPE;
  2086. const linked = (key, ...args) => {
  2087. const [arg1, arg2] = args;
  2088. let type = 'text';
  2089. let modifier = '';
  2090. if (args.length === 1) {
  2091. if (isObject(arg1)) {
  2092. modifier = arg1.modifier || modifier;
  2093. type = arg1.type || type;
  2094. }
  2095. else if (isString(arg1)) {
  2096. modifier = arg1 || modifier;
  2097. }
  2098. }
  2099. else if (args.length === 2) {
  2100. if (isString(arg1)) {
  2101. modifier = arg1 || modifier;
  2102. }
  2103. if (isString(arg2)) {
  2104. type = arg2 || type;
  2105. }
  2106. }
  2107. const ret = message(key)(ctx);
  2108. const msg =
  2109. // The message in vnode resolved with linked are returned as an array by processor.nomalize
  2110. type === 'vnode' && isArray(ret) && modifier
  2111. ? ret[0]
  2112. : ret;
  2113. return modifier ? _modifier(modifier)(msg, type) : msg;
  2114. };
  2115. const ctx = {
  2116. ["list" /* HelperNameMap.LIST */]: list,
  2117. ["named" /* HelperNameMap.NAMED */]: named,
  2118. ["plural" /* HelperNameMap.PLURAL */]: plural,
  2119. ["linked" /* HelperNameMap.LINKED */]: linked,
  2120. ["message" /* HelperNameMap.MESSAGE */]: message,
  2121. ["type" /* HelperNameMap.TYPE */]: type,
  2122. ["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
  2123. ["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
  2124. ["values" /* HelperNameMap.VALUES */]: assign({}, _list, _named)
  2125. };
  2126. return ctx;
  2127. }
  2128. let devtools = null;
  2129. function setDevToolsHook(hook) {
  2130. devtools = hook;
  2131. }
  2132. function initI18nDevTools(i18n, version, meta) {
  2133. // TODO: queue if devtools is undefined
  2134. devtools &&
  2135. devtools.emit("i18n:init" /* IntlifyDevToolsHooks.I18nInit */, {
  2136. timestamp: Date.now(),
  2137. i18n,
  2138. version,
  2139. meta
  2140. });
  2141. }
  2142. const translateDevTools = /* #__PURE__*/ createDevToolsHook("function:translate" /* IntlifyDevToolsHooks.FunctionTranslate */);
  2143. function createDevToolsHook(hook) {
  2144. return (payloads) => devtools && devtools.emit(hook, payloads);
  2145. }
  2146. const CoreWarnCodes = {
  2147. NOT_FOUND_KEY: 1,
  2148. FALLBACK_TO_TRANSLATE: 2,
  2149. CANNOT_FORMAT_NUMBER: 3,
  2150. FALLBACK_TO_NUMBER_FORMAT: 4,
  2151. CANNOT_FORMAT_DATE: 5,
  2152. FALLBACK_TO_DATE_FORMAT: 6,
  2153. EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
  2154. __EXTEND_POINT__: 8
  2155. };
  2156. /** @internal */
  2157. const warnMessages$1 = {
  2158. [CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
  2159. [CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
  2160. [CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
  2161. [CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
  2162. [CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
  2163. [CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
  2164. [CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`
  2165. };
  2166. function getWarnMessage$1(code, ...args) {
  2167. return format$1(warnMessages$1[code], ...args);
  2168. }
  2169. const code$2 = CompileErrorCodes.__EXTEND_POINT__;
  2170. const inc$2 = incrementer(code$2);
  2171. const CoreErrorCodes = {
  2172. INVALID_ARGUMENT: code$2,
  2173. INVALID_DATE_ARGUMENT: inc$2(),
  2174. INVALID_ISO_DATE_ARGUMENT: inc$2(),
  2175. NOT_SUPPORT_NON_STRING_MESSAGE: inc$2(),
  2176. NOT_SUPPORT_LOCALE_PROMISE_VALUE: inc$2(),
  2177. NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: inc$2(),
  2178. NOT_SUPPORT_LOCALE_TYPE: inc$2(),
  2179. __EXTEND_POINT__: inc$2() // 25
  2180. };
  2181. function createCoreError(code) {
  2182. return createCompileError(code, null, { messages: errorMessages$1 } );
  2183. }
  2184. /** @internal */
  2185. const errorMessages$1 = {
  2186. [CoreErrorCodes.INVALID_ARGUMENT]: 'Invalid arguments',
  2187. [CoreErrorCodes.INVALID_DATE_ARGUMENT]: 'The date provided is an invalid Date object.' +
  2188. 'Make sure your Date represents a valid date.',
  2189. [CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: 'The argument provided is not a valid ISO date string',
  2190. [CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: 'Not support non-string message',
  2191. [CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE]: 'cannot support promise value',
  2192. [CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: 'cannot support async function',
  2193. [CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: 'cannot support locale type'
  2194. };
  2195. /** @internal */
  2196. function getLocale(context, options) {
  2197. return options.locale != null
  2198. ? resolveLocale(options.locale)
  2199. : resolveLocale(context.locale);
  2200. }
  2201. let _resolveLocale;
  2202. /** @internal */
  2203. function resolveLocale(locale) {
  2204. if (isString(locale)) {
  2205. return locale;
  2206. }
  2207. else {
  2208. if (isFunction(locale)) {
  2209. if (locale.resolvedOnce && _resolveLocale != null) {
  2210. return _resolveLocale;
  2211. }
  2212. else if (locale.constructor.name === 'Function') {
  2213. const resolve = locale();
  2214. if (isPromise(resolve)) {
  2215. throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
  2216. }
  2217. return (_resolveLocale = resolve);
  2218. }
  2219. else {
  2220. throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
  2221. }
  2222. }
  2223. else {
  2224. throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
  2225. }
  2226. }
  2227. }
  2228. /**
  2229. * Fallback with simple implemenation
  2230. *
  2231. * @remarks
  2232. * A fallback locale function implemented with a simple fallback algorithm.
  2233. *
  2234. * Basically, it returns the value as specified in the `fallbackLocale` props, and is processed with the fallback inside intlify.
  2235. *
  2236. * @param ctx - A {@link CoreContext | context}
  2237. * @param fallback - A {@link FallbackLocale | fallback locale}
  2238. * @param start - A starting {@link Locale | locale}
  2239. *
  2240. * @returns Fallback locales
  2241. *
  2242. * @VueI18nGeneral
  2243. */
  2244. function fallbackWithSimple(ctx, fallback, start // eslint-disable-line @typescript-eslint/no-unused-vars
  2245. ) {
  2246. // prettier-ignore
  2247. return [...new Set([
  2248. start,
  2249. ...(isArray(fallback)
  2250. ? fallback
  2251. : isObject(fallback)
  2252. ? Object.keys(fallback)
  2253. : isString(fallback)
  2254. ? [fallback]
  2255. : [start])
  2256. ])];
  2257. }
  2258. /**
  2259. * Fallback with locale chain
  2260. *
  2261. * @remarks
  2262. * A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
  2263. *
  2264. * @param ctx - A {@link CoreContext | context}
  2265. * @param fallback - A {@link FallbackLocale | fallback locale}
  2266. * @param start - A starting {@link Locale | locale}
  2267. *
  2268. * @returns Fallback locales
  2269. *
  2270. * @VueI18nSee [Fallbacking](../guide/essentials/fallback)
  2271. *
  2272. * @VueI18nGeneral
  2273. */
  2274. function fallbackWithLocaleChain(ctx, fallback, start) {
  2275. const startLocale = isString(start) ? start : DEFAULT_LOCALE;
  2276. const context = ctx;
  2277. if (!context.__localeChainCache) {
  2278. context.__localeChainCache = new Map();
  2279. }
  2280. let chain = context.__localeChainCache.get(startLocale);
  2281. if (!chain) {
  2282. chain = [];
  2283. // first block defined by start
  2284. let block = [start];
  2285. // while any intervening block found
  2286. while (isArray(block)) {
  2287. block = appendBlockToChain(chain, block, fallback);
  2288. }
  2289. // prettier-ignore
  2290. // last block defined by default
  2291. const defaults = isArray(fallback) || !isPlainObject(fallback)
  2292. ? fallback
  2293. : fallback['default']
  2294. ? fallback['default']
  2295. : null;
  2296. // convert defaults to array
  2297. block = isString(defaults) ? [defaults] : defaults;
  2298. if (isArray(block)) {
  2299. appendBlockToChain(chain, block, false);
  2300. }
  2301. context.__localeChainCache.set(startLocale, chain);
  2302. }
  2303. return chain;
  2304. }
  2305. function appendBlockToChain(chain, block, blocks) {
  2306. let follow = true;
  2307. for (let i = 0; i < block.length && isBoolean(follow); i++) {
  2308. const locale = block[i];
  2309. if (isString(locale)) {
  2310. follow = appendLocaleToChain(chain, block[i], blocks);
  2311. }
  2312. }
  2313. return follow;
  2314. }
  2315. function appendLocaleToChain(chain, locale, blocks) {
  2316. let follow;
  2317. const tokens = locale.split('-');
  2318. do {
  2319. const target = tokens.join('-');
  2320. follow = appendItemToChain(chain, target, blocks);
  2321. tokens.splice(-1, 1);
  2322. } while (tokens.length && follow === true);
  2323. return follow;
  2324. }
  2325. function appendItemToChain(chain, target, blocks) {
  2326. let follow = false;
  2327. if (!chain.includes(target)) {
  2328. follow = true;
  2329. if (target) {
  2330. follow = target[target.length - 1] !== '!';
  2331. const locale = target.replace(/!/g, '');
  2332. chain.push(locale);
  2333. if ((isArray(blocks) || isPlainObject(blocks)) &&
  2334. blocks[locale] // eslint-disable-line @typescript-eslint/no-explicit-any
  2335. ) {
  2336. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2337. follow = blocks[locale];
  2338. }
  2339. }
  2340. }
  2341. return follow;
  2342. }
  2343. /* eslint-disable @typescript-eslint/no-explicit-any */
  2344. /**
  2345. * Intlify core-base version
  2346. * @internal
  2347. */
  2348. const VERSION$1 = '9.8.0';
  2349. const NOT_REOSLVED = -1;
  2350. const DEFAULT_LOCALE = 'en-US';
  2351. const MISSING_RESOLVE_VALUE = '';
  2352. const capitalize = (str) => `${str.charAt(0).toLocaleUpperCase()}${str.substr(1)}`;
  2353. function getDefaultLinkedModifiers() {
  2354. return {
  2355. upper: (val, type) => {
  2356. // prettier-ignore
  2357. return type === 'text' && isString(val)
  2358. ? val.toUpperCase()
  2359. : type === 'vnode' && isObject(val) && '__v_isVNode' in val
  2360. ? val.children.toUpperCase()
  2361. : val;
  2362. },
  2363. lower: (val, type) => {
  2364. // prettier-ignore
  2365. return type === 'text' && isString(val)
  2366. ? val.toLowerCase()
  2367. : type === 'vnode' && isObject(val) && '__v_isVNode' in val
  2368. ? val.children.toLowerCase()
  2369. : val;
  2370. },
  2371. capitalize: (val, type) => {
  2372. // prettier-ignore
  2373. return (type === 'text' && isString(val)
  2374. ? capitalize(val)
  2375. : type === 'vnode' && isObject(val) && '__v_isVNode' in val
  2376. ? capitalize(val.children)
  2377. : val);
  2378. }
  2379. };
  2380. }
  2381. let _compiler;
  2382. function registerMessageCompiler(compiler) {
  2383. _compiler = compiler;
  2384. }
  2385. let _resolver;
  2386. /**
  2387. * Register the message resolver
  2388. *
  2389. * @param resolver - A {@link MessageResolver} function
  2390. *
  2391. * @VueI18nGeneral
  2392. */
  2393. function registerMessageResolver(resolver) {
  2394. _resolver = resolver;
  2395. }
  2396. let _fallbacker;
  2397. /**
  2398. * Register the locale fallbacker
  2399. *
  2400. * @param fallbacker - A {@link LocaleFallbacker} function
  2401. *
  2402. * @VueI18nGeneral
  2403. */
  2404. function registerLocaleFallbacker(fallbacker) {
  2405. _fallbacker = fallbacker;
  2406. }
  2407. // Additional Meta for Intlify DevTools
  2408. let _additionalMeta = null;
  2409. /* #__NO_SIDE_EFFECTS__ */
  2410. const setAdditionalMeta = (meta) => {
  2411. _additionalMeta = meta;
  2412. };
  2413. /* #__NO_SIDE_EFFECTS__ */
  2414. const getAdditionalMeta = () => _additionalMeta;
  2415. let _fallbackContext = null;
  2416. const setFallbackContext = (context) => {
  2417. _fallbackContext = context;
  2418. };
  2419. const getFallbackContext = () => _fallbackContext;
  2420. // ID for CoreContext
  2421. let _cid = 0;
  2422. function createCoreContext(options = {}) {
  2423. // setup options
  2424. const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
  2425. const version = isString(options.version) ? options.version : VERSION$1;
  2426. const locale = isString(options.locale) || isFunction(options.locale)
  2427. ? options.locale
  2428. : DEFAULT_LOCALE;
  2429. const _locale = isFunction(locale) ? DEFAULT_LOCALE : locale;
  2430. const fallbackLocale = isArray(options.fallbackLocale) ||
  2431. isPlainObject(options.fallbackLocale) ||
  2432. isString(options.fallbackLocale) ||
  2433. options.fallbackLocale === false
  2434. ? options.fallbackLocale
  2435. : _locale;
  2436. const messages = isPlainObject(options.messages)
  2437. ? options.messages
  2438. : { [_locale]: {} };
  2439. const datetimeFormats = isPlainObject(options.datetimeFormats)
  2440. ? options.datetimeFormats
  2441. : { [_locale]: {} }
  2442. ;
  2443. const numberFormats = isPlainObject(options.numberFormats)
  2444. ? options.numberFormats
  2445. : { [_locale]: {} }
  2446. ;
  2447. const modifiers = assign({}, options.modifiers || {}, getDefaultLinkedModifiers());
  2448. const pluralRules = options.pluralRules || {};
  2449. const missing = isFunction(options.missing) ? options.missing : null;
  2450. const missingWarn = isBoolean(options.missingWarn) || isRegExp(options.missingWarn)
  2451. ? options.missingWarn
  2452. : true;
  2453. const fallbackWarn = isBoolean(options.fallbackWarn) || isRegExp(options.fallbackWarn)
  2454. ? options.fallbackWarn
  2455. : true;
  2456. const fallbackFormat = !!options.fallbackFormat;
  2457. const unresolving = !!options.unresolving;
  2458. const postTranslation = isFunction(options.postTranslation)
  2459. ? options.postTranslation
  2460. : null;
  2461. const processor = isPlainObject(options.processor) ? options.processor : null;
  2462. const warnHtmlMessage = isBoolean(options.warnHtmlMessage)
  2463. ? options.warnHtmlMessage
  2464. : true;
  2465. const escapeParameter = !!options.escapeParameter;
  2466. const messageCompiler = isFunction(options.messageCompiler)
  2467. ? options.messageCompiler
  2468. : _compiler;
  2469. const messageResolver = isFunction(options.messageResolver)
  2470. ? options.messageResolver
  2471. : _resolver || resolveWithKeyValue;
  2472. const localeFallbacker = isFunction(options.localeFallbacker)
  2473. ? options.localeFallbacker
  2474. : _fallbacker || fallbackWithSimple;
  2475. const fallbackContext = isObject(options.fallbackContext)
  2476. ? options.fallbackContext
  2477. : undefined;
  2478. // setup internal options
  2479. const internalOptions = options;
  2480. const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters)
  2481. ? internalOptions.__datetimeFormatters
  2482. : new Map()
  2483. ;
  2484. const __numberFormatters = isObject(internalOptions.__numberFormatters)
  2485. ? internalOptions.__numberFormatters
  2486. : new Map()
  2487. ;
  2488. const __meta = isObject(internalOptions.__meta) ? internalOptions.__meta : {};
  2489. _cid++;
  2490. const context = {
  2491. version,
  2492. cid: _cid,
  2493. locale,
  2494. fallbackLocale,
  2495. messages,
  2496. modifiers,
  2497. pluralRules,
  2498. missing,
  2499. missingWarn,
  2500. fallbackWarn,
  2501. fallbackFormat,
  2502. unresolving,
  2503. postTranslation,
  2504. processor,
  2505. warnHtmlMessage,
  2506. escapeParameter,
  2507. messageCompiler,
  2508. messageResolver,
  2509. localeFallbacker,
  2510. fallbackContext,
  2511. onWarn,
  2512. __meta
  2513. };
  2514. {
  2515. context.datetimeFormats = datetimeFormats;
  2516. context.numberFormats = numberFormats;
  2517. context.__datetimeFormatters = __datetimeFormatters;
  2518. context.__numberFormatters = __numberFormatters;
  2519. }
  2520. // for vue-devtools timeline event
  2521. {
  2522. context.__v_emitter =
  2523. internalOptions.__v_emitter != null
  2524. ? internalOptions.__v_emitter
  2525. : undefined;
  2526. }
  2527. // NOTE: experimental !!
  2528. {
  2529. initI18nDevTools(context, version, __meta);
  2530. }
  2531. return context;
  2532. }
  2533. /** @internal */
  2534. function isTranslateFallbackWarn(fallback, key) {
  2535. return fallback instanceof RegExp ? fallback.test(key) : fallback;
  2536. }
  2537. /** @internal */
  2538. function isTranslateMissingWarn(missing, key) {
  2539. return missing instanceof RegExp ? missing.test(key) : missing;
  2540. }
  2541. /** @internal */
  2542. function handleMissing(context, key, locale, missingWarn, type) {
  2543. const { missing, onWarn } = context;
  2544. // for vue-devtools timeline event
  2545. {
  2546. const emitter = context.__v_emitter;
  2547. if (emitter) {
  2548. emitter.emit("missing" /* VueDevToolsTimelineEvents.MISSING */, {
  2549. locale,
  2550. key,
  2551. type,
  2552. groupId: `${type}:${key}`
  2553. });
  2554. }
  2555. }
  2556. if (missing !== null) {
  2557. const ret = missing(context, locale, key, type);
  2558. return isString(ret) ? ret : key;
  2559. }
  2560. else {
  2561. if (isTranslateMissingWarn(missingWarn, key)) {
  2562. onWarn(getWarnMessage$1(CoreWarnCodes.NOT_FOUND_KEY, { key, locale }));
  2563. }
  2564. return key;
  2565. }
  2566. }
  2567. /** @internal */
  2568. function updateFallbackLocale(ctx, locale, fallback) {
  2569. const context = ctx;
  2570. context.__localeChainCache = new Map();
  2571. ctx.localeFallbacker(ctx, fallback, locale);
  2572. }
  2573. /* eslint-enable @typescript-eslint/no-explicit-any */
  2574. function format(ast) {
  2575. const msg = (ctx) => formatParts(ctx, ast);
  2576. return msg;
  2577. }
  2578. function formatParts(ctx, ast) {
  2579. const body = ast.b || ast.body;
  2580. if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
  2581. const plural = body;
  2582. const cases = plural.c || plural.cases;
  2583. return ctx.plural(cases.reduce((messages, c) => [
  2584. ...messages,
  2585. formatMessageParts(ctx, c)
  2586. ], []));
  2587. }
  2588. else {
  2589. return formatMessageParts(ctx, body);
  2590. }
  2591. }
  2592. function formatMessageParts(ctx, node) {
  2593. const _static = node.s || node.static;
  2594. if (_static) {
  2595. return ctx.type === 'text'
  2596. ? _static
  2597. : ctx.normalize([_static]);
  2598. }
  2599. else {
  2600. const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
  2601. return ctx.normalize(messages);
  2602. }
  2603. }
  2604. function formatMessagePart(ctx, node) {
  2605. const type = node.t || node.type;
  2606. switch (type) {
  2607. case 3 /* NodeTypes.Text */:
  2608. const text = node;
  2609. return (text.v || text.value);
  2610. case 9 /* NodeTypes.Literal */:
  2611. const literal = node;
  2612. return (literal.v || literal.value);
  2613. case 4 /* NodeTypes.Named */:
  2614. const named = node;
  2615. return ctx.interpolate(ctx.named(named.k || named.key));
  2616. case 5 /* NodeTypes.List */:
  2617. const list = node;
  2618. return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
  2619. case 6 /* NodeTypes.Linked */:
  2620. const linked = node;
  2621. const modifier = linked.m || linked.modifier;
  2622. return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
  2623. case 7 /* NodeTypes.LinkedKey */:
  2624. const linkedKey = node;
  2625. return (linkedKey.v || linkedKey.value);
  2626. case 8 /* NodeTypes.LinkedModifier */:
  2627. const linkedModifier = node;
  2628. return (linkedModifier.v || linkedModifier.value);
  2629. default:
  2630. throw new Error(`unhandled node type on format message part: ${type}`);
  2631. }
  2632. }
  2633. const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
  2634. function checkHtmlMessage(source, warnHtmlMessage) {
  2635. if (warnHtmlMessage && detectHtmlTag(source)) {
  2636. warn(format$1(WARN_MESSAGE, { source }));
  2637. }
  2638. }
  2639. const defaultOnCacheKey = (message) => message;
  2640. let compileCache = Object.create(null);
  2641. const isMessageAST = (val) => isObject(val) &&
  2642. (val.t === 0 || val.type === 0) &&
  2643. ('b' in val || 'body' in val);
  2644. function baseCompile(message, options = {}) {
  2645. // error detecting on compile
  2646. let detectError = false;
  2647. const onError = options.onError || defaultOnError;
  2648. options.onError = (err) => {
  2649. detectError = true;
  2650. onError(err);
  2651. };
  2652. // compile with mesasge-compiler
  2653. return { ...baseCompile$1(message, options), detectError };
  2654. }
  2655. function compile(message, context) {
  2656. if (isString(message)) {
  2657. // check HTML message
  2658. const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
  2659. ? context.warnHtmlMessage
  2660. : true;
  2661. checkHtmlMessage(message, warnHtmlMessage);
  2662. // check caches
  2663. const onCacheKey = context.onCacheKey || defaultOnCacheKey;
  2664. const cacheKey = onCacheKey(message);
  2665. const cached = compileCache[cacheKey];
  2666. if (cached) {
  2667. return cached;
  2668. }
  2669. // compile with JIT mode
  2670. const { ast, detectError } = baseCompile(message, {
  2671. ...context,
  2672. location: true,
  2673. jit: true
  2674. });
  2675. // compose message function from AST
  2676. const msg = format(ast);
  2677. // if occurred compile error, don't cache
  2678. return !detectError
  2679. ? (compileCache[cacheKey] = msg)
  2680. : msg;
  2681. }
  2682. else {
  2683. if (!isMessageAST(message)) {
  2684. warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
  2685. return (() => message);
  2686. }
  2687. // AST case (passed from bundler)
  2688. const cacheKey = message.cacheKey;
  2689. if (cacheKey) {
  2690. const cached = compileCache[cacheKey];
  2691. if (cached) {
  2692. return cached;
  2693. }
  2694. // compose message function from message (AST)
  2695. return (compileCache[cacheKey] =
  2696. format(message));
  2697. }
  2698. else {
  2699. return format(message);
  2700. }
  2701. }
  2702. }
  2703. const NOOP_MESSAGE_FUNCTION = () => '';
  2704. const isMessageFunction = (val) => isFunction(val);
  2705. // implementation of `translate` function
  2706. function translate(context, ...args) {
  2707. const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
  2708. const [key, options] = parseTranslateArgs(...args);
  2709. const missingWarn = isBoolean(options.missingWarn)
  2710. ? options.missingWarn
  2711. : context.missingWarn;
  2712. const fallbackWarn = isBoolean(options.fallbackWarn)
  2713. ? options.fallbackWarn
  2714. : context.fallbackWarn;
  2715. const escapeParameter = isBoolean(options.escapeParameter)
  2716. ? options.escapeParameter
  2717. : context.escapeParameter;
  2718. const resolvedMessage = !!options.resolvedMessage;
  2719. // prettier-ignore
  2720. const defaultMsgOrKey = isString(options.default) || isBoolean(options.default) // default by function option
  2721. ? !isBoolean(options.default)
  2722. ? options.default
  2723. : (!messageCompiler ? () => key : key)
  2724. : fallbackFormat // default by `fallbackFormat` option
  2725. ? (!messageCompiler ? () => key : key)
  2726. : '';
  2727. const enableDefaultMsg = fallbackFormat || defaultMsgOrKey !== '';
  2728. const locale = getLocale(context, options);
  2729. // escape params
  2730. escapeParameter && escapeParams(options);
  2731. // resolve message format
  2732. // eslint-disable-next-line prefer-const
  2733. let [formatScope, targetLocale, message] = !resolvedMessage
  2734. ? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
  2735. : [
  2736. key,
  2737. locale,
  2738. messages[locale] || {}
  2739. ];
  2740. // NOTE:
  2741. // Fix to work around `ssrTransfrom` bug in Vite.
  2742. // https://github.com/vitejs/vite/issues/4306
  2743. // To get around this, use temporary variables.
  2744. // https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
  2745. let format = formatScope;
  2746. // if you use default message, set it as message format!
  2747. let cacheBaseKey = key;
  2748. if (!resolvedMessage &&
  2749. !(isString(format) ||
  2750. isMessageAST(format) ||
  2751. isMessageFunction(format))) {
  2752. if (enableDefaultMsg) {
  2753. format = defaultMsgOrKey;
  2754. cacheBaseKey = format;
  2755. }
  2756. }
  2757. // checking message format and target locale
  2758. if (!resolvedMessage &&
  2759. (!(isString(format) ||
  2760. isMessageAST(format) ||
  2761. isMessageFunction(format)) ||
  2762. !isString(targetLocale))) {
  2763. return unresolving ? NOT_REOSLVED : key;
  2764. }
  2765. // TODO: refactor
  2766. if (isString(format) && context.messageCompiler == null) {
  2767. warn(`The message format compilation is not supported in this build. ` +
  2768. `Because message compiler isn't included. ` +
  2769. `You need to pre-compilation all message format. ` +
  2770. `So translate function return '${key}'.`);
  2771. return key;
  2772. }
  2773. // setup compile error detecting
  2774. let occurred = false;
  2775. const onError = () => {
  2776. occurred = true;
  2777. };
  2778. // compile message format
  2779. const msg = !isMessageFunction(format)
  2780. ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
  2781. : format;
  2782. // if occurred compile error, return the message format
  2783. if (occurred) {
  2784. return format;
  2785. }
  2786. // evaluate message with context
  2787. const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
  2788. const msgContext = createMessageContext(ctxOptions);
  2789. const messaged = evaluateMessage(context, msg, msgContext);
  2790. // if use post translation option, proceed it with handler
  2791. const ret = postTranslation
  2792. ? postTranslation(messaged, key)
  2793. : messaged;
  2794. // NOTE: experimental !!
  2795. {
  2796. // prettier-ignore
  2797. const payloads = {
  2798. timestamp: Date.now(),
  2799. key: isString(key)
  2800. ? key
  2801. : isMessageFunction(format)
  2802. ? format.key
  2803. : '',
  2804. locale: targetLocale || (isMessageFunction(format)
  2805. ? format.locale
  2806. : ''),
  2807. format: isString(format)
  2808. ? format
  2809. : isMessageFunction(format)
  2810. ? format.source
  2811. : '',
  2812. message: ret
  2813. };
  2814. payloads.meta = assign({}, context.__meta, getAdditionalMeta() || {});
  2815. translateDevTools(payloads);
  2816. }
  2817. return ret;
  2818. }
  2819. function escapeParams(options) {
  2820. if (isArray(options.list)) {
  2821. options.list = options.list.map(item => isString(item) ? escapeHtml(item) : item);
  2822. }
  2823. else if (isObject(options.named)) {
  2824. Object.keys(options.named).forEach(key => {
  2825. if (isString(options.named[key])) {
  2826. options.named[key] = escapeHtml(options.named[key]);
  2827. }
  2828. });
  2829. }
  2830. }
  2831. function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
  2832. const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
  2833. const locales = localeFallbacker(context, fallbackLocale, locale); // eslint-disable-line @typescript-eslint/no-explicit-any
  2834. let message = {};
  2835. let targetLocale;
  2836. let format = null;
  2837. let from = locale;
  2838. let to = null;
  2839. const type = 'translate';
  2840. for (let i = 0; i < locales.length; i++) {
  2841. targetLocale = to = locales[i];
  2842. if (locale !== targetLocale &&
  2843. isTranslateFallbackWarn(fallbackWarn, key)) {
  2844. onWarn(getWarnMessage$1(CoreWarnCodes.FALLBACK_TO_TRANSLATE, {
  2845. key,
  2846. target: targetLocale
  2847. }));
  2848. }
  2849. // for vue-devtools timeline event
  2850. if (locale !== targetLocale) {
  2851. const emitter = context.__v_emitter;
  2852. if (emitter) {
  2853. emitter.emit("fallback" /* VueDevToolsTimelineEvents.FALBACK */, {
  2854. type,
  2855. key,
  2856. from,
  2857. to,
  2858. groupId: `${type}:${key}`
  2859. });
  2860. }
  2861. }
  2862. message =
  2863. messages[targetLocale] || {};
  2864. // for vue-devtools timeline event
  2865. let start = null;
  2866. let startTag;
  2867. let endTag;
  2868. if (inBrowser) {
  2869. start = window.performance.now();
  2870. startTag = 'intlify-message-resolve-start';
  2871. endTag = 'intlify-message-resolve-end';
  2872. mark && mark(startTag);
  2873. }
  2874. if ((format = resolveValue(message, key)) === null) {
  2875. // if null, resolve with object key path
  2876. format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
  2877. }
  2878. // for vue-devtools timeline event
  2879. if (inBrowser) {
  2880. const end = window.performance.now();
  2881. const emitter = context.__v_emitter;
  2882. if (emitter && start && format) {
  2883. emitter.emit("message-resolve" /* VueDevToolsTimelineEvents.MESSAGE_RESOLVE */, {
  2884. type: "message-resolve" /* VueDevToolsTimelineEvents.MESSAGE_RESOLVE */,
  2885. key,
  2886. message: format,
  2887. time: end - start,
  2888. groupId: `${type}:${key}`
  2889. });
  2890. }
  2891. if (startTag && endTag && mark && measure) {
  2892. mark(endTag);
  2893. measure('intlify message resolve', startTag, endTag);
  2894. }
  2895. }
  2896. if (isString(format) || isMessageAST(format) || isMessageFunction(format)) {
  2897. break;
  2898. }
  2899. const missingRet = handleMissing(context, // eslint-disable-line @typescript-eslint/no-explicit-any
  2900. key, targetLocale, missingWarn, type);
  2901. if (missingRet !== key) {
  2902. format = missingRet;
  2903. }
  2904. from = to;
  2905. }
  2906. return [format, targetLocale, message];
  2907. }
  2908. function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
  2909. const { messageCompiler, warnHtmlMessage } = context;
  2910. if (isMessageFunction(format)) {
  2911. const msg = format;
  2912. msg.locale = msg.locale || targetLocale;
  2913. msg.key = msg.key || key;
  2914. return msg;
  2915. }
  2916. if (messageCompiler == null) {
  2917. const msg = (() => format);
  2918. msg.locale = targetLocale;
  2919. msg.key = key;
  2920. return msg;
  2921. }
  2922. // for vue-devtools timeline event
  2923. let start = null;
  2924. let startTag;
  2925. let endTag;
  2926. if (inBrowser) {
  2927. start = window.performance.now();
  2928. startTag = 'intlify-message-compilation-start';
  2929. endTag = 'intlify-message-compilation-end';
  2930. mark && mark(startTag);
  2931. }
  2932. const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
  2933. // for vue-devtools timeline event
  2934. if (inBrowser) {
  2935. const end = window.performance.now();
  2936. const emitter = context.__v_emitter;
  2937. if (emitter && start) {
  2938. emitter.emit("message-compilation" /* VueDevToolsTimelineEvents.MESSAGE_COMPILATION */, {
  2939. type: "message-compilation" /* VueDevToolsTimelineEvents.MESSAGE_COMPILATION */,
  2940. message: format,
  2941. time: end - start,
  2942. groupId: `${'translate'}:${key}`
  2943. });
  2944. }
  2945. if (startTag && endTag && mark && measure) {
  2946. mark(endTag);
  2947. measure('intlify message compilation', startTag, endTag);
  2948. }
  2949. }
  2950. msg.locale = targetLocale;
  2951. msg.key = key;
  2952. msg.source = format;
  2953. return msg;
  2954. }
  2955. function evaluateMessage(context, msg, msgCtx) {
  2956. // for vue-devtools timeline event
  2957. let start = null;
  2958. let startTag;
  2959. let endTag;
  2960. if (inBrowser) {
  2961. start = window.performance.now();
  2962. startTag = 'intlify-message-evaluation-start';
  2963. endTag = 'intlify-message-evaluation-end';
  2964. mark && mark(startTag);
  2965. }
  2966. const messaged = msg(msgCtx);
  2967. // for vue-devtools timeline event
  2968. if (inBrowser) {
  2969. const end = window.performance.now();
  2970. const emitter = context.__v_emitter;
  2971. if (emitter && start) {
  2972. emitter.emit("message-evaluation" /* VueDevToolsTimelineEvents.MESSAGE_EVALUATION */, {
  2973. type: "message-evaluation" /* VueDevToolsTimelineEvents.MESSAGE_EVALUATION */,
  2974. value: messaged,
  2975. time: end - start,
  2976. groupId: `${'translate'}:${msg.key}`
  2977. });
  2978. }
  2979. if (startTag && endTag && mark && measure) {
  2980. mark(endTag);
  2981. measure('intlify message evaluation', startTag, endTag);
  2982. }
  2983. }
  2984. return messaged;
  2985. }
  2986. /** @internal */
  2987. function parseTranslateArgs(...args) {
  2988. const [arg1, arg2, arg3] = args;
  2989. const options = {};
  2990. if (!isString(arg1) &&
  2991. !isNumber(arg1) &&
  2992. !isMessageFunction(arg1) &&
  2993. !isMessageAST(arg1)) {
  2994. throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
  2995. }
  2996. // prettier-ignore
  2997. const key = isNumber(arg1)
  2998. ? String(arg1)
  2999. : isMessageFunction(arg1)
  3000. ? arg1
  3001. : arg1;
  3002. if (isNumber(arg2)) {
  3003. options.plural = arg2;
  3004. }
  3005. else if (isString(arg2)) {
  3006. options.default = arg2;
  3007. }
  3008. else if (isPlainObject(arg2) && !isEmptyObject(arg2)) {
  3009. options.named = arg2;
  3010. }
  3011. else if (isArray(arg2)) {
  3012. options.list = arg2;
  3013. }
  3014. if (isNumber(arg3)) {
  3015. options.plural = arg3;
  3016. }
  3017. else if (isString(arg3)) {
  3018. options.default = arg3;
  3019. }
  3020. else if (isPlainObject(arg3)) {
  3021. assign(options, arg3);
  3022. }
  3023. return [key, options];
  3024. }
  3025. function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
  3026. return {
  3027. locale,
  3028. key,
  3029. warnHtmlMessage,
  3030. onError: (err) => {
  3031. onError && onError(err);
  3032. {
  3033. const _source = getSourceForCodeFrame(source);
  3034. const message = `Message compilation error: ${err.message}`;
  3035. const codeFrame = err.location &&
  3036. _source &&
  3037. generateCodeFrame(_source, err.location.start.offset, err.location.end.offset);
  3038. const emitter = context.__v_emitter;
  3039. if (emitter && _source) {
  3040. emitter.emit("compile-error" /* VueDevToolsTimelineEvents.COMPILE_ERROR */, {
  3041. message: _source,
  3042. error: err.message,
  3043. start: err.location && err.location.start.offset,
  3044. end: err.location && err.location.end.offset,
  3045. groupId: `${'translate'}:${key}`
  3046. });
  3047. }
  3048. console.error(codeFrame ? `${message}\n${codeFrame}` : message);
  3049. }
  3050. },
  3051. onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
  3052. };
  3053. }
  3054. function getSourceForCodeFrame(source) {
  3055. if (isString(source)) {
  3056. return source;
  3057. }
  3058. else {
  3059. if (source.loc && source.loc.source) {
  3060. return source.loc.source;
  3061. }
  3062. }
  3063. }
  3064. function getMessageContextOptions(context, locale, message, options) {
  3065. const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
  3066. const resolveMessage = (key) => {
  3067. let val = resolveValue(message, key);
  3068. // fallback to root context
  3069. if (val == null && fallbackContext) {
  3070. const [, , message] = resolveMessageFormat(fallbackContext, key, locale, fallbackLocale, fallbackWarn, missingWarn);
  3071. val = resolveValue(message, key);
  3072. }
  3073. if (isString(val) || isMessageAST(val)) {
  3074. let occurred = false;
  3075. const onError = () => {
  3076. occurred = true;
  3077. };
  3078. const msg = compileMessageFormat(context, key, locale, val, key, onError);
  3079. return !occurred
  3080. ? msg
  3081. : NOOP_MESSAGE_FUNCTION;
  3082. }
  3083. else if (isMessageFunction(val)) {
  3084. return val;
  3085. }
  3086. else {
  3087. // TODO: should be implemented warning message
  3088. return NOOP_MESSAGE_FUNCTION;
  3089. }
  3090. };
  3091. const ctxOptions = {
  3092. locale,
  3093. modifiers,
  3094. pluralRules,
  3095. messages: resolveMessage
  3096. };
  3097. if (context.processor) {
  3098. ctxOptions.processor = context.processor;
  3099. }
  3100. if (options.list) {
  3101. ctxOptions.list = options.list;
  3102. }
  3103. if (options.named) {
  3104. ctxOptions.named = options.named;
  3105. }
  3106. if (isNumber(options.plural)) {
  3107. ctxOptions.pluralIndex = options.plural;
  3108. }
  3109. return ctxOptions;
  3110. }
  3111. const intlDefined = typeof Intl !== 'undefined';
  3112. const Availabilities = {
  3113. dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== 'undefined',
  3114. numberFormat: intlDefined && typeof Intl.NumberFormat !== 'undefined'
  3115. };
  3116. // implementation of `datetime` function
  3117. function datetime(context, ...args) {
  3118. const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
  3119. const { __datetimeFormatters } = context;
  3120. if (!Availabilities.dateTimeFormat) {
  3121. onWarn(getWarnMessage$1(CoreWarnCodes.CANNOT_FORMAT_DATE));
  3122. return MISSING_RESOLVE_VALUE;
  3123. }
  3124. const [key, value, options, overrides] = parseDateTimeArgs(...args);
  3125. const missingWarn = isBoolean(options.missingWarn)
  3126. ? options.missingWarn
  3127. : context.missingWarn;
  3128. const fallbackWarn = isBoolean(options.fallbackWarn)
  3129. ? options.fallbackWarn
  3130. : context.fallbackWarn;
  3131. const part = !!options.part;
  3132. const locale = getLocale(context, options);
  3133. const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
  3134. fallbackLocale, locale);
  3135. if (!isString(key) || key === '') {
  3136. return new Intl.DateTimeFormat(locale, overrides).format(value);
  3137. }
  3138. // resolve format
  3139. let datetimeFormat = {};
  3140. let targetLocale;
  3141. let format = null;
  3142. let from = locale;
  3143. let to = null;
  3144. const type = 'datetime format';
  3145. for (let i = 0; i < locales.length; i++) {
  3146. targetLocale = to = locales[i];
  3147. if (locale !== targetLocale &&
  3148. isTranslateFallbackWarn(fallbackWarn, key)) {
  3149. onWarn(getWarnMessage$1(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
  3150. key,
  3151. target: targetLocale
  3152. }));
  3153. }
  3154. // for vue-devtools timeline event
  3155. if (locale !== targetLocale) {
  3156. const emitter = context.__v_emitter;
  3157. if (emitter) {
  3158. emitter.emit("fallback" /* VueDevToolsTimelineEvents.FALBACK */, {
  3159. type,
  3160. key,
  3161. from,
  3162. to,
  3163. groupId: `${type}:${key}`
  3164. });
  3165. }
  3166. }
  3167. datetimeFormat =
  3168. datetimeFormats[targetLocale] || {};
  3169. format = datetimeFormat[key];
  3170. if (isPlainObject(format))
  3171. break;
  3172. handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
  3173. from = to;
  3174. }
  3175. // checking format and target locale
  3176. if (!isPlainObject(format) || !isString(targetLocale)) {
  3177. return unresolving ? NOT_REOSLVED : key;
  3178. }
  3179. let id = `${targetLocale}__${key}`;
  3180. if (!isEmptyObject(overrides)) {
  3181. id = `${id}__${JSON.stringify(overrides)}`;
  3182. }
  3183. let formatter = __datetimeFormatters.get(id);
  3184. if (!formatter) {
  3185. formatter = new Intl.DateTimeFormat(targetLocale, assign({}, format, overrides));
  3186. __datetimeFormatters.set(id, formatter);
  3187. }
  3188. return !part ? formatter.format(value) : formatter.formatToParts(value);
  3189. }
  3190. /** @internal */
  3191. const DATETIME_FORMAT_OPTIONS_KEYS = [
  3192. 'localeMatcher',
  3193. 'weekday',
  3194. 'era',
  3195. 'year',
  3196. 'month',
  3197. 'day',
  3198. 'hour',
  3199. 'minute',
  3200. 'second',
  3201. 'timeZoneName',
  3202. 'formatMatcher',
  3203. 'hour12',
  3204. 'timeZone',
  3205. 'dateStyle',
  3206. 'timeStyle',
  3207. 'calendar',
  3208. 'dayPeriod',
  3209. 'numberingSystem',
  3210. 'hourCycle',
  3211. 'fractionalSecondDigits'
  3212. ];
  3213. /** @internal */
  3214. function parseDateTimeArgs(...args) {
  3215. const [arg1, arg2, arg3, arg4] = args;
  3216. const options = {};
  3217. let overrides = {};
  3218. let value;
  3219. if (isString(arg1)) {
  3220. // Only allow ISO strings - other date formats are often supported,
  3221. // but may cause different results in different browsers.
  3222. const matches = arg1.match(/(\d{4}-\d{2}-\d{2})(T|\s)?(.*)/);
  3223. if (!matches) {
  3224. throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
  3225. }
  3226. // Some browsers can not parse the iso datetime separated by space,
  3227. // this is a compromise solution by replace the 'T'/' ' with 'T'
  3228. const dateTime = matches[3]
  3229. ? matches[3].trim().startsWith('T')
  3230. ? `${matches[1].trim()}${matches[3].trim()}`
  3231. : `${matches[1].trim()}T${matches[3].trim()}`
  3232. : matches[1].trim();
  3233. value = new Date(dateTime);
  3234. try {
  3235. // This will fail if the date is not valid
  3236. value.toISOString();
  3237. }
  3238. catch (e) {
  3239. throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
  3240. }
  3241. }
  3242. else if (isDate(arg1)) {
  3243. if (isNaN(arg1.getTime())) {
  3244. throw createCoreError(CoreErrorCodes.INVALID_DATE_ARGUMENT);
  3245. }
  3246. value = arg1;
  3247. }
  3248. else if (isNumber(arg1)) {
  3249. value = arg1;
  3250. }
  3251. else {
  3252. throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
  3253. }
  3254. if (isString(arg2)) {
  3255. options.key = arg2;
  3256. }
  3257. else if (isPlainObject(arg2)) {
  3258. Object.keys(arg2).forEach(key => {
  3259. if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
  3260. overrides[key] = arg2[key];
  3261. }
  3262. else {
  3263. options[key] = arg2[key];
  3264. }
  3265. });
  3266. }
  3267. if (isString(arg3)) {
  3268. options.locale = arg3;
  3269. }
  3270. else if (isPlainObject(arg3)) {
  3271. overrides = arg3;
  3272. }
  3273. if (isPlainObject(arg4)) {
  3274. overrides = arg4;
  3275. }
  3276. return [options.key || '', value, options, overrides];
  3277. }
  3278. /** @internal */
  3279. function clearDateTimeFormat(ctx, locale, format) {
  3280. const context = ctx;
  3281. for (const key in format) {
  3282. const id = `${locale}__${key}`;
  3283. if (!context.__datetimeFormatters.has(id)) {
  3284. continue;
  3285. }
  3286. context.__datetimeFormatters.delete(id);
  3287. }
  3288. }
  3289. // implementation of `number` function
  3290. function number(context, ...args) {
  3291. const { numberFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
  3292. const { __numberFormatters } = context;
  3293. if (!Availabilities.numberFormat) {
  3294. onWarn(getWarnMessage$1(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
  3295. return MISSING_RESOLVE_VALUE;
  3296. }
  3297. const [key, value, options, overrides] = parseNumberArgs(...args);
  3298. const missingWarn = isBoolean(options.missingWarn)
  3299. ? options.missingWarn
  3300. : context.missingWarn;
  3301. const fallbackWarn = isBoolean(options.fallbackWarn)
  3302. ? options.fallbackWarn
  3303. : context.fallbackWarn;
  3304. const part = !!options.part;
  3305. const locale = getLocale(context, options);
  3306. const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
  3307. fallbackLocale, locale);
  3308. if (!isString(key) || key === '') {
  3309. return new Intl.NumberFormat(locale, overrides).format(value);
  3310. }
  3311. // resolve format
  3312. let numberFormat = {};
  3313. let targetLocale;
  3314. let format = null;
  3315. let from = locale;
  3316. let to = null;
  3317. const type = 'number format';
  3318. for (let i = 0; i < locales.length; i++) {
  3319. targetLocale = to = locales[i];
  3320. if (locale !== targetLocale &&
  3321. isTranslateFallbackWarn(fallbackWarn, key)) {
  3322. onWarn(getWarnMessage$1(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
  3323. key,
  3324. target: targetLocale
  3325. }));
  3326. }
  3327. // for vue-devtools timeline event
  3328. if (locale !== targetLocale) {
  3329. const emitter = context.__v_emitter;
  3330. if (emitter) {
  3331. emitter.emit("fallback" /* VueDevToolsTimelineEvents.FALBACK */, {
  3332. type,
  3333. key,
  3334. from,
  3335. to,
  3336. groupId: `${type}:${key}`
  3337. });
  3338. }
  3339. }
  3340. numberFormat =
  3341. numberFormats[targetLocale] || {};
  3342. format = numberFormat[key];
  3343. if (isPlainObject(format))
  3344. break;
  3345. handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
  3346. from = to;
  3347. }
  3348. // checking format and target locale
  3349. if (!isPlainObject(format) || !isString(targetLocale)) {
  3350. return unresolving ? NOT_REOSLVED : key;
  3351. }
  3352. let id = `${targetLocale}__${key}`;
  3353. if (!isEmptyObject(overrides)) {
  3354. id = `${id}__${JSON.stringify(overrides)}`;
  3355. }
  3356. let formatter = __numberFormatters.get(id);
  3357. if (!formatter) {
  3358. formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
  3359. __numberFormatters.set(id, formatter);
  3360. }
  3361. return !part ? formatter.format(value) : formatter.formatToParts(value);
  3362. }
  3363. /** @internal */
  3364. const NUMBER_FORMAT_OPTIONS_KEYS = [
  3365. 'localeMatcher',
  3366. 'style',
  3367. 'currency',
  3368. 'currencyDisplay',
  3369. 'currencySign',
  3370. 'useGrouping',
  3371. 'minimumIntegerDigits',
  3372. 'minimumFractionDigits',
  3373. 'maximumFractionDigits',
  3374. 'minimumSignificantDigits',
  3375. 'maximumSignificantDigits',
  3376. 'compactDisplay',
  3377. 'notation',
  3378. 'signDisplay',
  3379. 'unit',
  3380. 'unitDisplay',
  3381. 'roundingMode',
  3382. 'roundingPriority',
  3383. 'roundingIncrement',
  3384. 'trailingZeroDisplay'
  3385. ];
  3386. /** @internal */
  3387. function parseNumberArgs(...args) {
  3388. const [arg1, arg2, arg3, arg4] = args;
  3389. const options = {};
  3390. let overrides = {};
  3391. if (!isNumber(arg1)) {
  3392. throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
  3393. }
  3394. const value = arg1;
  3395. if (isString(arg2)) {
  3396. options.key = arg2;
  3397. }
  3398. else if (isPlainObject(arg2)) {
  3399. Object.keys(arg2).forEach(key => {
  3400. if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
  3401. overrides[key] = arg2[key];
  3402. }
  3403. else {
  3404. options[key] = arg2[key];
  3405. }
  3406. });
  3407. }
  3408. if (isString(arg3)) {
  3409. options.locale = arg3;
  3410. }
  3411. else if (isPlainObject(arg3)) {
  3412. overrides = arg3;
  3413. }
  3414. if (isPlainObject(arg4)) {
  3415. overrides = arg4;
  3416. }
  3417. return [options.key || '', value, options, overrides];
  3418. }
  3419. /** @internal */
  3420. function clearNumberFormat(ctx, locale, format) {
  3421. const context = ctx;
  3422. for (const key in format) {
  3423. const id = `${locale}__${key}`;
  3424. if (!context.__numberFormatters.has(id)) {
  3425. continue;
  3426. }
  3427. context.__numberFormatters.delete(id);
  3428. }
  3429. }
  3430. /**
  3431. * Vue I18n Version
  3432. *
  3433. * @remarks
  3434. * Semver format. Same format as the package.json `version` field.
  3435. *
  3436. * @VueI18nGeneral
  3437. */
  3438. const VERSION = '9.8.0';
  3439. /**
  3440. * This is only called development env
  3441. * istanbul-ignore-next
  3442. */
  3443. function initDev() {
  3444. {
  3445. {
  3446. console.info(`You are running a development build of vue-i18n.\n` +
  3447. `Make sure to use the production build (*.prod.js) when deploying for production.`);
  3448. }
  3449. }
  3450. }
  3451. const code$1 = CoreWarnCodes.__EXTEND_POINT__;
  3452. const inc$1 = incrementer(code$1);
  3453. const I18nWarnCodes = {
  3454. FALLBACK_TO_ROOT: code$1,
  3455. NOT_SUPPORTED_PRESERVE: inc$1(),
  3456. NOT_SUPPORTED_FORMATTER: inc$1(),
  3457. NOT_SUPPORTED_PRESERVE_DIRECTIVE: inc$1(),
  3458. NOT_SUPPORTED_GET_CHOICE_INDEX: inc$1(),
  3459. COMPONENT_NAME_LEGACY_COMPATIBLE: inc$1(),
  3460. NOT_FOUND_PARENT_SCOPE: inc$1(),
  3461. IGNORE_OBJ_FLATTEN: inc$1(),
  3462. NOTICE_DROP_ALLOW_COMPOSITION: inc$1() // 17
  3463. };
  3464. const warnMessages = {
  3465. [I18nWarnCodes.FALLBACK_TO_ROOT]: `Fall back to {type} '{key}' with root locale.`,
  3466. [I18nWarnCodes.NOT_SUPPORTED_PRESERVE]: `Not supported 'preserve'.`,
  3467. [I18nWarnCodes.NOT_SUPPORTED_FORMATTER]: `Not supported 'formatter'.`,
  3468. [I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE]: `Not supported 'preserveDirectiveContent'.`,
  3469. [I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX]: `Not supported 'getChoiceIndex'.`,
  3470. [I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `Component name legacy compatible: '{name}' -> 'i18n'`,
  3471. [I18nWarnCodes.NOT_FOUND_PARENT_SCOPE]: `Not found parent scope. use the global scope.`,
  3472. [I18nWarnCodes.IGNORE_OBJ_FLATTEN]: `Ignore object flatten: '{key}' key has an string value`,
  3473. [I18nWarnCodes.NOTICE_DROP_ALLOW_COMPOSITION]: `'allowComposition' option will be dropped in the next major version. For more information, please see 👉 https://tinyurl.com/2p97mcze`
  3474. };
  3475. function getWarnMessage(code, ...args) {
  3476. return format$1(warnMessages[code], ...args);
  3477. }
  3478. const code = CoreErrorCodes.__EXTEND_POINT__;
  3479. const inc = incrementer(code);
  3480. const I18nErrorCodes = {
  3481. // composer module errors
  3482. UNEXPECTED_RETURN_TYPE: code,
  3483. // legacy module errors
  3484. INVALID_ARGUMENT: inc(),
  3485. // i18n module errors
  3486. MUST_BE_CALL_SETUP_TOP: inc(),
  3487. NOT_INSTALLED: inc(),
  3488. NOT_AVAILABLE_IN_LEGACY_MODE: inc(),
  3489. // directive module errors
  3490. REQUIRED_VALUE: inc(),
  3491. INVALID_VALUE: inc(),
  3492. // vue-devtools errors
  3493. CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: inc(),
  3494. NOT_INSTALLED_WITH_PROVIDE: inc(),
  3495. // unexpected error
  3496. UNEXPECTED_ERROR: inc(),
  3497. // not compatible legacy vue-i18n constructor
  3498. NOT_COMPATIBLE_LEGACY_VUE_I18N: inc(),
  3499. // bridge support vue 2.x only
  3500. BRIDGE_SUPPORT_VUE_2_ONLY: inc(),
  3501. // need to define `i18n` option in `allowComposition: true` and `useScope: 'local' at `useI18n``
  3502. MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION: inc(),
  3503. // Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly
  3504. NOT_AVAILABLE_COMPOSITION_IN_LEGACY: inc(),
  3505. // for enhancement
  3506. __EXTEND_POINT__: inc() // 40
  3507. };
  3508. function createI18nError(code, ...args) {
  3509. return createCompileError(code, null, { messages: errorMessages, args } );
  3510. }
  3511. const errorMessages = {
  3512. [I18nErrorCodes.UNEXPECTED_RETURN_TYPE]: 'Unexpected return type in composer',
  3513. [I18nErrorCodes.INVALID_ARGUMENT]: 'Invalid argument',
  3514. [I18nErrorCodes.MUST_BE_CALL_SETUP_TOP]: 'Must be called at the top of a `setup` function',
  3515. [I18nErrorCodes.NOT_INSTALLED]: 'Need to install with `app.use` function',
  3516. [I18nErrorCodes.UNEXPECTED_ERROR]: 'Unexpected error',
  3517. [I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE]: 'Not available in legacy mode',
  3518. [I18nErrorCodes.REQUIRED_VALUE]: `Required in value: {0}`,
  3519. [I18nErrorCodes.INVALID_VALUE]: `Invalid value`,
  3520. [I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN]: `Cannot setup vue-devtools plugin`,
  3521. [I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE]: 'Need to install with `provide` function',
  3522. [I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N]: 'Not compatible legacy VueI18n.',
  3523. [I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY]: 'vue-i18n-bridge support Vue 2.x only',
  3524. [I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION]: 'Must define ‘i18n’ option or custom block in Composition API with using local scope in Legacy API mode',
  3525. [I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY]: 'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly'
  3526. };
  3527. const TranslateVNodeSymbol =
  3528. /* #__PURE__*/ makeSymbol('__translateVNode');
  3529. const DatetimePartsSymbol = /* #__PURE__*/ makeSymbol('__datetimeParts');
  3530. const NumberPartsSymbol = /* #__PURE__*/ makeSymbol('__numberParts');
  3531. const EnableEmitter = /* #__PURE__*/ makeSymbol('__enableEmitter');
  3532. const DisableEmitter = /* #__PURE__*/ makeSymbol('__disableEmitter');
  3533. const SetPluralRulesSymbol = makeSymbol('__setPluralRules');
  3534. const InejctWithOptionSymbol =
  3535. /* #__PURE__*/ makeSymbol('__injectWithOption');
  3536. const DisposeSymbol = /* #__PURE__*/ makeSymbol('__dispose');
  3537. const __VUE_I18N_BRIDGE__ = '__VUE_I18N_BRIDGE__';
  3538. /* eslint-disable @typescript-eslint/no-explicit-any */
  3539. /**
  3540. * Transform flat json in obj to normal json in obj
  3541. */
  3542. function handleFlatJson(obj) {
  3543. // check obj
  3544. if (!isObject(obj)) {
  3545. return obj;
  3546. }
  3547. for (const key in obj) {
  3548. // check key
  3549. if (!hasOwn(obj, key)) {
  3550. continue;
  3551. }
  3552. // handle for normal json
  3553. if (!key.includes('.')) {
  3554. // recursive process value if value is also a object
  3555. if (isObject(obj[key])) {
  3556. handleFlatJson(obj[key]);
  3557. }
  3558. }
  3559. // handle for flat json, transform to normal json
  3560. else {
  3561. // go to the last object
  3562. const subKeys = key.split('.');
  3563. const lastIndex = subKeys.length - 1;
  3564. let currentObj = obj;
  3565. let hasStringValue = false;
  3566. for (let i = 0; i < lastIndex; i++) {
  3567. if (!(subKeys[i] in currentObj)) {
  3568. currentObj[subKeys[i]] = {};
  3569. }
  3570. if (!isObject(currentObj[subKeys[i]])) {
  3571. warn(getWarnMessage(I18nWarnCodes.IGNORE_OBJ_FLATTEN, {
  3572. key: subKeys[i]
  3573. }));
  3574. hasStringValue = true;
  3575. break;
  3576. }
  3577. currentObj = currentObj[subKeys[i]];
  3578. }
  3579. // update last object value, delete old property
  3580. if (!hasStringValue) {
  3581. currentObj[subKeys[lastIndex]] = obj[key];
  3582. delete obj[key];
  3583. }
  3584. // recursive process value if value is also a object
  3585. if (isObject(currentObj[subKeys[lastIndex]])) {
  3586. handleFlatJson(currentObj[subKeys[lastIndex]]);
  3587. }
  3588. }
  3589. }
  3590. return obj;
  3591. }
  3592. function getLocaleMessages(locale, options) {
  3593. const { messages, __i18n, messageResolver, flatJson } = options;
  3594. // prettier-ignore
  3595. const ret = (isPlainObject(messages)
  3596. ? messages
  3597. : isArray(__i18n)
  3598. ? {}
  3599. : { [locale]: {} });
  3600. // merge locale messages of i18n custom block
  3601. if (isArray(__i18n)) {
  3602. __i18n.forEach(custom => {
  3603. if ('locale' in custom && 'resource' in custom) {
  3604. const { locale, resource } = custom;
  3605. if (locale) {
  3606. ret[locale] = ret[locale] || {};
  3607. deepCopy(resource, ret[locale]);
  3608. }
  3609. else {
  3610. deepCopy(resource, ret);
  3611. }
  3612. }
  3613. else {
  3614. isString(custom) && deepCopy(JSON.parse(custom), ret);
  3615. }
  3616. });
  3617. }
  3618. // handle messages for flat json
  3619. if (messageResolver == null && flatJson) {
  3620. for (const key in ret) {
  3621. if (hasOwn(ret, key)) {
  3622. handleFlatJson(ret[key]);
  3623. }
  3624. }
  3625. }
  3626. return ret;
  3627. }
  3628. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3629. function getComponentOptions(instance) {
  3630. return instance.type ;
  3631. }
  3632. function adjustI18nResources(gl, options, componentOptions // eslint-disable-line @typescript-eslint/no-explicit-any
  3633. ) {
  3634. let messages = isObject(options.messages) ? options.messages : {};
  3635. if ('__i18nGlobal' in componentOptions) {
  3636. messages = getLocaleMessages(gl.locale.value, {
  3637. messages,
  3638. __i18n: componentOptions.__i18nGlobal
  3639. });
  3640. }
  3641. // merge locale messages
  3642. const locales = Object.keys(messages);
  3643. if (locales.length) {
  3644. locales.forEach(locale => {
  3645. gl.mergeLocaleMessage(locale, messages[locale]);
  3646. });
  3647. }
  3648. {
  3649. // merge datetime formats
  3650. if (isObject(options.datetimeFormats)) {
  3651. const locales = Object.keys(options.datetimeFormats);
  3652. if (locales.length) {
  3653. locales.forEach(locale => {
  3654. gl.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
  3655. });
  3656. }
  3657. }
  3658. // merge number formats
  3659. if (isObject(options.numberFormats)) {
  3660. const locales = Object.keys(options.numberFormats);
  3661. if (locales.length) {
  3662. locales.forEach(locale => {
  3663. gl.mergeNumberFormat(locale, options.numberFormats[locale]);
  3664. });
  3665. }
  3666. }
  3667. }
  3668. }
  3669. function createTextNode(key) {
  3670. return vue.createVNode(vue.Text, null, key, 0)
  3671. ;
  3672. }
  3673. /* eslint-enable @typescript-eslint/no-explicit-any */
  3674. /* eslint-disable @typescript-eslint/no-explicit-any */
  3675. // extend VNode interface
  3676. const DEVTOOLS_META = '__INTLIFY_META__';
  3677. const NOOP_RETURN_ARRAY = () => [];
  3678. const NOOP_RETURN_FALSE = () => false;
  3679. let composerID = 0;
  3680. function defineCoreMissingHandler(missing) {
  3681. return ((ctx, locale, key, type) => {
  3682. return missing(locale, key, vue.getCurrentInstance() || undefined, type);
  3683. });
  3684. }
  3685. // for Intlify DevTools
  3686. /* #__NO_SIDE_EFFECTS__ */
  3687. const getMetaInfo = () => {
  3688. const instance = vue.getCurrentInstance();
  3689. let meta = null; // eslint-disable-line @typescript-eslint/no-explicit-any
  3690. return instance && (meta = getComponentOptions(instance)[DEVTOOLS_META])
  3691. ? { [DEVTOOLS_META]: meta } // eslint-disable-line @typescript-eslint/no-explicit-any
  3692. : null;
  3693. };
  3694. /**
  3695. * Create composer interface factory
  3696. *
  3697. * @internal
  3698. */
  3699. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  3700. function createComposer(options = {}, VueI18nLegacy) {
  3701. const { __root, __injectWithOption } = options;
  3702. const _isGlobal = __root === undefined;
  3703. const flatJson = options.flatJson;
  3704. let _inheritLocale = isBoolean(options.inheritLocale)
  3705. ? options.inheritLocale
  3706. : true;
  3707. const _locale = vue.ref(
  3708. // prettier-ignore
  3709. __root && _inheritLocale
  3710. ? __root.locale.value
  3711. : isString(options.locale)
  3712. ? options.locale
  3713. : DEFAULT_LOCALE);
  3714. const _fallbackLocale = vue.ref(
  3715. // prettier-ignore
  3716. __root && _inheritLocale
  3717. ? __root.fallbackLocale.value
  3718. : isString(options.fallbackLocale) ||
  3719. isArray(options.fallbackLocale) ||
  3720. isPlainObject(options.fallbackLocale) ||
  3721. options.fallbackLocale === false
  3722. ? options.fallbackLocale
  3723. : _locale.value);
  3724. const _messages = vue.ref(getLocaleMessages(_locale.value, options));
  3725. // prettier-ignore
  3726. const _datetimeFormats = vue.ref(isPlainObject(options.datetimeFormats)
  3727. ? options.datetimeFormats
  3728. : { [_locale.value]: {} })
  3729. ;
  3730. // prettier-ignore
  3731. const _numberFormats = vue.ref(isPlainObject(options.numberFormats)
  3732. ? options.numberFormats
  3733. : { [_locale.value]: {} })
  3734. ;
  3735. // warning suppress options
  3736. // prettier-ignore
  3737. let _missingWarn = __root
  3738. ? __root.missingWarn
  3739. : isBoolean(options.missingWarn) || isRegExp(options.missingWarn)
  3740. ? options.missingWarn
  3741. : true;
  3742. // prettier-ignore
  3743. let _fallbackWarn = __root
  3744. ? __root.fallbackWarn
  3745. : isBoolean(options.fallbackWarn) || isRegExp(options.fallbackWarn)
  3746. ? options.fallbackWarn
  3747. : true;
  3748. // prettier-ignore
  3749. let _fallbackRoot = __root
  3750. ? __root.fallbackRoot
  3751. : isBoolean(options.fallbackRoot)
  3752. ? options.fallbackRoot
  3753. : true;
  3754. // configure fall back to root
  3755. let _fallbackFormat = !!options.fallbackFormat;
  3756. // runtime missing
  3757. let _missing = isFunction(options.missing) ? options.missing : null;
  3758. let _runtimeMissing = isFunction(options.missing)
  3759. ? defineCoreMissingHandler(options.missing)
  3760. : null;
  3761. // postTranslation handler
  3762. let _postTranslation = isFunction(options.postTranslation)
  3763. ? options.postTranslation
  3764. : null;
  3765. // prettier-ignore
  3766. let _warnHtmlMessage = __root
  3767. ? __root.warnHtmlMessage
  3768. : isBoolean(options.warnHtmlMessage)
  3769. ? options.warnHtmlMessage
  3770. : true;
  3771. let _escapeParameter = !!options.escapeParameter;
  3772. // custom linked modifiers
  3773. // prettier-ignore
  3774. const _modifiers = __root
  3775. ? __root.modifiers
  3776. : isPlainObject(options.modifiers)
  3777. ? options.modifiers
  3778. : {};
  3779. // pluralRules
  3780. let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
  3781. // runtime context
  3782. // eslint-disable-next-line prefer-const
  3783. let _context;
  3784. const getCoreContext = () => {
  3785. _isGlobal && setFallbackContext(null);
  3786. const ctxOptions = {
  3787. version: VERSION,
  3788. locale: _locale.value,
  3789. fallbackLocale: _fallbackLocale.value,
  3790. messages: _messages.value,
  3791. modifiers: _modifiers,
  3792. pluralRules: _pluralRules,
  3793. missing: _runtimeMissing === null ? undefined : _runtimeMissing,
  3794. missingWarn: _missingWarn,
  3795. fallbackWarn: _fallbackWarn,
  3796. fallbackFormat: _fallbackFormat,
  3797. unresolving: true,
  3798. postTranslation: _postTranslation === null ? undefined : _postTranslation,
  3799. warnHtmlMessage: _warnHtmlMessage,
  3800. escapeParameter: _escapeParameter,
  3801. messageResolver: options.messageResolver,
  3802. messageCompiler: options.messageCompiler,
  3803. __meta: { framework: 'vue' }
  3804. };
  3805. {
  3806. ctxOptions.datetimeFormats = _datetimeFormats.value;
  3807. ctxOptions.numberFormats = _numberFormats.value;
  3808. ctxOptions.__datetimeFormatters = isPlainObject(_context)
  3809. ? _context.__datetimeFormatters
  3810. : undefined;
  3811. ctxOptions.__numberFormatters = isPlainObject(_context)
  3812. ? _context.__numberFormatters
  3813. : undefined;
  3814. }
  3815. {
  3816. ctxOptions.__v_emitter = isPlainObject(_context)
  3817. ? _context.__v_emitter
  3818. : undefined;
  3819. }
  3820. const ctx = createCoreContext(ctxOptions);
  3821. _isGlobal && setFallbackContext(ctx);
  3822. return ctx;
  3823. };
  3824. _context = getCoreContext();
  3825. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  3826. // track reactivity
  3827. function trackReactivityValues() {
  3828. return [
  3829. _locale.value,
  3830. _fallbackLocale.value,
  3831. _messages.value,
  3832. _datetimeFormats.value,
  3833. _numberFormats.value
  3834. ]
  3835. ;
  3836. }
  3837. // locale
  3838. const locale = vue.computed({
  3839. get: () => _locale.value,
  3840. set: val => {
  3841. _locale.value = val;
  3842. _context.locale = _locale.value;
  3843. }
  3844. });
  3845. // fallbackLocale
  3846. const fallbackLocale = vue.computed({
  3847. get: () => _fallbackLocale.value,
  3848. set: val => {
  3849. _fallbackLocale.value = val;
  3850. _context.fallbackLocale = _fallbackLocale.value;
  3851. updateFallbackLocale(_context, _locale.value, val);
  3852. }
  3853. });
  3854. // messages
  3855. const messages = vue.computed(() => _messages.value);
  3856. // datetimeFormats
  3857. const datetimeFormats = /* #__PURE__*/ vue.computed(() => _datetimeFormats.value);
  3858. // numberFormats
  3859. const numberFormats = /* #__PURE__*/ vue.computed(() => _numberFormats.value);
  3860. // getPostTranslationHandler
  3861. function getPostTranslationHandler() {
  3862. return isFunction(_postTranslation) ? _postTranslation : null;
  3863. }
  3864. // setPostTranslationHandler
  3865. function setPostTranslationHandler(handler) {
  3866. _postTranslation = handler;
  3867. _context.postTranslation = handler;
  3868. }
  3869. // getMissingHandler
  3870. function getMissingHandler() {
  3871. return _missing;
  3872. }
  3873. // setMissingHandler
  3874. function setMissingHandler(handler) {
  3875. if (handler !== null) {
  3876. _runtimeMissing = defineCoreMissingHandler(handler);
  3877. }
  3878. _missing = handler;
  3879. _context.missing = _runtimeMissing;
  3880. }
  3881. function isResolvedTranslateMessage(type, arg // eslint-disable-line @typescript-eslint/no-explicit-any
  3882. ) {
  3883. return type !== 'translate' || !arg.resolvedMessage;
  3884. }
  3885. const wrapWithDeps = (fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) => {
  3886. trackReactivityValues(); // track reactive dependency
  3887. // NOTE: experimental !!
  3888. let ret;
  3889. try {
  3890. if (true || false) {
  3891. setAdditionalMeta(getMetaInfo());
  3892. }
  3893. if (!_isGlobal) {
  3894. _context.fallbackContext = __root
  3895. ? getFallbackContext()
  3896. : undefined;
  3897. }
  3898. ret = fn(_context);
  3899. }
  3900. finally {
  3901. if (!_isGlobal) {
  3902. _context.fallbackContext = undefined;
  3903. }
  3904. }
  3905. if ((warnType !== 'translate exists' && // for not `te` (e.g `t`)
  3906. isNumber(ret) &&
  3907. ret === NOT_REOSLVED) ||
  3908. (warnType === 'translate exists' && !ret) // for `te`
  3909. ) {
  3910. const [key, arg2] = argumentParser();
  3911. if (__root &&
  3912. isString(key) &&
  3913. isResolvedTranslateMessage(warnType, arg2)) {
  3914. if (_fallbackRoot &&
  3915. (isTranslateFallbackWarn(_fallbackWarn, key) ||
  3916. isTranslateMissingWarn(_missingWarn, key))) {
  3917. warn(getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
  3918. key,
  3919. type: warnType
  3920. }));
  3921. }
  3922. // for vue-devtools timeline event
  3923. {
  3924. const { __v_emitter: emitter } = _context;
  3925. if (emitter && _fallbackRoot) {
  3926. emitter.emit("fallback" /* VueDevToolsTimelineEvents.FALBACK */, {
  3927. type: warnType,
  3928. key,
  3929. to: 'global',
  3930. groupId: `${warnType}:${key}`
  3931. });
  3932. }
  3933. }
  3934. }
  3935. return __root && _fallbackRoot
  3936. ? fallbackSuccess(__root)
  3937. : fallbackFail(key);
  3938. }
  3939. else if (successCondition(ret)) {
  3940. return ret;
  3941. }
  3942. else {
  3943. /* istanbul ignore next */
  3944. throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE);
  3945. }
  3946. };
  3947. // t
  3948. function t(...args) {
  3949. return wrapWithDeps(context => Reflect.apply(translate, null, [context, ...args]), () => parseTranslateArgs(...args), 'translate', root => Reflect.apply(root.t, root, [...args]), key => key, val => isString(val));
  3950. }
  3951. // rt
  3952. function rt(...args) {
  3953. const [arg1, arg2, arg3] = args;
  3954. if (arg3 && !isObject(arg3)) {
  3955. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  3956. }
  3957. return t(...[arg1, arg2, assign({ resolvedMessage: true }, arg3 || {})]);
  3958. }
  3959. // d
  3960. function d(...args) {
  3961. return wrapWithDeps(context => Reflect.apply(datetime, null, [context, ...args]), () => parseDateTimeArgs(...args), 'datetime format', root => Reflect.apply(root.d, root, [...args]), () => MISSING_RESOLVE_VALUE, val => isString(val));
  3962. }
  3963. // n
  3964. function n(...args) {
  3965. return wrapWithDeps(context => Reflect.apply(number, null, [context, ...args]), () => parseNumberArgs(...args), 'number format', root => Reflect.apply(root.n, root, [...args]), () => MISSING_RESOLVE_VALUE, val => isString(val));
  3966. }
  3967. // for custom processor
  3968. function normalize(values) {
  3969. return values.map(val => isString(val) || isNumber(val) || isBoolean(val)
  3970. ? createTextNode(String(val))
  3971. : val);
  3972. }
  3973. const interpolate = (val) => val;
  3974. const processor = {
  3975. normalize,
  3976. interpolate,
  3977. type: 'vnode'
  3978. };
  3979. // translateVNode, using for `i18n-t` component
  3980. function translateVNode(...args) {
  3981. return wrapWithDeps(context => {
  3982. let ret;
  3983. const _context = context;
  3984. try {
  3985. _context.processor = processor;
  3986. ret = Reflect.apply(translate, null, [_context, ...args]);
  3987. }
  3988. finally {
  3989. _context.processor = null;
  3990. }
  3991. return ret;
  3992. }, () => parseTranslateArgs(...args), 'translate',
  3993. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3994. root => root[TranslateVNodeSymbol](...args), key => [createTextNode(key)], val => isArray(val));
  3995. }
  3996. // numberParts, using for `i18n-n` component
  3997. function numberParts(...args) {
  3998. return wrapWithDeps(context => Reflect.apply(number, null, [context, ...args]), () => parseNumberArgs(...args), 'number format',
  3999. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4000. root => root[NumberPartsSymbol](...args), NOOP_RETURN_ARRAY, val => isString(val) || isArray(val));
  4001. }
  4002. // datetimeParts, using for `i18n-d` component
  4003. function datetimeParts(...args) {
  4004. return wrapWithDeps(context => Reflect.apply(datetime, null, [context, ...args]), () => parseDateTimeArgs(...args), 'datetime format',
  4005. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4006. root => root[DatetimePartsSymbol](...args), NOOP_RETURN_ARRAY, val => isString(val) || isArray(val));
  4007. }
  4008. function setPluralRules(rules) {
  4009. _pluralRules = rules;
  4010. _context.pluralRules = _pluralRules;
  4011. }
  4012. // te
  4013. function te(key, locale) {
  4014. return wrapWithDeps(() => {
  4015. if (!key) {
  4016. return false;
  4017. }
  4018. const targetLocale = isString(locale) ? locale : _locale.value;
  4019. const message = getLocaleMessage(targetLocale);
  4020. const resolved = _context.messageResolver(message, key);
  4021. return (isMessageAST(resolved) ||
  4022. isMessageFunction(resolved) ||
  4023. isString(resolved));
  4024. }, () => [key], 'translate exists', root => {
  4025. return Reflect.apply(root.te, root, [key, locale]);
  4026. }, NOOP_RETURN_FALSE, val => isBoolean(val));
  4027. }
  4028. function resolveMessages(key) {
  4029. let messages = null;
  4030. const locales = fallbackWithLocaleChain(_context, _fallbackLocale.value, _locale.value);
  4031. for (let i = 0; i < locales.length; i++) {
  4032. const targetLocaleMessages = _messages.value[locales[i]] || {};
  4033. const messageValue = _context.messageResolver(targetLocaleMessages, key);
  4034. if (messageValue != null) {
  4035. messages = messageValue;
  4036. break;
  4037. }
  4038. }
  4039. return messages;
  4040. }
  4041. // tm
  4042. function tm(key) {
  4043. const messages = resolveMessages(key);
  4044. // prettier-ignore
  4045. return messages != null
  4046. ? messages
  4047. : __root
  4048. ? __root.tm(key) || {}
  4049. : {};
  4050. }
  4051. // getLocaleMessage
  4052. function getLocaleMessage(locale) {
  4053. return (_messages.value[locale] || {});
  4054. }
  4055. // setLocaleMessage
  4056. function setLocaleMessage(locale, message) {
  4057. if (flatJson) {
  4058. const _message = { [locale]: message };
  4059. for (const key in _message) {
  4060. if (hasOwn(_message, key)) {
  4061. handleFlatJson(_message[key]);
  4062. }
  4063. }
  4064. message = _message[locale];
  4065. }
  4066. _messages.value[locale] = message;
  4067. _context.messages = _messages.value;
  4068. }
  4069. // mergeLocaleMessage
  4070. function mergeLocaleMessage(locale, message) {
  4071. _messages.value[locale] = _messages.value[locale] || {};
  4072. const _message = { [locale]: message };
  4073. for (const key in _message) {
  4074. if (hasOwn(_message, key)) {
  4075. handleFlatJson(_message[key]);
  4076. }
  4077. }
  4078. message = _message[locale];
  4079. deepCopy(message, _messages.value[locale]);
  4080. _context.messages = _messages.value;
  4081. }
  4082. // getDateTimeFormat
  4083. function getDateTimeFormat(locale) {
  4084. return _datetimeFormats.value[locale] || {};
  4085. }
  4086. // setDateTimeFormat
  4087. function setDateTimeFormat(locale, format) {
  4088. _datetimeFormats.value[locale] = format;
  4089. _context.datetimeFormats = _datetimeFormats.value;
  4090. clearDateTimeFormat(_context, locale, format);
  4091. }
  4092. // mergeDateTimeFormat
  4093. function mergeDateTimeFormat(locale, format) {
  4094. _datetimeFormats.value[locale] = assign(_datetimeFormats.value[locale] || {}, format);
  4095. _context.datetimeFormats = _datetimeFormats.value;
  4096. clearDateTimeFormat(_context, locale, format);
  4097. }
  4098. // getNumberFormat
  4099. function getNumberFormat(locale) {
  4100. return _numberFormats.value[locale] || {};
  4101. }
  4102. // setNumberFormat
  4103. function setNumberFormat(locale, format) {
  4104. _numberFormats.value[locale] = format;
  4105. _context.numberFormats = _numberFormats.value;
  4106. clearNumberFormat(_context, locale, format);
  4107. }
  4108. // mergeNumberFormat
  4109. function mergeNumberFormat(locale, format) {
  4110. _numberFormats.value[locale] = assign(_numberFormats.value[locale] || {}, format);
  4111. _context.numberFormats = _numberFormats.value;
  4112. clearNumberFormat(_context, locale, format);
  4113. }
  4114. // for debug
  4115. composerID++;
  4116. // watch root locale & fallbackLocale
  4117. if (__root && inBrowser) {
  4118. vue.watch(__root.locale, (val) => {
  4119. if (_inheritLocale) {
  4120. _locale.value = val;
  4121. _context.locale = val;
  4122. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  4123. }
  4124. });
  4125. vue.watch(__root.fallbackLocale, (val) => {
  4126. if (_inheritLocale) {
  4127. _fallbackLocale.value = val;
  4128. _context.fallbackLocale = val;
  4129. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  4130. }
  4131. });
  4132. }
  4133. // define basic composition API!
  4134. const composer = {
  4135. id: composerID,
  4136. locale,
  4137. fallbackLocale,
  4138. get inheritLocale() {
  4139. return _inheritLocale;
  4140. },
  4141. set inheritLocale(val) {
  4142. _inheritLocale = val;
  4143. if (val && __root) {
  4144. _locale.value = __root.locale.value;
  4145. _fallbackLocale.value = __root.fallbackLocale.value;
  4146. updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
  4147. }
  4148. },
  4149. get availableLocales() {
  4150. return Object.keys(_messages.value).sort();
  4151. },
  4152. messages,
  4153. get modifiers() {
  4154. return _modifiers;
  4155. },
  4156. get pluralRules() {
  4157. return _pluralRules || {};
  4158. },
  4159. get isGlobal() {
  4160. return _isGlobal;
  4161. },
  4162. get missingWarn() {
  4163. return _missingWarn;
  4164. },
  4165. set missingWarn(val) {
  4166. _missingWarn = val;
  4167. _context.missingWarn = _missingWarn;
  4168. },
  4169. get fallbackWarn() {
  4170. return _fallbackWarn;
  4171. },
  4172. set fallbackWarn(val) {
  4173. _fallbackWarn = val;
  4174. _context.fallbackWarn = _fallbackWarn;
  4175. },
  4176. get fallbackRoot() {
  4177. return _fallbackRoot;
  4178. },
  4179. set fallbackRoot(val) {
  4180. _fallbackRoot = val;
  4181. },
  4182. get fallbackFormat() {
  4183. return _fallbackFormat;
  4184. },
  4185. set fallbackFormat(val) {
  4186. _fallbackFormat = val;
  4187. _context.fallbackFormat = _fallbackFormat;
  4188. },
  4189. get warnHtmlMessage() {
  4190. return _warnHtmlMessage;
  4191. },
  4192. set warnHtmlMessage(val) {
  4193. _warnHtmlMessage = val;
  4194. _context.warnHtmlMessage = val;
  4195. },
  4196. get escapeParameter() {
  4197. return _escapeParameter;
  4198. },
  4199. set escapeParameter(val) {
  4200. _escapeParameter = val;
  4201. _context.escapeParameter = val;
  4202. },
  4203. t,
  4204. getLocaleMessage,
  4205. setLocaleMessage,
  4206. mergeLocaleMessage,
  4207. getPostTranslationHandler,
  4208. setPostTranslationHandler,
  4209. getMissingHandler,
  4210. setMissingHandler,
  4211. [SetPluralRulesSymbol]: setPluralRules
  4212. };
  4213. {
  4214. composer.datetimeFormats = datetimeFormats;
  4215. composer.numberFormats = numberFormats;
  4216. composer.rt = rt;
  4217. composer.te = te;
  4218. composer.tm = tm;
  4219. composer.d = d;
  4220. composer.n = n;
  4221. composer.getDateTimeFormat = getDateTimeFormat;
  4222. composer.setDateTimeFormat = setDateTimeFormat;
  4223. composer.mergeDateTimeFormat = mergeDateTimeFormat;
  4224. composer.getNumberFormat = getNumberFormat;
  4225. composer.setNumberFormat = setNumberFormat;
  4226. composer.mergeNumberFormat = mergeNumberFormat;
  4227. composer[InejctWithOptionSymbol] = __injectWithOption;
  4228. composer[TranslateVNodeSymbol] = translateVNode;
  4229. composer[DatetimePartsSymbol] = datetimeParts;
  4230. composer[NumberPartsSymbol] = numberParts;
  4231. }
  4232. // for vue-devtools timeline event
  4233. {
  4234. composer[EnableEmitter] = (emitter) => {
  4235. _context.__v_emitter = emitter;
  4236. };
  4237. composer[DisableEmitter] = () => {
  4238. _context.__v_emitter = undefined;
  4239. };
  4240. }
  4241. return composer;
  4242. }
  4243. /* eslint-enable @typescript-eslint/no-explicit-any */
  4244. /* eslint-disable @typescript-eslint/no-explicit-any */
  4245. /**
  4246. * Convert to I18n Composer Options from VueI18n Options
  4247. *
  4248. * @internal
  4249. */
  4250. function convertComposerOptions(options) {
  4251. const locale = isString(options.locale) ? options.locale : DEFAULT_LOCALE;
  4252. const fallbackLocale = isString(options.fallbackLocale) ||
  4253. isArray(options.fallbackLocale) ||
  4254. isPlainObject(options.fallbackLocale) ||
  4255. options.fallbackLocale === false
  4256. ? options.fallbackLocale
  4257. : locale;
  4258. const missing = isFunction(options.missing) ? options.missing : undefined;
  4259. const missingWarn = isBoolean(options.silentTranslationWarn) ||
  4260. isRegExp(options.silentTranslationWarn)
  4261. ? !options.silentTranslationWarn
  4262. : true;
  4263. const fallbackWarn = isBoolean(options.silentFallbackWarn) ||
  4264. isRegExp(options.silentFallbackWarn)
  4265. ? !options.silentFallbackWarn
  4266. : true;
  4267. const fallbackRoot = isBoolean(options.fallbackRoot)
  4268. ? options.fallbackRoot
  4269. : true;
  4270. const fallbackFormat = !!options.formatFallbackMessages;
  4271. const modifiers = isPlainObject(options.modifiers) ? options.modifiers : {};
  4272. const pluralizationRules = options.pluralizationRules;
  4273. const postTranslation = isFunction(options.postTranslation)
  4274. ? options.postTranslation
  4275. : undefined;
  4276. const warnHtmlMessage = isString(options.warnHtmlInMessage)
  4277. ? options.warnHtmlInMessage !== 'off'
  4278. : true;
  4279. const escapeParameter = !!options.escapeParameterHtml;
  4280. const inheritLocale = isBoolean(options.sync) ? options.sync : true;
  4281. if (options.formatter) {
  4282. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
  4283. }
  4284. if (options.preserveDirectiveContent) {
  4285. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
  4286. }
  4287. let messages = options.messages;
  4288. if (isPlainObject(options.sharedMessages)) {
  4289. const sharedMessages = options.sharedMessages;
  4290. const locales = Object.keys(sharedMessages);
  4291. messages = locales.reduce((messages, locale) => {
  4292. const message = messages[locale] || (messages[locale] = {});
  4293. assign(message, sharedMessages[locale]);
  4294. return messages;
  4295. }, (messages || {}));
  4296. }
  4297. const { __i18n, __root, __injectWithOption } = options;
  4298. const datetimeFormats = options.datetimeFormats;
  4299. const numberFormats = options.numberFormats;
  4300. const flatJson = options.flatJson;
  4301. return {
  4302. locale,
  4303. fallbackLocale,
  4304. messages,
  4305. flatJson,
  4306. datetimeFormats,
  4307. numberFormats,
  4308. missing,
  4309. missingWarn,
  4310. fallbackWarn,
  4311. fallbackRoot,
  4312. fallbackFormat,
  4313. modifiers,
  4314. pluralRules: pluralizationRules,
  4315. postTranslation,
  4316. warnHtmlMessage,
  4317. escapeParameter,
  4318. messageResolver: options.messageResolver,
  4319. inheritLocale,
  4320. __i18n,
  4321. __root,
  4322. __injectWithOption
  4323. };
  4324. }
  4325. /**
  4326. * create VueI18n interface factory
  4327. *
  4328. * @internal
  4329. */
  4330. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  4331. function createVueI18n(options = {}, VueI18nLegacy) {
  4332. {
  4333. const composer = createComposer(convertComposerOptions(options));
  4334. const { __extender } = options;
  4335. // defines VueI18n
  4336. const vueI18n = {
  4337. // id
  4338. id: composer.id,
  4339. // locale
  4340. get locale() {
  4341. return composer.locale.value;
  4342. },
  4343. set locale(val) {
  4344. composer.locale.value = val;
  4345. },
  4346. // fallbackLocale
  4347. get fallbackLocale() {
  4348. return composer.fallbackLocale.value;
  4349. },
  4350. set fallbackLocale(val) {
  4351. composer.fallbackLocale.value = val;
  4352. },
  4353. // messages
  4354. get messages() {
  4355. return composer.messages.value;
  4356. },
  4357. // datetimeFormats
  4358. get datetimeFormats() {
  4359. return composer.datetimeFormats.value;
  4360. },
  4361. // numberFormats
  4362. get numberFormats() {
  4363. return composer.numberFormats.value;
  4364. },
  4365. // availableLocales
  4366. get availableLocales() {
  4367. return composer.availableLocales;
  4368. },
  4369. // formatter
  4370. get formatter() {
  4371. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
  4372. // dummy
  4373. return {
  4374. interpolate() {
  4375. return [];
  4376. }
  4377. };
  4378. },
  4379. set formatter(val) {
  4380. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
  4381. },
  4382. // missing
  4383. get missing() {
  4384. return composer.getMissingHandler();
  4385. },
  4386. set missing(handler) {
  4387. composer.setMissingHandler(handler);
  4388. },
  4389. // silentTranslationWarn
  4390. get silentTranslationWarn() {
  4391. return isBoolean(composer.missingWarn)
  4392. ? !composer.missingWarn
  4393. : composer.missingWarn;
  4394. },
  4395. set silentTranslationWarn(val) {
  4396. composer.missingWarn = isBoolean(val) ? !val : val;
  4397. },
  4398. // silentFallbackWarn
  4399. get silentFallbackWarn() {
  4400. return isBoolean(composer.fallbackWarn)
  4401. ? !composer.fallbackWarn
  4402. : composer.fallbackWarn;
  4403. },
  4404. set silentFallbackWarn(val) {
  4405. composer.fallbackWarn = isBoolean(val) ? !val : val;
  4406. },
  4407. // modifiers
  4408. get modifiers() {
  4409. return composer.modifiers;
  4410. },
  4411. // formatFallbackMessages
  4412. get formatFallbackMessages() {
  4413. return composer.fallbackFormat;
  4414. },
  4415. set formatFallbackMessages(val) {
  4416. composer.fallbackFormat = val;
  4417. },
  4418. // postTranslation
  4419. get postTranslation() {
  4420. return composer.getPostTranslationHandler();
  4421. },
  4422. set postTranslation(handler) {
  4423. composer.setPostTranslationHandler(handler);
  4424. },
  4425. // sync
  4426. get sync() {
  4427. return composer.inheritLocale;
  4428. },
  4429. set sync(val) {
  4430. composer.inheritLocale = val;
  4431. },
  4432. // warnInHtmlMessage
  4433. get warnHtmlInMessage() {
  4434. return composer.warnHtmlMessage ? 'warn' : 'off';
  4435. },
  4436. set warnHtmlInMessage(val) {
  4437. composer.warnHtmlMessage = val !== 'off';
  4438. },
  4439. // escapeParameterHtml
  4440. get escapeParameterHtml() {
  4441. return composer.escapeParameter;
  4442. },
  4443. set escapeParameterHtml(val) {
  4444. composer.escapeParameter = val;
  4445. },
  4446. // preserveDirectiveContent
  4447. get preserveDirectiveContent() {
  4448. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
  4449. return true;
  4450. },
  4451. set preserveDirectiveContent(val) {
  4452. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
  4453. },
  4454. // pluralizationRules
  4455. get pluralizationRules() {
  4456. return composer.pluralRules || {};
  4457. },
  4458. // for internal
  4459. __composer: composer,
  4460. // t
  4461. t(...args) {
  4462. const [arg1, arg2, arg3] = args;
  4463. const options = {};
  4464. let list = null;
  4465. let named = null;
  4466. if (!isString(arg1)) {
  4467. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  4468. }
  4469. const key = arg1;
  4470. if (isString(arg2)) {
  4471. options.locale = arg2;
  4472. }
  4473. else if (isArray(arg2)) {
  4474. list = arg2;
  4475. }
  4476. else if (isPlainObject(arg2)) {
  4477. named = arg2;
  4478. }
  4479. if (isArray(arg3)) {
  4480. list = arg3;
  4481. }
  4482. else if (isPlainObject(arg3)) {
  4483. named = arg3;
  4484. }
  4485. // return composer.t(key, (list || named || {}) as any, options)
  4486. return Reflect.apply(composer.t, composer, [
  4487. key,
  4488. (list || named || {}),
  4489. options
  4490. ]);
  4491. },
  4492. rt(...args) {
  4493. return Reflect.apply(composer.rt, composer, [...args]);
  4494. },
  4495. // tc
  4496. tc(...args) {
  4497. const [arg1, arg2, arg3] = args;
  4498. const options = { plural: 1 };
  4499. let list = null;
  4500. let named = null;
  4501. if (!isString(arg1)) {
  4502. throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
  4503. }
  4504. const key = arg1;
  4505. if (isString(arg2)) {
  4506. options.locale = arg2;
  4507. }
  4508. else if (isNumber(arg2)) {
  4509. options.plural = arg2;
  4510. }
  4511. else if (isArray(arg2)) {
  4512. list = arg2;
  4513. }
  4514. else if (isPlainObject(arg2)) {
  4515. named = arg2;
  4516. }
  4517. if (isString(arg3)) {
  4518. options.locale = arg3;
  4519. }
  4520. else if (isArray(arg3)) {
  4521. list = arg3;
  4522. }
  4523. else if (isPlainObject(arg3)) {
  4524. named = arg3;
  4525. }
  4526. // return composer.t(key, (list || named || {}) as any, options)
  4527. return Reflect.apply(composer.t, composer, [
  4528. key,
  4529. (list || named || {}),
  4530. options
  4531. ]);
  4532. },
  4533. // te
  4534. te(key, locale) {
  4535. return composer.te(key, locale);
  4536. },
  4537. // tm
  4538. tm(key) {
  4539. return composer.tm(key);
  4540. },
  4541. // getLocaleMessage
  4542. getLocaleMessage(locale) {
  4543. return composer.getLocaleMessage(locale);
  4544. },
  4545. // setLocaleMessage
  4546. setLocaleMessage(locale, message) {
  4547. composer.setLocaleMessage(locale, message);
  4548. },
  4549. // mergeLocaleMessage
  4550. mergeLocaleMessage(locale, message) {
  4551. composer.mergeLocaleMessage(locale, message);
  4552. },
  4553. // d
  4554. d(...args) {
  4555. return Reflect.apply(composer.d, composer, [...args]);
  4556. },
  4557. // getDateTimeFormat
  4558. getDateTimeFormat(locale) {
  4559. return composer.getDateTimeFormat(locale);
  4560. },
  4561. // setDateTimeFormat
  4562. setDateTimeFormat(locale, format) {
  4563. composer.setDateTimeFormat(locale, format);
  4564. },
  4565. // mergeDateTimeFormat
  4566. mergeDateTimeFormat(locale, format) {
  4567. composer.mergeDateTimeFormat(locale, format);
  4568. },
  4569. // n
  4570. n(...args) {
  4571. return Reflect.apply(composer.n, composer, [...args]);
  4572. },
  4573. // getNumberFormat
  4574. getNumberFormat(locale) {
  4575. return composer.getNumberFormat(locale);
  4576. },
  4577. // setNumberFormat
  4578. setNumberFormat(locale, format) {
  4579. composer.setNumberFormat(locale, format);
  4580. },
  4581. // mergeNumberFormat
  4582. mergeNumberFormat(locale, format) {
  4583. composer.mergeNumberFormat(locale, format);
  4584. },
  4585. // getChoiceIndex
  4586. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  4587. getChoiceIndex(choice, choicesLength) {
  4588. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX));
  4589. return -1;
  4590. }
  4591. };
  4592. vueI18n.__extender = __extender;
  4593. // for vue-devtools timeline event
  4594. {
  4595. vueI18n.__enableEmitter = (emitter) => {
  4596. const __composer = composer;
  4597. __composer[EnableEmitter] && __composer[EnableEmitter](emitter);
  4598. };
  4599. vueI18n.__disableEmitter = () => {
  4600. const __composer = composer;
  4601. __composer[DisableEmitter] && __composer[DisableEmitter]();
  4602. };
  4603. }
  4604. return vueI18n;
  4605. }
  4606. }
  4607. /* eslint-enable @typescript-eslint/no-explicit-any */
  4608. const baseFormatProps = {
  4609. tag: {
  4610. type: [String, Object]
  4611. },
  4612. locale: {
  4613. type: String
  4614. },
  4615. scope: {
  4616. type: String,
  4617. // NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
  4618. validator: (val /* ComponentI18nScope */) => val === 'parent' || val === 'global',
  4619. default: 'parent' /* ComponentI18nScope */
  4620. },
  4621. i18n: {
  4622. type: Object
  4623. }
  4624. };
  4625. function getInterpolateArg(
  4626. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4627. { slots }, // SetupContext,
  4628. keys) {
  4629. if (keys.length === 1 && keys[0] === 'default') {
  4630. // default slot with list
  4631. const ret = slots.default ? slots.default() : [];
  4632. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4633. return ret.reduce((slot, current) => {
  4634. return [
  4635. ...slot,
  4636. // prettier-ignore
  4637. ...(current.type === vue.Fragment ? current.children : [current]
  4638. )
  4639. ];
  4640. }, []);
  4641. }
  4642. else {
  4643. // named slots
  4644. return keys.reduce((arg, key) => {
  4645. const slot = slots[key];
  4646. if (slot) {
  4647. arg[key] = slot();
  4648. }
  4649. return arg;
  4650. }, {});
  4651. }
  4652. }
  4653. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4654. function getFragmentableTag(tag) {
  4655. return vue.Fragment ;
  4656. }
  4657. const TranslationImpl = /*#__PURE__*/ vue.defineComponent({
  4658. /* eslint-disable */
  4659. name: 'i18n-t',
  4660. props: assign({
  4661. keypath: {
  4662. type: String,
  4663. required: true
  4664. },
  4665. plural: {
  4666. type: [Number, String],
  4667. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4668. validator: (val) => isNumber(val) || !isNaN(val)
  4669. }
  4670. }, baseFormatProps),
  4671. /* eslint-enable */
  4672. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4673. setup(props, context) {
  4674. const { slots, attrs } = context;
  4675. // NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
  4676. const i18n = props.i18n ||
  4677. useI18n({
  4678. useScope: props.scope,
  4679. __useComponent: true
  4680. });
  4681. return () => {
  4682. const keys = Object.keys(slots).filter(key => key !== '_');
  4683. const options = {};
  4684. if (props.locale) {
  4685. options.locale = props.locale;
  4686. }
  4687. if (props.plural !== undefined) {
  4688. options.plural = isString(props.plural) ? +props.plural : props.plural;
  4689. }
  4690. const arg = getInterpolateArg(context, keys);
  4691. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4692. const children = i18n[TranslateVNodeSymbol](props.keypath, arg, options);
  4693. const assignedAttrs = assign({}, attrs);
  4694. const tag = isString(props.tag) || isObject(props.tag)
  4695. ? props.tag
  4696. : getFragmentableTag();
  4697. return vue.h(tag, assignedAttrs, children);
  4698. };
  4699. }
  4700. });
  4701. /**
  4702. * export the public type for h/tsx inference
  4703. * also to avoid inline import() in generated d.ts files
  4704. */
  4705. /**
  4706. * Translation Component
  4707. *
  4708. * @remarks
  4709. * See the following items for property about details
  4710. *
  4711. * @VueI18nSee [TranslationProps](component#translationprops)
  4712. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  4713. * @VueI18nSee [Component Interpolation](../guide/advanced/component)
  4714. *
  4715. * @example
  4716. * ```html
  4717. * <div id="app">
  4718. * <!-- ... -->
  4719. * <i18n keypath="term" tag="label" for="tos">
  4720. * <a :href="url" target="_blank">{{ $t('tos') }}</a>
  4721. * </i18n>
  4722. * <!-- ... -->
  4723. * </div>
  4724. * ```
  4725. * ```js
  4726. * import { createApp } from 'vue'
  4727. * import { createI18n } from 'vue-i18n'
  4728. *
  4729. * const messages = {
  4730. * en: {
  4731. * tos: 'Term of Service',
  4732. * term: 'I accept xxx {0}.'
  4733. * },
  4734. * ja: {
  4735. * tos: '利用規約',
  4736. * term: '私は xxx の{0}に同意します。'
  4737. * }
  4738. * }
  4739. *
  4740. * const i18n = createI18n({
  4741. * locale: 'en',
  4742. * messages
  4743. * })
  4744. *
  4745. * const app = createApp({
  4746. * data: {
  4747. * url: '/term'
  4748. * }
  4749. * }).use(i18n).mount('#app')
  4750. * ```
  4751. *
  4752. * @VueI18nComponent
  4753. */
  4754. const Translation = TranslationImpl;
  4755. const I18nT = Translation;
  4756. function isVNode(target) {
  4757. return isArray(target) && !isString(target[0]);
  4758. }
  4759. function renderFormatter(props, context, slotKeys, partFormatter) {
  4760. const { slots, attrs } = context;
  4761. return () => {
  4762. const options = { part: true };
  4763. let overrides = {};
  4764. if (props.locale) {
  4765. options.locale = props.locale;
  4766. }
  4767. if (isString(props.format)) {
  4768. options.key = props.format;
  4769. }
  4770. else if (isObject(props.format)) {
  4771. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4772. if (isString(props.format.key)) {
  4773. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4774. options.key = props.format.key;
  4775. }
  4776. // Filter out number format options only
  4777. overrides = Object.keys(props.format).reduce((options, prop) => {
  4778. return slotKeys.includes(prop)
  4779. ? assign({}, options, { [prop]: props.format[prop] }) // eslint-disable-line @typescript-eslint/no-explicit-any
  4780. : options;
  4781. }, {});
  4782. }
  4783. const parts = partFormatter(...[props.value, options, overrides]);
  4784. let children = [options.key];
  4785. if (isArray(parts)) {
  4786. children = parts.map((part, index) => {
  4787. const slot = slots[part.type];
  4788. const node = slot
  4789. ? slot({ [part.type]: part.value, index, parts })
  4790. : [part.value];
  4791. if (isVNode(node)) {
  4792. node[0].key = `${part.type}-${index}`;
  4793. }
  4794. return node;
  4795. });
  4796. }
  4797. else if (isString(parts)) {
  4798. children = [parts];
  4799. }
  4800. const assignedAttrs = assign({}, attrs);
  4801. const tag = isString(props.tag) || isObject(props.tag)
  4802. ? props.tag
  4803. : getFragmentableTag();
  4804. return vue.h(tag, assignedAttrs, children);
  4805. };
  4806. }
  4807. const NumberFormatImpl = /*#__PURE__*/ vue.defineComponent({
  4808. /* eslint-disable */
  4809. name: 'i18n-n',
  4810. props: assign({
  4811. value: {
  4812. type: Number,
  4813. required: true
  4814. },
  4815. format: {
  4816. type: [String, Object]
  4817. }
  4818. }, baseFormatProps),
  4819. /* eslint-enable */
  4820. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4821. setup(props, context) {
  4822. const i18n = props.i18n ||
  4823. useI18n({
  4824. useScope: 'parent',
  4825. __useComponent: true
  4826. });
  4827. return renderFormatter(props, context, NUMBER_FORMAT_OPTIONS_KEYS, (...args) =>
  4828. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4829. i18n[NumberPartsSymbol](...args));
  4830. }
  4831. });
  4832. /**
  4833. * export the public type for h/tsx inference
  4834. * also to avoid inline import() in generated d.ts files
  4835. */
  4836. /**
  4837. * Number Format Component
  4838. *
  4839. * @remarks
  4840. * See the following items for property about details
  4841. *
  4842. * @VueI18nSee [FormattableProps](component#formattableprops)
  4843. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  4844. * @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
  4845. *
  4846. * @VueI18nDanger
  4847. * Not supported IE, due to no support `Intl.NumberFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts)
  4848. *
  4849. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
  4850. *
  4851. * @VueI18nComponent
  4852. */
  4853. const NumberFormat = NumberFormatImpl;
  4854. const I18nN = NumberFormat;
  4855. const DatetimeFormatImpl = /* #__PURE__*/ vue.defineComponent({
  4856. /* eslint-disable */
  4857. name: 'i18n-d',
  4858. props: assign({
  4859. value: {
  4860. type: [Number, Date],
  4861. required: true
  4862. },
  4863. format: {
  4864. type: [String, Object]
  4865. }
  4866. }, baseFormatProps),
  4867. /* eslint-enable */
  4868. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4869. setup(props, context) {
  4870. const i18n = props.i18n ||
  4871. useI18n({
  4872. useScope: 'parent',
  4873. __useComponent: true
  4874. });
  4875. return renderFormatter(props, context, DATETIME_FORMAT_OPTIONS_KEYS, (...args) =>
  4876. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  4877. i18n[DatetimePartsSymbol](...args));
  4878. }
  4879. });
  4880. /**
  4881. * Datetime Format Component
  4882. *
  4883. * @remarks
  4884. * See the following items for property about details
  4885. *
  4886. * @VueI18nSee [FormattableProps](component#formattableprops)
  4887. * @VueI18nSee [BaseFormatProps](component#baseformatprops)
  4888. * @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
  4889. *
  4890. * @VueI18nDanger
  4891. * Not supported IE, due to no support `Intl.DateTimeFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts)
  4892. *
  4893. * If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
  4894. *
  4895. * @VueI18nComponent
  4896. */
  4897. const DatetimeFormat = DatetimeFormatImpl;
  4898. const I18nD = DatetimeFormat;
  4899. function getComposer$2(i18n, instance) {
  4900. const i18nInternal = i18n;
  4901. if (i18n.mode === 'composition') {
  4902. return (i18nInternal.__getInstance(instance) || i18n.global);
  4903. }
  4904. else {
  4905. const vueI18n = i18nInternal.__getInstance(instance);
  4906. return vueI18n != null
  4907. ? vueI18n.__composer
  4908. : i18n.global.__composer;
  4909. }
  4910. }
  4911. function vTDirective(i18n) {
  4912. const _process = (binding) => {
  4913. const { instance, modifiers, value } = binding;
  4914. /* istanbul ignore if */
  4915. if (!instance || !instance.$) {
  4916. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  4917. }
  4918. const composer = getComposer$2(i18n, instance.$);
  4919. if (modifiers.preserve) {
  4920. warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE));
  4921. }
  4922. const parsedValue = parseValue(value);
  4923. return [
  4924. Reflect.apply(composer.t, composer, [...makeParams(parsedValue)]),
  4925. composer
  4926. ];
  4927. };
  4928. const register = (el, binding) => {
  4929. const [textContent, composer] = _process(binding);
  4930. if (inBrowser && i18n.global === composer) {
  4931. // global scope only
  4932. el.__i18nWatcher = vue.watch(composer.locale, () => {
  4933. binding.instance && binding.instance.$forceUpdate();
  4934. });
  4935. }
  4936. el.__composer = composer;
  4937. el.textContent = textContent;
  4938. };
  4939. const unregister = (el) => {
  4940. if (inBrowser && el.__i18nWatcher) {
  4941. el.__i18nWatcher();
  4942. el.__i18nWatcher = undefined;
  4943. delete el.__i18nWatcher;
  4944. }
  4945. if (el.__composer) {
  4946. el.__composer = undefined;
  4947. delete el.__composer;
  4948. }
  4949. };
  4950. const update = (el, { value }) => {
  4951. if (el.__composer) {
  4952. const composer = el.__composer;
  4953. const parsedValue = parseValue(value);
  4954. el.textContent = Reflect.apply(composer.t, composer, [
  4955. ...makeParams(parsedValue)
  4956. ]);
  4957. }
  4958. };
  4959. const getSSRProps = (binding) => {
  4960. const [textContent] = _process(binding);
  4961. return { textContent };
  4962. };
  4963. return {
  4964. created: register,
  4965. unmounted: unregister,
  4966. beforeUpdate: update,
  4967. getSSRProps
  4968. };
  4969. }
  4970. function parseValue(value) {
  4971. if (isString(value)) {
  4972. return { path: value };
  4973. }
  4974. else if (isPlainObject(value)) {
  4975. if (!('path' in value)) {
  4976. throw createI18nError(I18nErrorCodes.REQUIRED_VALUE, 'path');
  4977. }
  4978. return value;
  4979. }
  4980. else {
  4981. throw createI18nError(I18nErrorCodes.INVALID_VALUE);
  4982. }
  4983. }
  4984. function makeParams(value) {
  4985. const { path, locale, args, choice, plural } = value;
  4986. const options = {};
  4987. const named = args || {};
  4988. if (isString(locale)) {
  4989. options.locale = locale;
  4990. }
  4991. if (isNumber(choice)) {
  4992. options.plural = choice;
  4993. }
  4994. if (isNumber(plural)) {
  4995. options.plural = plural;
  4996. }
  4997. return [path, named, options];
  4998. }
  4999. function apply(app, i18n, ...options) {
  5000. const pluginOptions = isPlainObject(options[0])
  5001. ? options[0]
  5002. : {};
  5003. const useI18nComponentName = !!pluginOptions.useI18nComponentName;
  5004. const globalInstall = isBoolean(pluginOptions.globalInstall)
  5005. ? pluginOptions.globalInstall
  5006. : true;
  5007. if (globalInstall && useI18nComponentName) {
  5008. warn(getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, {
  5009. name: Translation.name
  5010. }));
  5011. }
  5012. if (globalInstall) {
  5013. [!useI18nComponentName ? Translation.name : 'i18n', 'I18nT'].forEach(name => app.component(name, Translation));
  5014. [NumberFormat.name, 'I18nN'].forEach(name => app.component(name, NumberFormat));
  5015. [DatetimeFormat.name, 'I18nD'].forEach(name => app.component(name, DatetimeFormat));
  5016. }
  5017. // install directive
  5018. {
  5019. app.directive('t', vTDirective(i18n));
  5020. }
  5021. }
  5022. var global$1 = (typeof global !== "undefined" ? global :
  5023. typeof self !== "undefined" ? self :
  5024. typeof window !== "undefined" ? window : {});
  5025. function getDevtoolsGlobalHook() {
  5026. return getTarget().__VUE_DEVTOOLS_GLOBAL_HOOK__;
  5027. }
  5028. function getTarget() {
  5029. // @ts-ignore
  5030. return (typeof navigator !== 'undefined' && typeof window !== 'undefined')
  5031. ? window
  5032. : typeof global$1 !== 'undefined'
  5033. ? global$1
  5034. : {};
  5035. }
  5036. const isProxyAvailable = typeof Proxy === 'function';
  5037. const HOOK_SETUP = 'devtools-plugin:setup';
  5038. const HOOK_PLUGIN_SETTINGS_SET = 'plugin:settings:set';
  5039. let supported;
  5040. let perf;
  5041. function isPerformanceSupported() {
  5042. var _a;
  5043. if (supported !== undefined) {
  5044. return supported;
  5045. }
  5046. if (typeof window !== 'undefined' && window.performance) {
  5047. supported = true;
  5048. perf = window.performance;
  5049. }
  5050. else if (typeof global$1 !== 'undefined' && ((_a = global$1.perf_hooks) === null || _a === void 0 ? void 0 : _a.performance)) {
  5051. supported = true;
  5052. perf = global$1.perf_hooks.performance;
  5053. }
  5054. else {
  5055. supported = false;
  5056. }
  5057. return supported;
  5058. }
  5059. function now() {
  5060. return isPerformanceSupported() ? perf.now() : Date.now();
  5061. }
  5062. class ApiProxy {
  5063. constructor(plugin, hook) {
  5064. this.target = null;
  5065. this.targetQueue = [];
  5066. this.onQueue = [];
  5067. this.plugin = plugin;
  5068. this.hook = hook;
  5069. const defaultSettings = {};
  5070. if (plugin.settings) {
  5071. for (const id in plugin.settings) {
  5072. const item = plugin.settings[id];
  5073. defaultSettings[id] = item.defaultValue;
  5074. }
  5075. }
  5076. const localSettingsSaveId = `__vue-devtools-plugin-settings__${plugin.id}`;
  5077. let currentSettings = Object.assign({}, defaultSettings);
  5078. try {
  5079. const raw = localStorage.getItem(localSettingsSaveId);
  5080. const data = JSON.parse(raw);
  5081. Object.assign(currentSettings, data);
  5082. }
  5083. catch (e) {
  5084. // noop
  5085. }
  5086. this.fallbacks = {
  5087. getSettings() {
  5088. return currentSettings;
  5089. },
  5090. setSettings(value) {
  5091. try {
  5092. localStorage.setItem(localSettingsSaveId, JSON.stringify(value));
  5093. }
  5094. catch (e) {
  5095. // noop
  5096. }
  5097. currentSettings = value;
  5098. },
  5099. now() {
  5100. return now();
  5101. },
  5102. };
  5103. if (hook) {
  5104. hook.on(HOOK_PLUGIN_SETTINGS_SET, (pluginId, value) => {
  5105. if (pluginId === this.plugin.id) {
  5106. this.fallbacks.setSettings(value);
  5107. }
  5108. });
  5109. }
  5110. this.proxiedOn = new Proxy({}, {
  5111. get: (_target, prop) => {
  5112. if (this.target) {
  5113. return this.target.on[prop];
  5114. }
  5115. else {
  5116. return (...args) => {
  5117. this.onQueue.push({
  5118. method: prop,
  5119. args,
  5120. });
  5121. };
  5122. }
  5123. },
  5124. });
  5125. this.proxiedTarget = new Proxy({}, {
  5126. get: (_target, prop) => {
  5127. if (this.target) {
  5128. return this.target[prop];
  5129. }
  5130. else if (prop === 'on') {
  5131. return this.proxiedOn;
  5132. }
  5133. else if (Object.keys(this.fallbacks).includes(prop)) {
  5134. return (...args) => {
  5135. this.targetQueue.push({
  5136. method: prop,
  5137. args,
  5138. resolve: () => { },
  5139. });
  5140. return this.fallbacks[prop](...args);
  5141. };
  5142. }
  5143. else {
  5144. return (...args) => {
  5145. return new Promise(resolve => {
  5146. this.targetQueue.push({
  5147. method: prop,
  5148. args,
  5149. resolve,
  5150. });
  5151. });
  5152. };
  5153. }
  5154. },
  5155. });
  5156. }
  5157. async setRealTarget(target) {
  5158. this.target = target;
  5159. for (const item of this.onQueue) {
  5160. this.target.on[item.method](...item.args);
  5161. }
  5162. for (const item of this.targetQueue) {
  5163. item.resolve(await this.target[item.method](...item.args));
  5164. }
  5165. }
  5166. }
  5167. function setupDevtoolsPlugin(pluginDescriptor, setupFn) {
  5168. const descriptor = pluginDescriptor;
  5169. const target = getTarget();
  5170. const hook = getDevtoolsGlobalHook();
  5171. const enableProxy = isProxyAvailable && descriptor.enableEarlyProxy;
  5172. if (hook && (target.__VUE_DEVTOOLS_PLUGIN_API_AVAILABLE__ || !enableProxy)) {
  5173. hook.emit(HOOK_SETUP, pluginDescriptor, setupFn);
  5174. }
  5175. else {
  5176. const proxy = enableProxy ? new ApiProxy(descriptor, hook) : null;
  5177. const list = target.__VUE_DEVTOOLS_PLUGINS__ = target.__VUE_DEVTOOLS_PLUGINS__ || [];
  5178. list.push({
  5179. pluginDescriptor: descriptor,
  5180. setupFn,
  5181. proxy,
  5182. });
  5183. if (proxy)
  5184. setupFn(proxy.proxiedTarget);
  5185. }
  5186. }
  5187. const VueDevToolsLabels = {
  5188. ["vue-devtools-plugin-vue-i18n" /* VueDevToolsIDs.PLUGIN */]: 'Vue I18n devtools',
  5189. ["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */]: 'I18n Resources',
  5190. ["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */]: 'Vue I18n'
  5191. };
  5192. const VueDevToolsPlaceholders = {
  5193. ["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */]: 'Search for scopes ...'
  5194. };
  5195. const VueDevToolsTimelineColors = {
  5196. ["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */]: 0xffcd19
  5197. };
  5198. const VUE_I18N_COMPONENT_TYPES = 'vue-i18n: composer properties';
  5199. let devtoolsApi;
  5200. async function enableDevTools(app, i18n) {
  5201. return new Promise((resolve, reject) => {
  5202. try {
  5203. setupDevtoolsPlugin({
  5204. id: "vue-devtools-plugin-vue-i18n" /* VueDevToolsIDs.PLUGIN */,
  5205. label: VueDevToolsLabels["vue-devtools-plugin-vue-i18n" /* VueDevToolsIDs.PLUGIN */],
  5206. packageName: 'vue-i18n',
  5207. homepage: 'https://vue-i18n.intlify.dev',
  5208. logo: 'https://vue-i18n.intlify.dev/vue-i18n-devtools-logo.png',
  5209. componentStateTypes: [VUE_I18N_COMPONENT_TYPES],
  5210. app: app // eslint-disable-line @typescript-eslint/no-explicit-any
  5211. }, api => {
  5212. devtoolsApi = api;
  5213. api.on.visitComponentTree(({ componentInstance, treeNode }) => {
  5214. updateComponentTreeTags(componentInstance, treeNode, i18n);
  5215. });
  5216. api.on.inspectComponent(({ componentInstance, instanceData }) => {
  5217. if (componentInstance.vnode.el &&
  5218. componentInstance.vnode.el.__VUE_I18N__ &&
  5219. instanceData) {
  5220. if (i18n.mode === 'legacy') {
  5221. // ignore global scope on legacy mode
  5222. if (componentInstance.vnode.el.__VUE_I18N__ !==
  5223. i18n.global.__composer) {
  5224. inspectComposer(instanceData, componentInstance.vnode.el.__VUE_I18N__);
  5225. }
  5226. }
  5227. else {
  5228. inspectComposer(instanceData, componentInstance.vnode.el.__VUE_I18N__);
  5229. }
  5230. }
  5231. });
  5232. api.addInspector({
  5233. id: "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */,
  5234. label: VueDevToolsLabels["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */],
  5235. icon: 'language',
  5236. treeFilterPlaceholder: VueDevToolsPlaceholders["vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */]
  5237. });
  5238. api.on.getInspectorTree(payload => {
  5239. if (payload.app === app &&
  5240. payload.inspectorId === "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */) {
  5241. registerScope(payload, i18n);
  5242. }
  5243. });
  5244. const roots = new Map();
  5245. api.on.getInspectorState(async (payload) => {
  5246. if (payload.app === app &&
  5247. payload.inspectorId === "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */) {
  5248. api.unhighlightElement();
  5249. inspectScope(payload, i18n);
  5250. if (payload.nodeId === 'global') {
  5251. if (!roots.has(payload.app)) {
  5252. const [root] = await api.getComponentInstances(payload.app);
  5253. roots.set(payload.app, root);
  5254. }
  5255. api.highlightElement(roots.get(payload.app));
  5256. }
  5257. else {
  5258. const instance = getComponentInstance(payload.nodeId, i18n);
  5259. instance && api.highlightElement(instance);
  5260. }
  5261. }
  5262. });
  5263. api.on.editInspectorState(payload => {
  5264. if (payload.app === app &&
  5265. payload.inspectorId === "vue-i18n-resource-inspector" /* VueDevToolsIDs.CUSTOM_INSPECTOR */) {
  5266. editScope(payload, i18n);
  5267. }
  5268. });
  5269. api.addTimelineLayer({
  5270. id: "vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */,
  5271. label: VueDevToolsLabels["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */],
  5272. color: VueDevToolsTimelineColors["vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */]
  5273. });
  5274. resolve(true);
  5275. });
  5276. }
  5277. catch (e) {
  5278. console.error(e);
  5279. reject(false);
  5280. }
  5281. });
  5282. }
  5283. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5284. function getI18nScopeLable(instance) {
  5285. return (instance.type.name ||
  5286. instance.type.displayName ||
  5287. instance.type.__file ||
  5288. 'Anonymous');
  5289. }
  5290. function updateComponentTreeTags(instance, // eslint-disable-line @typescript-eslint/no-explicit-any
  5291. treeNode, i18n) {
  5292. // prettier-ignore
  5293. const global = i18n.mode === 'composition'
  5294. ? i18n.global
  5295. : i18n.global.__composer;
  5296. if (instance && instance.vnode.el && instance.vnode.el.__VUE_I18N__) {
  5297. // add custom tags local scope only
  5298. if (instance.vnode.el.__VUE_I18N__ !== global) {
  5299. const tag = {
  5300. label: `i18n (${getI18nScopeLable(instance)} Scope)`,
  5301. textColor: 0x000000,
  5302. backgroundColor: 0xffcd19
  5303. };
  5304. treeNode.tags.push(tag);
  5305. }
  5306. }
  5307. }
  5308. function inspectComposer(instanceData, composer) {
  5309. const type = VUE_I18N_COMPONENT_TYPES;
  5310. instanceData.state.push({
  5311. type,
  5312. key: 'locale',
  5313. editable: true,
  5314. value: composer.locale.value
  5315. });
  5316. instanceData.state.push({
  5317. type,
  5318. key: 'availableLocales',
  5319. editable: false,
  5320. value: composer.availableLocales
  5321. });
  5322. instanceData.state.push({
  5323. type,
  5324. key: 'fallbackLocale',
  5325. editable: true,
  5326. value: composer.fallbackLocale.value
  5327. });
  5328. instanceData.state.push({
  5329. type,
  5330. key: 'inheritLocale',
  5331. editable: true,
  5332. value: composer.inheritLocale
  5333. });
  5334. instanceData.state.push({
  5335. type,
  5336. key: 'messages',
  5337. editable: false,
  5338. value: getLocaleMessageValue(composer.messages.value)
  5339. });
  5340. {
  5341. instanceData.state.push({
  5342. type,
  5343. key: 'datetimeFormats',
  5344. editable: false,
  5345. value: composer.datetimeFormats.value
  5346. });
  5347. instanceData.state.push({
  5348. type,
  5349. key: 'numberFormats',
  5350. editable: false,
  5351. value: composer.numberFormats.value
  5352. });
  5353. }
  5354. }
  5355. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5356. function getLocaleMessageValue(messages) {
  5357. const value = {};
  5358. Object.keys(messages).forEach((key) => {
  5359. const v = messages[key];
  5360. if (isFunction(v) && 'source' in v) {
  5361. value[key] = getMessageFunctionDetails(v);
  5362. }
  5363. else if (isMessageAST(v) && v.loc && v.loc.source) {
  5364. value[key] = v.loc.source;
  5365. }
  5366. else if (isObject(v)) {
  5367. value[key] = getLocaleMessageValue(v);
  5368. }
  5369. else {
  5370. value[key] = v;
  5371. }
  5372. });
  5373. return value;
  5374. }
  5375. const ESC = {
  5376. '<': '&lt;',
  5377. '>': '&gt;',
  5378. '"': '&quot;',
  5379. '&': '&amp;'
  5380. };
  5381. function escape(s) {
  5382. return s.replace(/[<>"&]/g, escapeChar);
  5383. }
  5384. function escapeChar(a) {
  5385. return ESC[a] || a;
  5386. }
  5387. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5388. function getMessageFunctionDetails(func) {
  5389. const argString = func.source ? `("${escape(func.source)}")` : `(?)`;
  5390. return {
  5391. _custom: {
  5392. type: 'function',
  5393. display: `<span>ƒ</span> ${argString}`
  5394. }
  5395. };
  5396. }
  5397. function registerScope(payload, i18n) {
  5398. payload.rootNodes.push({
  5399. id: 'global',
  5400. label: 'Global Scope'
  5401. });
  5402. // prettier-ignore
  5403. const global = i18n.mode === 'composition'
  5404. ? i18n.global
  5405. : i18n.global.__composer;
  5406. for (const [keyInstance, instance] of i18n.__instances) {
  5407. // prettier-ignore
  5408. const composer = i18n.mode === 'composition'
  5409. ? instance
  5410. : instance.__composer;
  5411. if (global === composer) {
  5412. continue;
  5413. }
  5414. payload.rootNodes.push({
  5415. id: composer.id.toString(),
  5416. label: `${getI18nScopeLable(keyInstance)} Scope`
  5417. });
  5418. }
  5419. }
  5420. function getComponentInstance(nodeId, i18n) {
  5421. let instance = null;
  5422. if (nodeId !== 'global') {
  5423. for (const [component, composer] of i18n.__instances.entries()) {
  5424. if (composer.id.toString() === nodeId) {
  5425. instance = component;
  5426. break;
  5427. }
  5428. }
  5429. }
  5430. return instance;
  5431. }
  5432. function getComposer$1(nodeId, i18n) {
  5433. if (nodeId === 'global') {
  5434. return i18n.mode === 'composition'
  5435. ? i18n.global
  5436. : i18n.global.__composer;
  5437. }
  5438. else {
  5439. const instance = Array.from(i18n.__instances.values()).find(item => item.id.toString() === nodeId);
  5440. if (instance) {
  5441. return i18n.mode === 'composition'
  5442. ? instance
  5443. : instance.__composer;
  5444. }
  5445. else {
  5446. return null;
  5447. }
  5448. }
  5449. }
  5450. function inspectScope(payload, i18n
  5451. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5452. ) {
  5453. const composer = getComposer$1(payload.nodeId, i18n);
  5454. if (composer) {
  5455. // TODO:
  5456. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5457. payload.state = makeScopeInspectState(composer);
  5458. }
  5459. return null;
  5460. }
  5461. function makeScopeInspectState(composer) {
  5462. const state = {};
  5463. const localeType = 'Locale related info';
  5464. const localeStates = [
  5465. {
  5466. type: localeType,
  5467. key: 'locale',
  5468. editable: true,
  5469. value: composer.locale.value
  5470. },
  5471. {
  5472. type: localeType,
  5473. key: 'fallbackLocale',
  5474. editable: true,
  5475. value: composer.fallbackLocale.value
  5476. },
  5477. {
  5478. type: localeType,
  5479. key: 'availableLocales',
  5480. editable: false,
  5481. value: composer.availableLocales
  5482. },
  5483. {
  5484. type: localeType,
  5485. key: 'inheritLocale',
  5486. editable: true,
  5487. value: composer.inheritLocale
  5488. }
  5489. ];
  5490. state[localeType] = localeStates;
  5491. const localeMessagesType = 'Locale messages info';
  5492. const localeMessagesStates = [
  5493. {
  5494. type: localeMessagesType,
  5495. key: 'messages',
  5496. editable: false,
  5497. value: getLocaleMessageValue(composer.messages.value)
  5498. }
  5499. ];
  5500. state[localeMessagesType] = localeMessagesStates;
  5501. {
  5502. const datetimeFormatsType = 'Datetime formats info';
  5503. const datetimeFormatsStates = [
  5504. {
  5505. type: datetimeFormatsType,
  5506. key: 'datetimeFormats',
  5507. editable: false,
  5508. value: composer.datetimeFormats.value
  5509. }
  5510. ];
  5511. state[datetimeFormatsType] = datetimeFormatsStates;
  5512. const numberFormatsType = 'Datetime formats info';
  5513. const numberFormatsStates = [
  5514. {
  5515. type: numberFormatsType,
  5516. key: 'numberFormats',
  5517. editable: false,
  5518. value: composer.numberFormats.value
  5519. }
  5520. ];
  5521. state[numberFormatsType] = numberFormatsStates;
  5522. }
  5523. return state;
  5524. }
  5525. function addTimelineEvent(event, payload) {
  5526. if (devtoolsApi) {
  5527. let groupId;
  5528. if (payload && 'groupId' in payload) {
  5529. groupId = payload.groupId;
  5530. delete payload.groupId;
  5531. }
  5532. devtoolsApi.addTimelineEvent({
  5533. layerId: "vue-i18n-timeline" /* VueDevToolsIDs.TIMELINE */,
  5534. event: {
  5535. title: event,
  5536. groupId,
  5537. time: Date.now(),
  5538. meta: {},
  5539. data: payload || {},
  5540. logType: event === "compile-error" /* VueDevToolsTimelineEvents.COMPILE_ERROR */
  5541. ? 'error'
  5542. : event === "fallback" /* VueDevToolsTimelineEvents.FALBACK */ ||
  5543. event === "missing" /* VueDevToolsTimelineEvents.MISSING */
  5544. ? 'warning'
  5545. : 'default'
  5546. }
  5547. });
  5548. }
  5549. }
  5550. function editScope(payload, i18n) {
  5551. const composer = getComposer$1(payload.nodeId, i18n);
  5552. if (composer) {
  5553. const [field] = payload.path;
  5554. if (field === 'locale' && isString(payload.state.value)) {
  5555. composer.locale.value = payload.state.value;
  5556. }
  5557. else if (field === 'fallbackLocale' &&
  5558. (isString(payload.state.value) ||
  5559. isArray(payload.state.value) ||
  5560. isObject(payload.state.value))) {
  5561. composer.fallbackLocale.value = payload.state.value;
  5562. }
  5563. else if (field === 'inheritLocale' && isBoolean(payload.state.value)) {
  5564. composer.inheritLocale = payload.state.value;
  5565. }
  5566. }
  5567. }
  5568. /**
  5569. * Supports compatibility for legacy vue-i18n APIs
  5570. * This mixin is used when we use vue-i18n@v9.x or later
  5571. */
  5572. function defineMixin(vuei18n, composer, i18n) {
  5573. return {
  5574. beforeCreate() {
  5575. const instance = vue.getCurrentInstance();
  5576. /* istanbul ignore if */
  5577. if (!instance) {
  5578. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  5579. }
  5580. const options = this.$options;
  5581. if (options.i18n) {
  5582. const optionsI18n = options.i18n;
  5583. if (options.__i18n) {
  5584. optionsI18n.__i18n = options.__i18n;
  5585. }
  5586. optionsI18n.__root = composer;
  5587. if (this === this.$root) {
  5588. // merge option and gttach global
  5589. this.$i18n = mergeToGlobal(vuei18n, optionsI18n);
  5590. }
  5591. else {
  5592. optionsI18n.__injectWithOption = true;
  5593. optionsI18n.__extender = i18n.__vueI18nExtend;
  5594. // atttach local VueI18n instance
  5595. this.$i18n = createVueI18n(optionsI18n);
  5596. // extend VueI18n instance
  5597. const _vueI18n = this.$i18n;
  5598. if (_vueI18n.__extender) {
  5599. _vueI18n.__disposer = _vueI18n.__extender(this.$i18n);
  5600. }
  5601. }
  5602. }
  5603. else if (options.__i18n) {
  5604. if (this === this.$root) {
  5605. // merge option and gttach global
  5606. this.$i18n = mergeToGlobal(vuei18n, options);
  5607. }
  5608. else {
  5609. // atttach local VueI18n instance
  5610. this.$i18n = createVueI18n({
  5611. __i18n: options.__i18n,
  5612. __injectWithOption: true,
  5613. __extender: i18n.__vueI18nExtend,
  5614. __root: composer
  5615. });
  5616. // extend VueI18n instance
  5617. const _vueI18n = this.$i18n;
  5618. if (_vueI18n.__extender) {
  5619. _vueI18n.__disposer = _vueI18n.__extender(this.$i18n);
  5620. }
  5621. }
  5622. }
  5623. else {
  5624. // attach global VueI18n instance
  5625. this.$i18n = vuei18n;
  5626. }
  5627. if (options.__i18nGlobal) {
  5628. adjustI18nResources(composer, options, options);
  5629. }
  5630. // defines vue-i18n legacy APIs
  5631. this.$t = (...args) => this.$i18n.t(...args);
  5632. this.$rt = (...args) => this.$i18n.rt(...args);
  5633. this.$tc = (...args) => this.$i18n.tc(...args);
  5634. this.$te = (key, locale) => this.$i18n.te(key, locale);
  5635. this.$d = (...args) => this.$i18n.d(...args);
  5636. this.$n = (...args) => this.$i18n.n(...args);
  5637. this.$tm = (key) => this.$i18n.tm(key);
  5638. i18n.__setInstance(instance, this.$i18n);
  5639. },
  5640. mounted() {
  5641. /* istanbul ignore if */
  5642. if (this.$el &&
  5643. this.$i18n) {
  5644. const _vueI18n = this.$i18n;
  5645. this.$el.__VUE_I18N__ = _vueI18n.__composer;
  5646. const emitter = (this.__v_emitter =
  5647. createEmitter());
  5648. _vueI18n.__enableEmitter && _vueI18n.__enableEmitter(emitter);
  5649. emitter.on('*', addTimelineEvent);
  5650. }
  5651. },
  5652. unmounted() {
  5653. const instance = vue.getCurrentInstance();
  5654. /* istanbul ignore if */
  5655. if (!instance) {
  5656. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  5657. }
  5658. const _vueI18n = this.$i18n;
  5659. /* istanbul ignore if */
  5660. if (this.$el &&
  5661. this.$el.__VUE_I18N__) {
  5662. if (this.__v_emitter) {
  5663. this.__v_emitter.off('*', addTimelineEvent);
  5664. delete this.__v_emitter;
  5665. }
  5666. if (this.$i18n) {
  5667. _vueI18n.__disableEmitter && _vueI18n.__disableEmitter();
  5668. delete this.$el.__VUE_I18N__;
  5669. }
  5670. }
  5671. delete this.$t;
  5672. delete this.$rt;
  5673. delete this.$tc;
  5674. delete this.$te;
  5675. delete this.$d;
  5676. delete this.$n;
  5677. delete this.$tm;
  5678. if (_vueI18n.__disposer) {
  5679. _vueI18n.__disposer();
  5680. delete _vueI18n.__disposer;
  5681. delete _vueI18n.__extender;
  5682. }
  5683. i18n.__deleteInstance(instance);
  5684. delete this.$i18n;
  5685. }
  5686. };
  5687. }
  5688. function mergeToGlobal(g, options) {
  5689. g.locale = options.locale || g.locale;
  5690. g.fallbackLocale = options.fallbackLocale || g.fallbackLocale;
  5691. g.missing = options.missing || g.missing;
  5692. g.silentTranslationWarn =
  5693. options.silentTranslationWarn || g.silentFallbackWarn;
  5694. g.silentFallbackWarn = options.silentFallbackWarn || g.silentFallbackWarn;
  5695. g.formatFallbackMessages =
  5696. options.formatFallbackMessages || g.formatFallbackMessages;
  5697. g.postTranslation = options.postTranslation || g.postTranslation;
  5698. g.warnHtmlInMessage = options.warnHtmlInMessage || g.warnHtmlInMessage;
  5699. g.escapeParameterHtml = options.escapeParameterHtml || g.escapeParameterHtml;
  5700. g.sync = options.sync || g.sync;
  5701. g.__composer[SetPluralRulesSymbol](options.pluralizationRules || g.pluralizationRules);
  5702. const messages = getLocaleMessages(g.locale, {
  5703. messages: options.messages,
  5704. __i18n: options.__i18n
  5705. });
  5706. Object.keys(messages).forEach(locale => g.mergeLocaleMessage(locale, messages[locale]));
  5707. if (options.datetimeFormats) {
  5708. Object.keys(options.datetimeFormats).forEach(locale => g.mergeDateTimeFormat(locale, options.datetimeFormats[locale]));
  5709. }
  5710. if (options.numberFormats) {
  5711. Object.keys(options.numberFormats).forEach(locale => g.mergeNumberFormat(locale, options.numberFormats[locale]));
  5712. }
  5713. return g;
  5714. }
  5715. /**
  5716. * Injection key for {@link useI18n}
  5717. *
  5718. * @remarks
  5719. * The global injection key for I18n instances with `useI18n`. this injection key is used in Web Components.
  5720. * Specify the i18n instance created by {@link createI18n} together with `provide` function.
  5721. *
  5722. * @VueI18nGeneral
  5723. */
  5724. const I18nInjectionKey =
  5725. /* #__PURE__*/ makeSymbol('global-vue-i18n');
  5726. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  5727. function createI18n(options = {}, VueI18nLegacy) {
  5728. // prettier-ignore
  5729. const __legacyMode = isBoolean(options.legacy)
  5730. ? options.legacy
  5731. : true;
  5732. // prettier-ignore
  5733. const __globalInjection = isBoolean(options.globalInjection)
  5734. ? options.globalInjection
  5735. : true;
  5736. // prettier-ignore
  5737. const __allowComposition = __legacyMode
  5738. ? !!options.allowComposition
  5739. : true;
  5740. const __instances = new Map();
  5741. const [globalScope, __global] = createGlobal(options, __legacyMode);
  5742. const symbol = /* #__PURE__*/ makeSymbol('vue-i18n' );
  5743. {
  5744. if (__legacyMode && __allowComposition && !false) {
  5745. warn(getWarnMessage(I18nWarnCodes.NOTICE_DROP_ALLOW_COMPOSITION));
  5746. }
  5747. }
  5748. function __getInstance(component) {
  5749. return __instances.get(component) || null;
  5750. }
  5751. function __setInstance(component, instance) {
  5752. __instances.set(component, instance);
  5753. }
  5754. function __deleteInstance(component) {
  5755. __instances.delete(component);
  5756. }
  5757. {
  5758. const i18n = {
  5759. // mode
  5760. get mode() {
  5761. return __legacyMode
  5762. ? 'legacy'
  5763. : 'composition';
  5764. },
  5765. // allowComposition
  5766. get allowComposition() {
  5767. return __allowComposition;
  5768. },
  5769. // install plugin
  5770. async install(app, ...options) {
  5771. {
  5772. app.__VUE_I18N__ = i18n;
  5773. }
  5774. // setup global provider
  5775. app.__VUE_I18N_SYMBOL__ = symbol;
  5776. app.provide(app.__VUE_I18N_SYMBOL__, i18n);
  5777. // set composer & vuei18n extend hook options from plugin options
  5778. if (isPlainObject(options[0])) {
  5779. const opts = options[0];
  5780. i18n.__composerExtend =
  5781. opts.__composerExtend;
  5782. i18n.__vueI18nExtend =
  5783. opts.__vueI18nExtend;
  5784. }
  5785. // global method and properties injection for Composition API
  5786. let globalReleaseHandler = null;
  5787. if (!__legacyMode && __globalInjection) {
  5788. globalReleaseHandler = injectGlobalFields(app, i18n.global);
  5789. }
  5790. // install built-in components and directive
  5791. {
  5792. apply(app, i18n, ...options);
  5793. }
  5794. // setup mixin for Legacy API
  5795. if (__legacyMode) {
  5796. app.mixin(defineMixin(__global, __global.__composer, i18n));
  5797. }
  5798. // release global scope
  5799. const unmountApp = app.unmount;
  5800. app.unmount = () => {
  5801. globalReleaseHandler && globalReleaseHandler();
  5802. i18n.dispose();
  5803. unmountApp();
  5804. };
  5805. // setup vue-devtools plugin
  5806. {
  5807. const ret = await enableDevTools(app, i18n);
  5808. if (!ret) {
  5809. throw createI18nError(I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN);
  5810. }
  5811. const emitter = createEmitter();
  5812. if (__legacyMode) {
  5813. const _vueI18n = __global;
  5814. _vueI18n.__enableEmitter && _vueI18n.__enableEmitter(emitter);
  5815. }
  5816. else {
  5817. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5818. const _composer = __global;
  5819. _composer[EnableEmitter] && _composer[EnableEmitter](emitter);
  5820. }
  5821. emitter.on('*', addTimelineEvent);
  5822. }
  5823. },
  5824. // global accessor
  5825. get global() {
  5826. return __global;
  5827. },
  5828. dispose() {
  5829. globalScope.stop();
  5830. },
  5831. // @internal
  5832. __instances,
  5833. // @internal
  5834. __getInstance,
  5835. // @internal
  5836. __setInstance,
  5837. // @internal
  5838. __deleteInstance
  5839. };
  5840. return i18n;
  5841. }
  5842. }
  5843. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  5844. function useI18n(options = {}) {
  5845. const instance = vue.getCurrentInstance();
  5846. if (instance == null) {
  5847. throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
  5848. }
  5849. if (!instance.isCE &&
  5850. instance.appContext.app != null &&
  5851. !instance.appContext.app.__VUE_I18N_SYMBOL__) {
  5852. throw createI18nError(I18nErrorCodes.NOT_INSTALLED);
  5853. }
  5854. const i18n = getI18nInstance(instance);
  5855. const gl = getGlobalComposer(i18n);
  5856. const componentOptions = getComponentOptions(instance);
  5857. const scope = getScope(options, componentOptions);
  5858. {
  5859. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5860. if (i18n.mode === 'legacy' && !options.__useComponent) {
  5861. if (!i18n.allowComposition) {
  5862. throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE);
  5863. }
  5864. return useI18nForLegacy(instance, scope, gl, options);
  5865. }
  5866. }
  5867. if (scope === 'global') {
  5868. adjustI18nResources(gl, options, componentOptions);
  5869. return gl;
  5870. }
  5871. if (scope === 'parent') {
  5872. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5873. let composer = getComposer(i18n, instance, options.__useComponent);
  5874. if (composer == null) {
  5875. {
  5876. warn(getWarnMessage(I18nWarnCodes.NOT_FOUND_PARENT_SCOPE));
  5877. }
  5878. composer = gl;
  5879. }
  5880. return composer;
  5881. }
  5882. const i18nInternal = i18n;
  5883. let composer = i18nInternal.__getInstance(instance);
  5884. if (composer == null) {
  5885. const composerOptions = assign({}, options);
  5886. if ('__i18n' in componentOptions) {
  5887. composerOptions.__i18n = componentOptions.__i18n;
  5888. }
  5889. if (gl) {
  5890. composerOptions.__root = gl;
  5891. }
  5892. composer = createComposer(composerOptions);
  5893. if (i18nInternal.__composerExtend) {
  5894. composer[DisposeSymbol] =
  5895. i18nInternal.__composerExtend(composer);
  5896. }
  5897. setupLifeCycle(i18nInternal, instance, composer);
  5898. i18nInternal.__setInstance(instance, composer);
  5899. }
  5900. return composer;
  5901. }
  5902. /**
  5903. * Cast to VueI18n legacy compatible type
  5904. *
  5905. * @remarks
  5906. * This API is provided only with [vue-i18n-bridge](https://vue-i18n.intlify.dev/guide/migration/ways.html#what-is-vue-i18n-bridge).
  5907. *
  5908. * The purpose of this function is to convert an {@link I18n} instance created with {@link createI18n | createI18n(legacy: true)} into a `vue-i18n@v8.x` compatible instance of `new VueI18n` in a TypeScript environment.
  5909. *
  5910. * @param i18n - An instance of {@link I18n}
  5911. * @returns A i18n instance which is casted to {@link VueI18n} type
  5912. *
  5913. * @VueI18nTip
  5914. * :new: provided by **vue-i18n-bridge only**
  5915. *
  5916. * @VueI18nGeneral
  5917. */
  5918. /* #__NO_SIDE_EFFECTS__ */
  5919. const castToVueI18n = (i18n
  5920. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5921. ) => {
  5922. if (!(__VUE_I18N_BRIDGE__ in i18n)) {
  5923. throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N);
  5924. }
  5925. return i18n;
  5926. };
  5927. function createGlobal(options, legacyMode, VueI18nLegacy // eslint-disable-line @typescript-eslint/no-explicit-any
  5928. ) {
  5929. const scope = vue.effectScope();
  5930. {
  5931. const obj = legacyMode
  5932. ? scope.run(() => createVueI18n(options))
  5933. : scope.run(() => createComposer(options));
  5934. if (obj == null) {
  5935. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  5936. }
  5937. return [scope, obj];
  5938. }
  5939. }
  5940. function getI18nInstance(instance) {
  5941. {
  5942. const i18n = vue.inject(!instance.isCE
  5943. ? instance.appContext.app.__VUE_I18N_SYMBOL__
  5944. : I18nInjectionKey);
  5945. /* istanbul ignore if */
  5946. if (!i18n) {
  5947. throw createI18nError(!instance.isCE
  5948. ? I18nErrorCodes.UNEXPECTED_ERROR
  5949. : I18nErrorCodes.NOT_INSTALLED_WITH_PROVIDE);
  5950. }
  5951. return i18n;
  5952. }
  5953. }
  5954. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  5955. function getScope(options, componentOptions) {
  5956. // prettier-ignore
  5957. return isEmptyObject(options)
  5958. ? ('__i18n' in componentOptions)
  5959. ? 'local'
  5960. : 'global'
  5961. : !options.useScope
  5962. ? 'local'
  5963. : options.useScope;
  5964. }
  5965. function getGlobalComposer(i18n) {
  5966. // prettier-ignore
  5967. return i18n.mode === 'composition'
  5968. ? i18n.global
  5969. : i18n.global.__composer
  5970. ;
  5971. }
  5972. function getComposer(i18n, target, useComponent = false) {
  5973. let composer = null;
  5974. const root = target.root;
  5975. let current = getParentComponentInstance(target, useComponent);
  5976. while (current != null) {
  5977. const i18nInternal = i18n;
  5978. if (i18n.mode === 'composition') {
  5979. composer = i18nInternal.__getInstance(current);
  5980. }
  5981. else {
  5982. {
  5983. const vueI18n = i18nInternal.__getInstance(current);
  5984. if (vueI18n != null) {
  5985. composer = vueI18n
  5986. .__composer;
  5987. if (useComponent &&
  5988. composer &&
  5989. !composer[InejctWithOptionSymbol] // eslint-disable-line @typescript-eslint/no-explicit-any
  5990. ) {
  5991. composer = null;
  5992. }
  5993. }
  5994. }
  5995. }
  5996. if (composer != null) {
  5997. break;
  5998. }
  5999. if (root === current) {
  6000. break;
  6001. }
  6002. current = current.parent;
  6003. }
  6004. return composer;
  6005. }
  6006. function getParentComponentInstance(target, useComponent = false) {
  6007. if (target == null) {
  6008. return null;
  6009. }
  6010. {
  6011. // if `useComponent: true` will be specified, we get lexical scope owner instance for use-case slots
  6012. return !useComponent
  6013. ? target.parent
  6014. : target.vnode.ctx || target.parent; // eslint-disable-line @typescript-eslint/no-explicit-any
  6015. }
  6016. }
  6017. function setupLifeCycle(i18n, target, composer) {
  6018. let emitter = null;
  6019. {
  6020. vue.onMounted(() => {
  6021. // inject composer instance to DOM for intlify-devtools
  6022. if (target.vnode.el) {
  6023. target.vnode.el.__VUE_I18N__ = composer;
  6024. emitter = createEmitter();
  6025. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6026. const _composer = composer;
  6027. _composer[EnableEmitter] && _composer[EnableEmitter](emitter);
  6028. emitter.on('*', addTimelineEvent);
  6029. }
  6030. }, target);
  6031. vue.onUnmounted(() => {
  6032. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6033. const _composer = composer;
  6034. // remove composer instance from DOM for intlify-devtools
  6035. if (target.vnode.el &&
  6036. target.vnode.el.__VUE_I18N__) {
  6037. emitter && emitter.off('*', addTimelineEvent);
  6038. _composer[DisableEmitter] && _composer[DisableEmitter]();
  6039. delete target.vnode.el.__VUE_I18N__;
  6040. }
  6041. i18n.__deleteInstance(target);
  6042. // dispose extended resources
  6043. const dispose = _composer[DisposeSymbol];
  6044. if (dispose) {
  6045. dispose();
  6046. delete _composer[DisposeSymbol];
  6047. }
  6048. }, target);
  6049. }
  6050. }
  6051. function useI18nForLegacy(instance, scope, root, options = {} // eslint-disable-line @typescript-eslint/no-explicit-any
  6052. ) {
  6053. const isLocalScope = scope === 'local';
  6054. const _composer = vue.shallowRef(null);
  6055. if (isLocalScope &&
  6056. instance.proxy &&
  6057. !(instance.proxy.$options.i18n || instance.proxy.$options.__i18n)) {
  6058. throw createI18nError(I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION);
  6059. }
  6060. const _inheritLocale = isBoolean(options.inheritLocale)
  6061. ? options.inheritLocale
  6062. : !isString(options.locale);
  6063. const _locale = vue.ref(
  6064. // prettier-ignore
  6065. !isLocalScope || _inheritLocale
  6066. ? root.locale.value
  6067. : isString(options.locale)
  6068. ? options.locale
  6069. : DEFAULT_LOCALE);
  6070. const _fallbackLocale = vue.ref(
  6071. // prettier-ignore
  6072. !isLocalScope || _inheritLocale
  6073. ? root.fallbackLocale.value
  6074. : isString(options.fallbackLocale) ||
  6075. isArray(options.fallbackLocale) ||
  6076. isPlainObject(options.fallbackLocale) ||
  6077. options.fallbackLocale === false
  6078. ? options.fallbackLocale
  6079. : _locale.value);
  6080. const _messages = vue.ref(getLocaleMessages(_locale.value, options));
  6081. // prettier-ignore
  6082. const _datetimeFormats = vue.ref(isPlainObject(options.datetimeFormats)
  6083. ? options.datetimeFormats
  6084. : { [_locale.value]: {} });
  6085. // prettier-ignore
  6086. const _numberFormats = vue.ref(isPlainObject(options.numberFormats)
  6087. ? options.numberFormats
  6088. : { [_locale.value]: {} });
  6089. // prettier-ignore
  6090. const _missingWarn = isLocalScope
  6091. ? root.missingWarn
  6092. : isBoolean(options.missingWarn) || isRegExp(options.missingWarn)
  6093. ? options.missingWarn
  6094. : true;
  6095. // prettier-ignore
  6096. const _fallbackWarn = isLocalScope
  6097. ? root.fallbackWarn
  6098. : isBoolean(options.fallbackWarn) || isRegExp(options.fallbackWarn)
  6099. ? options.fallbackWarn
  6100. : true;
  6101. // prettier-ignore
  6102. const _fallbackRoot = isLocalScope
  6103. ? root.fallbackRoot
  6104. : isBoolean(options.fallbackRoot)
  6105. ? options.fallbackRoot
  6106. : true;
  6107. // configure fall back to root
  6108. const _fallbackFormat = !!options.fallbackFormat;
  6109. // runtime missing
  6110. const _missing = isFunction(options.missing) ? options.missing : null;
  6111. // postTranslation handler
  6112. const _postTranslation = isFunction(options.postTranslation)
  6113. ? options.postTranslation
  6114. : null;
  6115. // prettier-ignore
  6116. const _warnHtmlMessage = isLocalScope
  6117. ? root.warnHtmlMessage
  6118. : isBoolean(options.warnHtmlMessage)
  6119. ? options.warnHtmlMessage
  6120. : true;
  6121. const _escapeParameter = !!options.escapeParameter;
  6122. // prettier-ignore
  6123. const _modifiers = isLocalScope
  6124. ? root.modifiers
  6125. : isPlainObject(options.modifiers)
  6126. ? options.modifiers
  6127. : {};
  6128. // pluralRules
  6129. const _pluralRules = options.pluralRules || (isLocalScope && root.pluralRules);
  6130. // track reactivity
  6131. function trackReactivityValues() {
  6132. return [
  6133. _locale.value,
  6134. _fallbackLocale.value,
  6135. _messages.value,
  6136. _datetimeFormats.value,
  6137. _numberFormats.value
  6138. ];
  6139. }
  6140. // locale
  6141. const locale = vue.computed({
  6142. get: () => {
  6143. return _composer.value ? _composer.value.locale.value : _locale.value;
  6144. },
  6145. set: val => {
  6146. if (_composer.value) {
  6147. _composer.value.locale.value = val;
  6148. }
  6149. _locale.value = val;
  6150. }
  6151. });
  6152. // fallbackLocale
  6153. const fallbackLocale = vue.computed({
  6154. get: () => {
  6155. return _composer.value
  6156. ? _composer.value.fallbackLocale.value
  6157. : _fallbackLocale.value;
  6158. },
  6159. set: val => {
  6160. if (_composer.value) {
  6161. _composer.value.fallbackLocale.value = val;
  6162. }
  6163. _fallbackLocale.value = val;
  6164. }
  6165. });
  6166. // messages
  6167. const messages = vue.computed(() => {
  6168. if (_composer.value) {
  6169. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6170. return _composer.value.messages.value;
  6171. }
  6172. else {
  6173. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6174. return _messages.value;
  6175. }
  6176. });
  6177. const datetimeFormats = vue.computed(() => _datetimeFormats.value);
  6178. const numberFormats = vue.computed(() => _numberFormats.value);
  6179. function getPostTranslationHandler() {
  6180. return _composer.value
  6181. ? _composer.value.getPostTranslationHandler()
  6182. : _postTranslation;
  6183. }
  6184. function setPostTranslationHandler(handler) {
  6185. if (_composer.value) {
  6186. _composer.value.setPostTranslationHandler(handler);
  6187. }
  6188. }
  6189. function getMissingHandler() {
  6190. return _composer.value ? _composer.value.getMissingHandler() : _missing;
  6191. }
  6192. function setMissingHandler(handler) {
  6193. if (_composer.value) {
  6194. _composer.value.setMissingHandler(handler);
  6195. }
  6196. }
  6197. function warpWithDeps(fn) {
  6198. trackReactivityValues();
  6199. return fn();
  6200. }
  6201. function t(...args) {
  6202. return _composer.value
  6203. ? warpWithDeps(() => Reflect.apply(_composer.value.t, null, [...args]))
  6204. : warpWithDeps(() => '');
  6205. }
  6206. function rt(...args) {
  6207. return _composer.value
  6208. ? Reflect.apply(_composer.value.rt, null, [...args])
  6209. : '';
  6210. }
  6211. function d(...args) {
  6212. return _composer.value
  6213. ? warpWithDeps(() => Reflect.apply(_composer.value.d, null, [...args]))
  6214. : warpWithDeps(() => '');
  6215. }
  6216. function n(...args) {
  6217. return _composer.value
  6218. ? warpWithDeps(() => Reflect.apply(_composer.value.n, null, [...args]))
  6219. : warpWithDeps(() => '');
  6220. }
  6221. function tm(key) {
  6222. return _composer.value ? _composer.value.tm(key) : {};
  6223. }
  6224. function te(key, locale) {
  6225. return _composer.value ? _composer.value.te(key, locale) : false;
  6226. }
  6227. function getLocaleMessage(locale) {
  6228. return _composer.value ? _composer.value.getLocaleMessage(locale) : {};
  6229. }
  6230. function setLocaleMessage(locale, message) {
  6231. if (_composer.value) {
  6232. _composer.value.setLocaleMessage(locale, message);
  6233. _messages.value[locale] = message;
  6234. }
  6235. }
  6236. function mergeLocaleMessage(locale, message) {
  6237. if (_composer.value) {
  6238. _composer.value.mergeLocaleMessage(locale, message);
  6239. }
  6240. }
  6241. function getDateTimeFormat(locale) {
  6242. return _composer.value ? _composer.value.getDateTimeFormat(locale) : {};
  6243. }
  6244. function setDateTimeFormat(locale, format) {
  6245. if (_composer.value) {
  6246. _composer.value.setDateTimeFormat(locale, format);
  6247. _datetimeFormats.value[locale] = format;
  6248. }
  6249. }
  6250. function mergeDateTimeFormat(locale, format) {
  6251. if (_composer.value) {
  6252. _composer.value.mergeDateTimeFormat(locale, format);
  6253. }
  6254. }
  6255. function getNumberFormat(locale) {
  6256. return _composer.value ? _composer.value.getNumberFormat(locale) : {};
  6257. }
  6258. function setNumberFormat(locale, format) {
  6259. if (_composer.value) {
  6260. _composer.value.setNumberFormat(locale, format);
  6261. _numberFormats.value[locale] = format;
  6262. }
  6263. }
  6264. function mergeNumberFormat(locale, format) {
  6265. if (_composer.value) {
  6266. _composer.value.mergeNumberFormat(locale, format);
  6267. }
  6268. }
  6269. const wrapper = {
  6270. get id() {
  6271. return _composer.value ? _composer.value.id : -1;
  6272. },
  6273. locale,
  6274. fallbackLocale,
  6275. messages,
  6276. datetimeFormats,
  6277. numberFormats,
  6278. get inheritLocale() {
  6279. return _composer.value ? _composer.value.inheritLocale : _inheritLocale;
  6280. },
  6281. set inheritLocale(val) {
  6282. if (_composer.value) {
  6283. _composer.value.inheritLocale = val;
  6284. }
  6285. },
  6286. get availableLocales() {
  6287. return _composer.value
  6288. ? _composer.value.availableLocales
  6289. : Object.keys(_messages.value);
  6290. },
  6291. get modifiers() {
  6292. return (_composer.value ? _composer.value.modifiers : _modifiers);
  6293. },
  6294. get pluralRules() {
  6295. return (_composer.value ? _composer.value.pluralRules : _pluralRules);
  6296. },
  6297. get isGlobal() {
  6298. return _composer.value ? _composer.value.isGlobal : false;
  6299. },
  6300. get missingWarn() {
  6301. return _composer.value ? _composer.value.missingWarn : _missingWarn;
  6302. },
  6303. set missingWarn(val) {
  6304. if (_composer.value) {
  6305. _composer.value.missingWarn = val;
  6306. }
  6307. },
  6308. get fallbackWarn() {
  6309. return _composer.value ? _composer.value.fallbackWarn : _fallbackWarn;
  6310. },
  6311. set fallbackWarn(val) {
  6312. if (_composer.value) {
  6313. _composer.value.missingWarn = val;
  6314. }
  6315. },
  6316. get fallbackRoot() {
  6317. return _composer.value ? _composer.value.fallbackRoot : _fallbackRoot;
  6318. },
  6319. set fallbackRoot(val) {
  6320. if (_composer.value) {
  6321. _composer.value.fallbackRoot = val;
  6322. }
  6323. },
  6324. get fallbackFormat() {
  6325. return _composer.value ? _composer.value.fallbackFormat : _fallbackFormat;
  6326. },
  6327. set fallbackFormat(val) {
  6328. if (_composer.value) {
  6329. _composer.value.fallbackFormat = val;
  6330. }
  6331. },
  6332. get warnHtmlMessage() {
  6333. return _composer.value
  6334. ? _composer.value.warnHtmlMessage
  6335. : _warnHtmlMessage;
  6336. },
  6337. set warnHtmlMessage(val) {
  6338. if (_composer.value) {
  6339. _composer.value.warnHtmlMessage = val;
  6340. }
  6341. },
  6342. get escapeParameter() {
  6343. return _composer.value
  6344. ? _composer.value.escapeParameter
  6345. : _escapeParameter;
  6346. },
  6347. set escapeParameter(val) {
  6348. if (_composer.value) {
  6349. _composer.value.escapeParameter = val;
  6350. }
  6351. },
  6352. t,
  6353. getPostTranslationHandler,
  6354. setPostTranslationHandler,
  6355. getMissingHandler,
  6356. setMissingHandler,
  6357. rt,
  6358. d,
  6359. n,
  6360. tm,
  6361. te,
  6362. getLocaleMessage,
  6363. setLocaleMessage,
  6364. mergeLocaleMessage,
  6365. getDateTimeFormat,
  6366. setDateTimeFormat,
  6367. mergeDateTimeFormat,
  6368. getNumberFormat,
  6369. setNumberFormat,
  6370. mergeNumberFormat
  6371. };
  6372. function sync(composer) {
  6373. composer.locale.value = _locale.value;
  6374. composer.fallbackLocale.value = _fallbackLocale.value;
  6375. Object.keys(_messages.value).forEach(locale => {
  6376. composer.mergeLocaleMessage(locale, _messages.value[locale]);
  6377. });
  6378. Object.keys(_datetimeFormats.value).forEach(locale => {
  6379. composer.mergeDateTimeFormat(locale, _datetimeFormats.value[locale]);
  6380. });
  6381. Object.keys(_numberFormats.value).forEach(locale => {
  6382. composer.mergeNumberFormat(locale, _numberFormats.value[locale]);
  6383. });
  6384. composer.escapeParameter = _escapeParameter;
  6385. composer.fallbackFormat = _fallbackFormat;
  6386. composer.fallbackRoot = _fallbackRoot;
  6387. composer.fallbackWarn = _fallbackWarn;
  6388. composer.missingWarn = _missingWarn;
  6389. composer.warnHtmlMessage = _warnHtmlMessage;
  6390. }
  6391. vue.onBeforeMount(() => {
  6392. if (instance.proxy == null || instance.proxy.$i18n == null) {
  6393. throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY);
  6394. }
  6395. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6396. const composer = (_composer.value = instance.proxy.$i18n
  6397. .__composer);
  6398. if (scope === 'global') {
  6399. _locale.value = composer.locale.value;
  6400. _fallbackLocale.value = composer.fallbackLocale.value;
  6401. _messages.value = composer.messages.value;
  6402. _datetimeFormats.value = composer.datetimeFormats.value;
  6403. _numberFormats.value = composer.numberFormats.value;
  6404. }
  6405. else if (isLocalScope) {
  6406. sync(composer);
  6407. }
  6408. });
  6409. return wrapper;
  6410. }
  6411. const globalExportProps = [
  6412. 'locale',
  6413. 'fallbackLocale',
  6414. 'availableLocales'
  6415. ];
  6416. const globalExportMethods = ['t', 'rt', 'd', 'n', 'tm', 'te']
  6417. ;
  6418. function injectGlobalFields(app, composer) {
  6419. const i18n = Object.create(null);
  6420. globalExportProps.forEach(prop => {
  6421. const desc = Object.getOwnPropertyDescriptor(composer, prop);
  6422. if (!desc) {
  6423. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  6424. }
  6425. const wrap = vue.isRef(desc.value) // check computed props
  6426. ? {
  6427. get() {
  6428. return desc.value.value;
  6429. },
  6430. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6431. set(val) {
  6432. desc.value.value = val;
  6433. }
  6434. }
  6435. : {
  6436. get() {
  6437. return desc.get && desc.get();
  6438. }
  6439. };
  6440. Object.defineProperty(i18n, prop, wrap);
  6441. });
  6442. app.config.globalProperties.$i18n = i18n;
  6443. globalExportMethods.forEach(method => {
  6444. const desc = Object.getOwnPropertyDescriptor(composer, method);
  6445. if (!desc || !desc.value) {
  6446. throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
  6447. }
  6448. Object.defineProperty(app.config.globalProperties, `$${method}`, desc);
  6449. });
  6450. const dispose = () => {
  6451. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6452. delete app.config.globalProperties.$i18n;
  6453. globalExportMethods.forEach(method => {
  6454. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  6455. delete app.config.globalProperties[`$${method}`];
  6456. });
  6457. };
  6458. return dispose;
  6459. }
  6460. // register message compiler at vue-i18n
  6461. {
  6462. registerMessageCompiler(compile);
  6463. }
  6464. // register message resolver at vue-i18n
  6465. registerMessageResolver(resolveValue);
  6466. // register fallback locale at vue-i18n
  6467. registerLocaleFallbacker(fallbackWithLocaleChain);
  6468. // NOTE: experimental !!
  6469. {
  6470. const target = getGlobalThis();
  6471. target.__INTLIFY__ = true;
  6472. setDevToolsHook(target.__INTLIFY_DEVTOOLS_GLOBAL_HOOK__);
  6473. }
  6474. {
  6475. initDev();
  6476. }
  6477. exports.DatetimeFormat = DatetimeFormat;
  6478. exports.I18nD = I18nD;
  6479. exports.I18nInjectionKey = I18nInjectionKey;
  6480. exports.I18nN = I18nN;
  6481. exports.I18nT = I18nT;
  6482. exports.NumberFormat = NumberFormat;
  6483. exports.Translation = Translation;
  6484. exports.VERSION = VERSION;
  6485. exports.castToVueI18n = castToVueI18n;
  6486. exports.createI18n = createI18n;
  6487. exports.useI18n = useI18n;
  6488. exports.vTDirective = vTDirective;
  6489. return exports;
  6490. })({}, Vue);