Skip to content

Commit

Permalink
fix: 🐛 lookup message not caching correctly
Browse files Browse the repository at this point in the history
It was only caching lookups of fallback locales. If a message was found
in the passed locale, it wouldn't be cached.
  • Loading branch information
kaisermann committed Jan 8, 2020
1 parent f8b0fe8 commit bb8c68f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
18 changes: 14 additions & 4 deletions src/client/includes/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

const message = getMessageFromDictionary(locale, path)
if (message) return message

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 = getMessageFromDictionary(locale, path)
if (message) return message
const message = searchForMessage(path, locale)
if (message) {
return addToCache(path, locale, message)
}

return addToCache(path, locale, lookupMessage(path, getFallbackOf(locale)))
return null
}
4 changes: 2 additions & 2 deletions src/client/stores/format.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -29,7 +29,7 @@ const formatMessage: Formatter = (id, options = {}) => {
)
}

const message = lookupMessage(id, locale)
const message = lookup(id, locale)

if (!message) {
if (getOptions().warnOnMissingMessages) {
Expand Down
1 change: 1 addition & 0 deletions src/client/stores/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function getFallbackOf(locale: string) {
if (fallbackLocale && !isRelatedLocale(locale, fallbackLocale)) {
return fallbackLocale
}

return null
}

Expand Down
27 changes: 15 additions & 12 deletions test/client/includes/lookup.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
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(() => {
$dictionary.set({})
})

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' },
})
})
4 changes: 2 additions & 2 deletions test/client/stores/locale.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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')
})

0 comments on commit bb8c68f

Please sign in to comment.