Skip to content

Commit

Permalink
fix: reload locales on CRUD operations (#3667)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Nov 8, 2023
1 parent e86659a commit 284168e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/api-i18n/src/graphql/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface Locales {
content: string;
default: string;
}

const headerCache: Record<string, Locales> = {};
/**
* Parses "x-i18n-locale" header value (e.g. "default:en-US;content:hr-HR;").
Expand Down Expand Up @@ -46,6 +47,7 @@ interface GetLocaleFromHeadersResult {
acceptLanguageHeader: string | null;
contextLocaleHeader: string | null;
}

const getLocaleFromHeaders = (
headers: Record<string, any>,
localeContext: LocaleKeys = "default"
Expand Down Expand Up @@ -92,16 +94,19 @@ interface I18NCache {
// TODO: refactor this context factory following the newer applications (e.g., api-tenancy, api-security)
const createBaseContextPlugin = () => {
return new ContextPlugin<I18NContext>(async context => {
let locales: I18NLocale[] = [];
if (context.tenancy.getCurrentTenant()) {
const loadLocales = () => {
if (!context.tenancy.getCurrentTenant()) {
return [];
}

const plugin = context.plugins.byName<ContextI18NGetLocales>(
"context-i18n-get-locales"
);
if (!plugin) {
throw new Error('Cannot load locales - missing "context-i18n-get-locales" plugin.');
}
locales = await plugin.resolve({ context });
}
return plugin.resolve({ context });
};

const { plugins } = context;

Expand All @@ -111,7 +116,7 @@ const createBaseContextPlugin = () => {
acceptLanguage: "",
defaultLocale: null,
locale: new Map(),
locales
locales: await loadLocales()
};

const getDefaultLocale = () => {
Expand Down Expand Up @@ -210,6 +215,10 @@ const createBaseContextPlugin = () => {
return getCurrentLocale("content");
};

const reloadLocales = async () => {
__i18n.locales = await loadLocales();
};

context.i18n = {
...(context.i18n || ({} as any)),
getDefaultLocale,
Expand All @@ -220,6 +229,7 @@ const createBaseContextPlugin = () => {
setContentLocale,
getLocales,
getLocale,
reloadLocales,
hasI18NContentPermission: () => hasI18NContentPermission(context),
checkI18NContentPermission
};
Expand Down
12 changes: 12 additions & 0 deletions packages/api-i18n/src/graphql/crud/locales.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ export const createLocalesCrud = (params: CreateLocalesCrudParams): LocalesCRUD
locale: result
});
}

// We want to reload the internally cached locales after a new locale is created.
await context.i18n.reloadLocales();

await onLocaleAfterCreate.publish({
context,
locale: result,
Expand Down Expand Up @@ -251,6 +255,10 @@ export const createLocalesCrud = (params: CreateLocalesCrudParams): LocalesCRUD
locale
});
}

// We want to reload the internally cached locales after a locale is updated.
await context.i18n.reloadLocales();

await onLocaleAfterUpdate.publish({
context,
locale: result,
Expand Down Expand Up @@ -300,6 +308,10 @@ export const createLocalesCrud = (params: CreateLocalesCrudParams): LocalesCRUD
await storageOperations.delete({
locale
});

// We want to reload the internally cached locales after a locale is deleted.
await context.i18n.reloadLocales();

await onLocaleAfterDelete.publish({
context,
locale,
Expand Down
1 change: 1 addition & 0 deletions packages/api-i18n/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface I18NContextObject {
setContentLocale: (locale: I18NLocale) => void;
getLocales: () => I18NLocale[];
getLocale: (code: string) => I18NLocale | undefined;
reloadLocales: () => Promise<void>;
locales: LocalesCRUD;
system: SystemCRUD;
hasI18NContentPermission: () => Promise<boolean>;
Expand Down

0 comments on commit 284168e

Please sign in to comment.