@@ -20,13 +20,15 @@ import path, { resolve } from "node:path";
2020
2121import { generatePseudoLocaleModule } from "./pseudolocalize.mjs" ;
2222
23- // import localizeRules from "../lit-localize.json" with { type: "json" };
2423import { ConsoleLogger } from "#logger/node" ;
2524import { PackageRoot } from "#paths/node" ;
2625
2726import { readConfigFileAndWriteSchema } from "@lit/localize-tools/lib/config.js" ;
2827import { RuntimeLitLocalizer } from "@lit/localize-tools/lib/modes/runtime.js" ;
2928
29+ //#region Setup
30+
31+ const missingMessagePattern = / ( [ \w _ - ] + ) \s m e s s a g e \s (?: [ \w _ - ] + ) \s i s \s m i s s i n g / ;
3032const logger = ConsoleLogger . child ( { name : "Locales" } ) ;
3133
3234const localizeRules = readConfigFileAndWriteSchema ( path . join ( PackageRoot , "lit-localize.json" ) ) ;
@@ -43,6 +45,17 @@ const EmittedLocalesDirectory = resolve(
4345 /** @type {string } */ ( localizeRules . output . outputDir ) ,
4446) ;
4547
48+ const targetLocales = localizeRules . targetLocales . filter ( ( localeCode ) => {
49+ return localeCode !== "pseudo-LOCALE" ;
50+ } ) ;
51+
52+ //#endregion
53+
54+ //#region Utilities
55+
56+ /**
57+ * Cleans the emitted locales directory.
58+ */
4659async function cleanEmittedLocales ( ) {
4760 logger . info ( "♻️ Cleaning previously emitted locales..." ) ;
4861 logger . info ( `♻️ ${ EmittedLocalesDirectory } ` ) ;
@@ -113,10 +126,6 @@ async function checkIfEmittedFileCurrent(localeCode) {
113126async function checkIfLocalesAreCurrent ( ) {
114127 logger . info ( "Reading locale configuration..." ) ;
115128
116- const targetLocales = localizeRules . targetLocales . filter ( ( localeCode ) => {
117- return localeCode !== "pseudo-LOCALE" ;
118- } ) ;
119-
120129 logger . info ( `Checking ${ targetLocales . length } source files...` ) ;
121130
122131 let outOfDateCount = 0 ;
@@ -141,16 +150,56 @@ export async function generateLocaleModules() {
141150
142151 logger . info ( "Generating locale modules..." ) ;
143152
144- const localizer = new RuntimeLitLocalizer ( {
145- ...localizeRules ,
146- output : /** @type {RuntimeOutputConfig } */ ( localizeRules . output ) ,
147- } ) ;
153+ /**
154+ * @type {Map<string, number> }
155+ */
156+ const localeWarnings = new Map ( ) ;
157+
158+ const initialConsoleWarn = console . warn ;
159+
160+ console . warn = ( arg0 , ...args ) => {
161+ if ( typeof arg0 !== "string" ) {
162+ initialConsoleWarn ( arg0 , ...args ) ;
163+ return ;
164+ }
165+
166+ const [ , matchedLocale ] = arg0 . match ( missingMessagePattern ) || [ ] ;
167+
168+ if ( matchedLocale ) {
169+ const count = localeWarnings . get ( matchedLocale ) || 0 ;
170+
171+ localeWarnings . set ( matchedLocale , count + 1 ) ;
172+
173+ return ;
174+ }
175+
176+ initialConsoleWarn ( arg0 , ...args ) ;
177+ } ;
178+
179+ // @ts -expect-error: Type is too broad.
180+ const localizer = new RuntimeLitLocalizer ( localizeRules ) ;
148181
149182 await localizer . build ( ) ;
150183
184+ const report = Array . from ( localeWarnings )
185+ . filter ( ( [ , count ] ) => count )
186+ . sort ( ( [ , totalsA ] , [ , totalsB ] ) => {
187+ return totalsB - totalsA ;
188+ } )
189+ . map ( ( [ locale , count ] ) => `${ locale } : ${ count . toLocaleString ( ) } ` )
190+ . join ( "\n" ) ;
191+
192+ logger . info ( `Missing translations:\n${ report } ` ) ;
193+
194+ localizer . assertTranslationsAreValid ( ) ;
195+
151196 logger . info ( "Complete." ) ;
152197}
153198
199+ //#endregion
200+
201+ //#region Commands
202+
154203async function delegateCommand ( ) {
155204 const command = process . argv [ 2 ] ;
156205
@@ -160,7 +209,7 @@ async function delegateCommand() {
160209 case "--check" :
161210 return checkIfLocalesAreCurrent ( ) ;
162211 case "--force" :
163- return generateLocaleModules ( ) ;
212+ return cleanEmittedLocales ( ) . then ( generateLocaleModules ) ;
164213 }
165214
166215 const upToDate = await checkIfLocalesAreCurrent ( ) ;
@@ -184,3 +233,5 @@ await delegateCommand()
184233 logger . error ( `Error during locale build: ${ error } ` ) ;
185234 process . exit ( 1 ) ;
186235 } ) ;
236+
237+ //#endregion
0 commit comments