Skip to content

Commit

Permalink
fix(admin): improve translation caching
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 12, 2023
1 parent ddad788 commit 61c3a52
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions libs/admin/core/src/composables/language/useLanguage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type Ref, ref} from 'vue';
import {createGlobalState, useMemoize} from '@vueuse/core';
import {type NonTranslatable, type Translation} from '@myparcel-pdk/admin-common';
import {type NonTranslatable, type Translatable, type Translation} from '@myparcel-pdk/admin-common';
import {isOfType} from '@myparcel/ts-utils';
import {useGlobalContext} from '../context';
import {decodeHtmlEntities} from '../../utils';
Expand Down Expand Up @@ -78,22 +78,26 @@ const translate: UseLanguage['translate'] = (translation, replacers) => {

const cache = useCache();

if (memoizedHas(translationKey)) {
if (!cache.has(translationKey)) {
const cacheKey = [translationKey, isOfType<Translatable>(translation, 'args') ? translation.args : undefined]
.filter(Boolean)
.join('|');

if (memoizedHas(translation)) {
if (!cache.has(cacheKey)) {
const translations = memoizedAll();
const translated = decodeHtmlEntities(translations[translationKey]);

cache.set(translationKey, resolveTranslatedString(translated, replacers, translation));
cache.set(cacheKey, resolveTranslatedString(translated, replacers, translation));
}
} else {
const missingKeys = useMissingKeys();
// eslint-disable-next-line no-console
globalLogger.warn(`Missing translation: ${translationKey}`);
missingKeys.value.push(translationKey);
cache.set(translationKey, translationKey);
cache.set(cacheKey, translationKey);
}

return cache.get(translationKey) as string;
return cache.get(cacheKey) as string;
};

const memoizedAll = useMemoize(all);
Expand Down

0 comments on commit 61c3a52

Please sign in to comment.