@@ -34,9 +34,11 @@ import {
34
34
isEmptyObject ,
35
35
generateFormatCacheKey ,
36
36
generateCodeFrame ,
37
+ escapeHtml ,
37
38
inBrowser ,
38
39
mark ,
39
- measure
40
+ measure ,
41
+ isObject
40
42
} from '../utils'
41
43
import { DevToolsTimelineEvents } from '../debugger/constants'
42
44
@@ -83,6 +85,9 @@ const isMessageFunction = <T>(val: unknown): val is MessageFunction<T> =>
83
85
*
84
86
* // suppress localize fallback warning option, override context.fallbackWarn
85
87
* translate(context, 'foo.bar', { name: 'kazupon' }, { fallbackWarn: false })
88
+ *
89
+ * // escape parameter option, override context.escapeParameter
90
+ * translate(context, 'foo.bar', { name: 'kazupon' }, { escapeParameter: true })
86
91
*/
87
92
88
93
/** @internal */
@@ -94,6 +99,7 @@ export type TranslateOptions = {
94
99
locale ?: Locale
95
100
missingWarn ?: boolean
96
101
fallbackWarn ?: boolean
102
+ escapeParameter ?: boolean
97
103
}
98
104
99
105
// `translate` function overloads
@@ -210,6 +216,10 @@ export function translate<Messages, Message = string>(
210
216
? options . fallbackWarn
211
217
: context . fallbackWarn
212
218
219
+ const escapeParameter = isBoolean ( options . escapeParameter )
220
+ ? options . escapeParameter
221
+ : context . escapeParameter
222
+
213
223
// prettier-ignore
214
224
const defaultMsgOrKey : string =
215
225
isString ( options . default ) || isBoolean ( options . default ) // default by function option
@@ -222,6 +232,9 @@ export function translate<Messages, Message = string>(
222
232
const enableDefaultMsg = fallbackFormat || defaultMsgOrKey !== ''
223
233
const locale = isString ( options . locale ) ? options . locale : context . locale
224
234
235
+ // escape params
236
+ escapeParameter && escapeParams ( options )
237
+
225
238
// resolve message format
226
239
// eslint-disable-next-line prefer-const
227
240
let [ format , targetLocale , message ] = resolveMessageFormat (
@@ -289,6 +302,20 @@ export function translate<Messages, Message = string>(
289
302
return postTranslation ? postTranslation ( messaged ) : messaged
290
303
}
291
304
305
+ function escapeParams ( options : TranslateOptions ) {
306
+ if ( isArray ( options . list ) ) {
307
+ options . list = options . list . map ( item =>
308
+ isString ( item ) ? escapeHtml ( item ) : item
309
+ )
310
+ } else if ( isObject ( options . named ) ) {
311
+ Object . keys ( options . named ) . forEach ( key => {
312
+ if ( isString ( options . named ! [ key ] ) ) {
313
+ options . named ! [ key ] = escapeHtml ( options . named ! [ key ] as string )
314
+ }
315
+ } )
316
+ }
317
+ }
318
+
292
319
function resolveMessageFormat < Messages , Message > (
293
320
context : RuntimeTranslationContext < Messages , Message > ,
294
321
key : string ,
0 commit comments