vue-i18n.global.js 237 KB

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