@@ -103,33 +103,13 @@ function deepForIn(object: Object, fn: (value: string, key: string) => void) {
103
103
forIn ( object , iteratee )
104
104
}
105
105
106
- function encodeMessages ( messagesObject : object , locale : string ) {
107
- const messages = cloneDeep ( messagesObject )
108
- deepForIn ( messages , ( message , path ) => {
109
- const parts = message . split ( '|' ) . map ( part => part . trim ( ) )
110
- for ( let i = 0 ; i < parts . length ; i ++ ) {
111
- const meta = ZeroWidthEncoder . encode (
112
- JSON . stringify ( {
113
- locale,
114
- message,
115
- path,
116
- choice : i || undefined ,
117
- } as TranslationMeta ) ,
118
- )
119
- parts [ i ] = meta + parts [ i ]
120
- }
121
- set ( messages , path , parts . join ( ' | ' ) )
122
- } )
123
- return messages
124
- }
125
-
126
106
abstract class ZeroWidthEncoder {
127
107
static START = '\u200B'
128
108
static ZERO = '\u200C'
129
109
static ONE = '\u200D'
130
110
static SPACE = '\u200E'
131
111
static END = '\u200F'
132
- static PATTERN = `${ this . START } [${ this . ZERO } ${ this . ONE } ${ this . SPACE } ]+${ this . END } `
112
+ static PATTERN = new RegExp ( `${ this . START } [${ this . ZERO } ${ this . ONE } ${ this . SPACE } ]+${ this . END } ` , 'gm' )
133
113
134
114
static encode ( text : string ) {
135
115
const binary = text
@@ -259,11 +239,7 @@ class LiveTranslatorManager {
259
239
this . _wrapper . appendChild ( this . _box )
260
240
261
241
// initialize encode
262
- for ( const locale of this . i18n . availableLocales ) {
263
- let messages = this . i18n . getLocaleMessage ( locale )
264
- messages = encodeMessages ( messages , locale )
265
- this . i18n . setLocaleMessage ( locale , messages )
266
- }
242
+ // moved to toggle()
267
243
268
244
// initialize decode & render
269
245
const throttler = throttle ( ( ) => this . render ( ) , this . _options . refreshRate || 50 )
@@ -291,6 +267,35 @@ class LiveTranslatorManager {
291
267
return this . _options . i18n . global || this . _options . i18n
292
268
}
293
269
270
+ transformMessages ( operation : 'encode' | 'restore' = 'encode' ) {
271
+ for ( const locale of this . i18n . availableLocales ) {
272
+ const messages = cloneDeep ( this . i18n . getLocaleMessage ( locale ) )
273
+ deepForIn ( messages , ( message , path ) => {
274
+ const parts = message . split ( '|' ) . map ( part => part . trim ( ) )
275
+ for ( let i = 0 ; i < parts . length ; i ++ ) {
276
+ switch ( operation ) {
277
+ case 'encode' :
278
+ const meta = ZeroWidthEncoder . encode (
279
+ JSON . stringify ( {
280
+ locale,
281
+ message,
282
+ path,
283
+ choice : i || undefined ,
284
+ } as TranslationMeta ) ,
285
+ )
286
+ parts [ i ] = meta + parts [ i ]
287
+ break
288
+ case 'restore' :
289
+ parts [ i ] = parts [ i ] . replaceAll ( ZeroWidthEncoder . PATTERN , '' )
290
+ break
291
+ }
292
+ }
293
+ set ( messages , path , parts . join ( ' | ' ) )
294
+ } )
295
+ this . i18n . setLocaleMessage ( locale , messages )
296
+ }
297
+ }
298
+
294
299
toggle ( enable ?: boolean ) {
295
300
if ( enable !== undefined ) {
296
301
this . _enabled = enable
@@ -304,6 +309,7 @@ class LiveTranslatorManager {
304
309
if ( ! this . _enabled ) {
305
310
this . _cache . clear ( true )
306
311
}
312
+ this . transformMessages ( this . _enabled ? 'encode' : 'restore' )
307
313
}
308
314
309
315
render ( ) {
@@ -313,8 +319,6 @@ class LiveTranslatorManager {
313
319
return
314
320
}
315
321
316
- const re = new RegExp ( ZeroWidthEncoder . PATTERN , 'gm' )
317
-
318
322
const queue = [ this . root ] as Node [ ]
319
323
while ( queue . length > 0 ) {
320
324
const node = queue . pop ( ) as HTMLElement
@@ -324,7 +328,7 @@ class LiveTranslatorManager {
324
328
let cacheKeyParts = [ ]
325
329
326
330
if ( node instanceof Text ) {
327
- const matches = ( node . textContent as string ) . match ( re )
331
+ const matches = ( node . textContent as string ) . match ( ZeroWidthEncoder . PATTERN )
328
332
for ( const match of matches ?? [ ] ) {
329
333
const meta = JSON . parse ( ZeroWidthEncoder . decode ( match ) ) as TranslationMeta
330
334
const badge = createBadge ( meta , this . _options , node )
@@ -336,7 +340,7 @@ class LiveTranslatorManager {
336
340
}
337
341
338
342
const attributes = ( node . attributes ? [ ...node . attributes ] : [ ] )
339
- . map ( ( attribute ) => ( { attribute, match : attribute . value . match ( re ) } ) )
343
+ . map ( ( attribute ) => ( { attribute, match : attribute . value . match ( ZeroWidthEncoder . PATTERN ) } ) )
340
344
. filter ( ( { match } ) => ! ! match )
341
345
for ( const { attribute, match } of attributes ) {
342
346
for ( const m of ( match as RegExpMatchArray ) ) {
0 commit comments