From b9b6fa41ffd99b89fc117c44a5bc636335c63632 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Wed, 8 Jan 2020 11:28:44 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20lookup=20message=20not=20?= =?UTF-8?q?caching=20correctly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was only caching lookups of fallback locales. If a message was found in the passed locale, it wouldn't be cached. --- src/client/includes/lookup.ts | 20 +++++++++++++++----- src/client/stores/format.ts | 4 ++-- src/client/stores/locale.ts | 1 + test/client/includes/lookup.test.ts | 27 +++++++++++++++------------ test/client/stores/locale.test.ts | 4 ++-- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/client/includes/lookup.ts b/src/client/includes/lookup.ts index 0860ac1..4ca2e0f 100644 --- a/src/client/includes/lookup.ts +++ b/src/client/includes/lookup.ts @@ -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 } diff --git a/src/client/stores/format.ts b/src/client/stores/format.ts index 9628a1b..aa25e35 100644 --- a/src/client/stores/format.ts +++ b/src/client/stores/format.ts @@ -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) { diff --git a/src/client/stores/locale.ts b/src/client/stores/locale.ts index a18b7f1..50cf267 100644 --- a/src/client/stores/locale.ts +++ b/src/client/stores/locale.ts @@ -28,6 +28,7 @@ export function getFallbackOf(locale: string) { if (fallbackLocale && !isRelatedLocale(locale, fallbackLocale)) { return fallbackLocale } + return null } diff --git a/test/client/includes/lookup.test.ts b/test/client/includes/lookup.test.ts index 273ee79..bb647fc 100644 --- a/test/client/includes/lookup.test.ts +++ b/test/client/includes/lookup.test.ts @@ -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' }, }) }) diff --git a/test/client/stores/locale.test.ts b/test/client/stores/locale.test.ts index 1a44a20..bd0f923 100644 --- a/test/client/stores/locale.test.ts +++ b/test/client/stores/locale.test.ts @@ -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') })