feat: 🎸 add warnOnMissingMessages

This commit is contained in:
Christian Kaisermann 2019-11-28 14:59:52 -03:00
parent 9d636694b1
commit efbe793a0f
2 changed files with 27 additions and 25 deletions

View File

@ -13,6 +13,7 @@ interface Options {
initialLocale: string initialLocale: string
formats: Formats formats: Formats
loadingDelay: number loadingDelay: number
warnOnMissingMessages: boolean
} }
export const defaultFormats: Formats = { export const defaultFormats: Formats = {
@ -51,6 +52,7 @@ export const defaultOptions: Options = {
initialLocale: null, initialLocale: null,
loadingDelay: 200, loadingDelay: 200,
formats: defaultFormats, formats: defaultFormats,
warnOnMissingMessages: true,
} }
const options: Options = defaultOptions const options: Options = defaultOptions
@ -60,30 +62,26 @@ export function getOptions() {
} }
export function init(opts: ConfigureOptions) { export function init(opts: ConfigureOptions) {
const fallbackLocale = (options.fallbackLocale = opts.fallbackLocale) const { formats, ...rest } = opts
const initialLocale = opts.initialLocale const initialLocale = opts.initialLocale
? typeof opts.initialLocale === 'string' ? typeof opts.initialLocale === 'string'
? opts.initialLocale ? opts.initialLocale
: getClientLocale(opts.initialLocale) || fallbackLocale : getClientLocale(opts.initialLocale) || opts.fallbackLocale
: fallbackLocale : opts.fallbackLocale
$locale.set(initialLocale) Object.assign(options, rest, { initialLocale })
options.initialLocale = initialLocale
if (opts.formats) { if (formats) {
if ('number' in opts.formats) { if ('number' in formats) {
Object.assign(options.formats.number, opts.formats.number) Object.assign(options.formats.number, formats.number)
} }
if ('date' in opts.formats) { if ('date' in formats) {
Object.assign(options.formats.date, opts.formats.date) Object.assign(options.formats.date, formats.date)
} }
if ('time' in opts.formats) { if ('time' in formats) {
Object.assign(options.formats.time, opts.formats.time) Object.assign(options.formats.time, formats.time)
} }
} }
if (opts.loadingDelay != null) { return $locale.set(initialLocale)
options.loadingDelay = opts.loadingDelay
}
} }

View File

@ -10,6 +10,7 @@ import {
getDateFormatter, getDateFormatter,
getNumberFormatter, getNumberFormatter,
} from '../includes/formatters' } from '../includes/formatters'
import { getOptions } from '../configs'
import { $dictionary } from './dictionary' import { $dictionary } from './dictionary'
import { getCurrentLocale, getRelatedLocalesOf, $locale } from './locale' import { getCurrentLocale, getRelatedLocalesOf, $locale } from './locale'
@ -31,15 +32,18 @@ const formatMessage: Formatter = (id, options = {}) => {
const message = lookupMessage(id, locale) const message = lookupMessage(id, locale)
if (!message) { if (!message) {
if (getOptions().warnOnMissingMessages) {
// istanbul ignore next
console.warn( console.warn(
`[svelte-i18n] The message "${id}" was not found in "${getRelatedLocalesOf( `[svelte-i18n] The message "${id}" was not found in "${getRelatedLocalesOf(
locale locale
).join('", "')}". ${ ).join('", "')}".${
hasLocaleQueue(getCurrentLocale()) hasLocaleQueue(getCurrentLocale())
? `\n\nNote: there are at least one loader still registered to this locale that wasn't executed.` ? `\n\nNote: there are at least one loader still registered to this locale that wasn't executed.`
: '' : ''
}` }`
) )
}
return defaultValue || id return defaultValue || id
} }