@@ -15,11 +15,35 @@ const TEST_LANGUAGES = [
1515// hardcoded list to drift out of sync.
1616const PRINT_VIEW_SOURCE_FILE = './src/assets/print-view/fetch-orcid.js'
1717
18+ // Sentinels used to carry XLIFF <x/> placeholder elements through the xml2js
19+ // Builder, which escapes any markup it finds in a text node. We emit the
20+ // sentinels as plain text and swap them for real elements in formatXmlOutput().
21+ const PH_START = '@@XPH_START@@'
22+ const PH_MID = '@@XPH_MID@@'
23+ const PH_END = '@@XPH_END@@'
24+
25+ // Matches a $localize template placeholder: `${expression}:NAME:`.
26+ // NAME deliberately accepts any case: fetch-orcid.js uses camelCase names, and
27+ // $localize matches placeholder names verbatim.
28+ const LOCALIZE_PLACEHOLDER_RE = / \$ \{ ( [ ^ } ] * ) \} : ( [ A - Z a - z 0 - 9 _ ] + ) : / g
29+
30+ // Non-global twin of the above, for stateless `.test()` checks (a shared /g
31+ // regex carries lastIndex between calls).
32+ const HAS_LOCALIZE_PLACEHOLDER_RE = new RegExp ( LOCALIZE_PLACEHOLDER_RE . source )
33+
1834interface PrintViewUnit {
1935 id : string
2036 source : string
2137}
2238
39+ function escapeXmlAttr ( value : string ) : string {
40+ return value
41+ . replace ( / & / g, '&' )
42+ . replace ( / < / g, '<' )
43+ . replace ( / > / g, '>' )
44+ . replace ( / " / g, '"' )
45+ }
46+
2347function parsePrintViewUnits ( path : string ) : PrintViewUnit [ ] {
2448 const js = fs . readFileSync ( path , 'utf8' )
2549 // Matches $localize tagged templates of the form `:@@printView.someId:Source text`
@@ -33,11 +57,18 @@ function parsePrintViewUnits(path: string): PrintViewUnit[] {
3357 continue
3458 }
3559 seen . add ( id )
36- // Convert $localize template placeholders `${expression}:NAME:` into the
37- // canonical $localize message form `{$NAME}` so the XLF source matches the
38- // runtime message and stays stable regardless of the JS expression.
60+ // Convert $localize template placeholders `${expression}:NAME:` into XLIFF
61+ // <x id="NAME"/> elements (emitted as sentinels here, see formatXmlOutput).
62+ // Plain-text forms such as `{$NAME}` do NOT work: Xliff1TranslationParser
63+ // only recognises real <x/> elements, so a text placeholder is parsed as
64+ // literal content and the message ends up with zero placeholders — the
65+ // translation is then silently truncated at the first `$`.
3966 const source = normalizeText (
40- rawSource . replace ( / \$ \{ [ ^ } ] * \} : ( [ A - Z 0 - 9 _ ] + ) : / g, '{$$$1}' )
67+ rawSource . replace (
68+ LOCALIZE_PLACEHOLDER_RE ,
69+ ( _match , expression : string , name : string ) =>
70+ `${ PH_START } ${ name } ${ PH_MID } \${${ expression } }${ PH_END } `
71+ )
4172 )
4273 units . push ( { id, source } )
4374 }
@@ -54,6 +85,13 @@ function normalizeText(input: string): string {
5485
5586function formatXmlOutput ( xml : string ) : string {
5687 let out = xml
88+ // Restore XLIFF placeholder elements that were emitted as sentinels so the
89+ // xml2js Builder would not escape them into plain text.
90+ out = out . replace (
91+ new RegExp ( `${ PH_START } (.*?)${ PH_MID } (.*?)${ PH_END } ` , 'g' ) ,
92+ ( _match , name : string , equivText : string ) =>
93+ `<x id="${ name } " equiv-text="${ escapeXmlAttr ( equivText ) } "/>`
94+ )
5795 // Match tx pull header formatting (xml decl + <xliff ...> on same line)
5896 out = out . replace (
5997 / < x l i f f [ ^ > ] * > / ,
@@ -97,6 +135,69 @@ function generateTestingLanguages(baseData: any) {
97135 } )
98136}
99137
138+ /** Extracts the ordered <x id="..."/> names inside an XLF element body. */
139+ function placeholderIds ( fragment : string ) : string [ ] {
140+ return Array . from ( fragment . matchAll ( / < x \s + i d = " ( [ ^ " ] + ) " / g) ) . map ( ( m ) => m [ 1 ] )
141+ }
142+
143+ /**
144+ * Warns when a translated printView.* target has lost one of the placeholders
145+ * its source declares.
146+ *
147+ * Such a target still inlines cleanly — @angular/localize reports no error — but
148+ * renders with the value silently dropped, e.g. a peer review count vanishing
149+ * from the heading. That silence is what let this class of bug ship before, so
150+ * surface it at build time. Read-only: translations are never rewritten here.
151+ */
152+ function checkLocalePlaceholders ( localeDir : string ) {
153+ const files = fs
154+ . readdirSync ( localeDir )
155+ . filter ( ( name ) => name . startsWith ( 'messages.' ) && name . endsWith ( '.xlf' ) )
156+ // The generated test languages replace every target with a marker, so their
157+ // placeholders are legitimately absent.
158+ . filter (
159+ ( name ) =>
160+ ! TEST_LANGUAGES . some ( ( { code } ) => name === `messages.${ code } .xlf` )
161+ )
162+
163+ files . forEach ( ( name ) => {
164+ const xlf = fs . readFileSync ( `${ localeDir } /${ name } ` , 'utf8' )
165+
166+ // Transifex holds the <x/> form of these strings, so a pull should never
167+ // bring back the legacy `${expression}:name:` text. If it does, the source
168+ // there has gone stale and every locale would render the heading truncated
169+ // at the first `$` with no other symptom — fail rather than ship that.
170+ if ( HAS_LOCALIZE_PLACEHOLDER_RE . test ( xlf ) ) {
171+ throw new Error (
172+ `[normalize-xlf] ${ name } contains legacy raw placeholder syntax ` +
173+ `(\${expression}:name:). Push the current messages.xlf to Transifex ` +
174+ `so translations come back with <x id="..."/> placeholders.`
175+ )
176+ }
177+
178+ Array . from (
179+ xlf . matchAll ( / < t r a n s - u n i t i d = " ( p r i n t V i e w \. [ ^ " ] * ) " [ \s \S ] * ?< \/ t r a n s - u n i t > / g)
180+ ) . forEach ( ( [ unit , id ] ) => {
181+ const target = unit . match ( / < t a r g e t [ ^ > ] * > ( [ \s \S ] * ?) < \/ t a r g e t > / )
182+ if ( ! target ) {
183+ return
184+ }
185+ const expected = placeholderIds (
186+ unit . match ( / < s o u r c e [ ^ > ] * > ( [ \s \S ] * ?) < \/ s o u r c e > / ) ?. [ 1 ] ?? ''
187+ )
188+ const actual = placeholderIds ( target [ 1 ] )
189+ const missing = expected . filter ( ( p ) => ! actual . includes ( p ) )
190+ if ( missing . length ) {
191+ console . warn (
192+ `[normalize-xlf] ${ name } : "${ id } " target is missing placeholder(s) ${ missing . join (
193+ ', '
194+ ) } — their values will be dropped from the rendered string.`
195+ )
196+ }
197+ } )
198+ } )
199+ }
200+
100201function normalizeXlf12Sources ( path : string ) {
101202 const xml = fs . readFileSync ( path , 'utf8' )
102203 parseString ( xml , ( error , data ) => {
@@ -111,11 +212,24 @@ function normalizeXlf12Sources(path: string) {
111212 throw new Error ( 'No trans-units found in XLF 1.2 file' )
112213 }
113214
215+ const printViewUnits = parsePrintViewUnits ( PRINT_VIEW_SOURCE_FILE )
216+ const printViewById = new Map ( printViewUnits . map ( ( u ) => [ u . id , u ] ) )
217+
114218 transUnits . forEach ( ( tu ) => {
115219 // Provide a stable key for Transifex (some parsers rely on resname over id/source)
116220 if ( tu ?. $ ?. id ) {
117221 tu . $ . resname = tu . $ . id
118222 }
223+ const generated = tu ?. $ ?. id ? printViewById . get ( tu . $ . id ) : undefined
224+ if ( generated ) {
225+ // fetch-orcid.js is the source of truth for printView.* units, so
226+ // always regenerate the source rather than keeping what is on disk.
227+ // Re-parsing a unit that already contains <x/> elements yields mixed
228+ // content, which the Builder cannot round-trip; replacing the whole
229+ // node with a freshly built string keeps placeholders correct.
230+ tu . source = [ generated . source ]
231+ return
232+ }
119233 const src = tu ?. source ?. [ 0 ]
120234 if ( typeof src === 'string' ) {
121235 tu . source [ 0 ] = normalizeText ( src )
@@ -127,7 +241,6 @@ function normalizeXlf12Sources(path: string) {
127241 // will also stamp X / LR / RL for these strings, and so Transifex can
128242 // discover them for real locales. We use a stable @@-prefixed id so
129243 // $localize can match by explicit id.
130- const printViewUnits = parsePrintViewUnits ( PRINT_VIEW_SOURCE_FILE )
131244 const existingIds = new Set ( transUnits . map ( ( tu : any ) => tu ?. $ ?. id ) )
132245 const printViewBody = fileNode ?. body ?. [ 0 ]
133246 printViewUnits . forEach ( ( { id, source } ) => {
@@ -145,3 +258,4 @@ function normalizeXlf12Sources(path: string) {
145258}
146259
147260normalizeXlf12Sources ( './src/locale/messages.xlf' )
261+ checkLocalePlaceholders ( './src/locale' )
0 commit comments