2021-07-05 04:19:04 +01:00
|
|
|
import { get } from '../src/routes/_utils/lodash-lite.js'
|
|
|
|
import { DEFAULT_LOCALE, LOCALE } from '../src/routes/_static/intl.js'
|
2021-04-12 03:40:24 +01:00
|
|
|
|
2021-07-05 04:19:04 +01:00
|
|
|
import enUS from '../src/intl/en-US.js'
|
|
|
|
import fr from '../src/intl/fr.js'
|
|
|
|
import de from '../src/intl/de.js'
|
|
|
|
|
|
|
|
// TODO: make it so we don't have to explicitly list these out
|
|
|
|
const locales = {
|
|
|
|
'en-US': enUS,
|
|
|
|
fr,
|
|
|
|
de
|
|
|
|
}
|
|
|
|
|
|
|
|
const intl = locales[LOCALE]
|
|
|
|
const defaultIntl = locales[DEFAULT_LOCALE]
|
2021-04-12 03:40:24 +01:00
|
|
|
|
|
|
|
export function warningOrError (message) { // avoid crashing the whole server on `yarn dev`
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
throw new Error(message)
|
|
|
|
}
|
|
|
|
console.warn(message)
|
|
|
|
return '(Placeholder intl string)'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getIntl (path) {
|
2021-07-05 04:19:04 +01:00
|
|
|
path = path.split('.')
|
2021-04-12 03:40:24 +01:00
|
|
|
const res = get(intl, path, get(defaultIntl, path))
|
|
|
|
if (typeof res !== 'string') {
|
|
|
|
return warningOrError('Unknown intl string: ' + JSON.stringify(path))
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
export function trimWhitespace (str) {
|
|
|
|
return str.trim().replace(/\s+/g, ' ')
|
|
|
|
}
|