1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-13 18:57:02 +01:00
Pinafore-Web-Client-Frontend/bin/getIntl.js
Nolan Lawson 35fa3bdf9f fix!: remove esm package, use native Node ES modules
BREAKING CHANGE: Node v12.20+, v14.14+, or v16.0+ is required
2021-07-04 16:46:39 -07:00

38 lines
984 B
JavaScript

import { get } from '../src/routes/_utils/lodash-lite.js'
import { DEFAULT_LOCALE, LOCALE } from '../src/routes/_static/intl.js'
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]
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) {
path = path.split('.')
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, ' ')
}