2020-11-29 14:13:27 -08:00
|
|
|
import interpret from 'format-message-interpret'
|
2021-07-04 20:19:04 -07:00
|
|
|
import { LOCALE } from '../_static/intl.js'
|
|
|
|
import { mark, stop } from './marks.js'
|
2020-11-29 14:13:27 -08:00
|
|
|
|
|
|
|
function doFormatIntl (ast, values) {
|
|
|
|
return interpret(ast, LOCALE)(values).trim().replace(/\s+/g, ' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatIntl (ast, values) {
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
// useful error debugging for dev mode
|
|
|
|
if (typeof ast === 'string') {
|
|
|
|
throw new Error('bad ast: ' + ast)
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return doFormatIntl(ast, values)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
throw new Error('bad ast or values : ' + ast + ' ' + values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mark('formatIntl')
|
|
|
|
const res = doFormatIntl(ast, values)
|
|
|
|
stop('formatIntl')
|
|
|
|
return res
|
|
|
|
}
|