fix: 🐛 lookup message not caching correctly

It was only caching lookups of fallback locales. If a message was found
in the passed locale, it wouldn't be cached.
This commit is contained in:
Christian Kaisermann 2020-01-08 11:28:44 -03:00
parent f8b0fe8bbe
commit b9b6fa41ff
5 changed files with 35 additions and 21 deletions

View File

@ -10,14 +10,24 @@ const addToCache = (path: string, locale: string, message: string) => {
return message
}
export const lookupMessage = (path: string, locale: string): string => {
const searchForMessage = (path: string, locale: string): string => {
if (locale == null) return null
if (locale in lookupCache && path in lookupCache[locale]) {
return lookupCache[locale][path]
}
const message = getMessageFromDictionary(locale, path)
if (message) return message
return addToCache(path, locale, lookupMessage(path, getFallbackOf(locale)))
return searchForMessage(path, getFallbackOf(locale))
}
export const lookup = (path: string, locale: string) => {
if (locale in lookupCache && path in lookupCache[locale]) {
return lookupCache[locale][path]
}
const message = searchForMessage(path, locale)
if (message) {
return addToCache(path, locale, message)
}
return null
}

View File

@ -1,7 +1,7 @@
import { derived } from 'svelte/store'
import { Formatter, MessageObject } from '../types'
import { lookupMessage } from '../includes/lookup'
import { lookup } from '../includes/lookup'
import { hasLocaleQueue } from '../includes/loaderQueue'
import { capital, upper, lower, title } from '../includes/utils'
import {
@ -29,7 +29,7 @@ const formatMessage: Formatter = (id, options = {}) => {
)
}
const message = lookupMessage(id, locale)
const message = lookup(id, locale)
if (!message) {
if (getOptions().warnOnMissingMessages) {

View File

@ -28,6 +28,7 @@ export function getFallbackOf(locale: string) {
if (fallbackLocale && !isRelatedLocale(locale, fallbackLocale)) {
return fallbackLocale
}
return null
}

View File

@ -1,4 +1,4 @@
import { lookupMessage, lookupCache } from '../../../src/client/includes/lookup'
import { lookup, lookupCache } from '../../../src/client/includes/lookup'
import { $dictionary, addMessages } from '../../../src/client/stores/dictionary'
beforeEach(() => {
@ -6,43 +6,46 @@ beforeEach(() => {
})
test('returns null if no locale was passed', () => {
expect(lookupMessage('message.id', undefined)).toBe(null)
expect(lookupMessage('message.id', null)).toBe(null)
expect(lookup('message.id', undefined)).toBe(null)
expect(lookup('message.id', null)).toBe(null)
})
test('gets a shallow message of a locale dictionary', () => {
addMessages('en', { field: 'name' })
expect(lookupMessage('field', 'en')).toBe('name')
expect(lookup('field', 'en')).toBe('name')
})
test('gets a deep message of a locale dictionary', () => {
addMessages('en', { deep: { field: 'lastname' } })
expect(lookupMessage('deep.field', 'en')).toBe('lastname')
expect(lookup('deep.field', 'en')).toBe('lastname')
})
test('gets a message from the fallback dictionary', () => {
addMessages('en', { field: 'name' })
expect(lookupMessage('field', 'en-US')).toBe('name')
expect(lookup('field', 'en-US')).toBe('name')
})
test('caches found messages by locale', () => {
addMessages('en', { field: 'name' })
addMessages('pt', { field: 'nome' })
lookupMessage('field', 'en-US')
lookupMessage('field', 'pt-BR')
lookup('field', 'en-US')
lookup('field', 'pt')
expect(lookupCache).toMatchObject({
'en-US': { field: 'name' },
'pt-BR': { field: 'nome' },
pt: { field: 'nome' },
})
})
test("doesn't cache falsy messages", () => {
addMessages('en', { field: 'name' })
addMessages('pt', { field: 'nome' })
lookupMessage('field_2', 'en-US')
lookupMessage('field_2', 'pt-BR')
lookup('field_2', 'en-US')
lookup('field_2', 'pt')
expect(lookupCache).not.toMatchObject({
'en-US': { field_2: 'name' },
'pt-BR': { field_2: 'nome' },
pt: { field_2: 'nome' },
})
})

View File

@ -1,6 +1,6 @@
import { get } from 'svelte/store'
import { lookupMessage } from '../../../src/client/includes/lookup'
import { lookup } from '../../../src/client/includes/lookup'
import {
isFallbackLocaleOf,
getFallbackOf,
@ -132,5 +132,5 @@ test('should flush the queue of the locale when changing the store value', async
await $locale.set('en')
expect(hasLocaleQueue('en')).toBe(false)
expect(lookupMessage('foo', 'en')).toBe('Foo')
expect(lookup('foo', 'en')).toBe('Foo')
})