Skip to content

Commit

Permalink
fix(api-i18n): set first locale into context
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Apr 26, 2024
1 parent b428fa5 commit 7c49c5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
6 changes: 6 additions & 0 deletions packages/api-i18n/src/graphql/crud/locales.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ export const createLocalesCrud = (params: CreateLocalesCrudParams): LocalesCRUD
// We want to reload the internally cached locales after a new locale is created.
await context.i18n.reloadLocales();

// If the new locale is the only locale in the system, make it the current request locale.
if (context.i18n.getLocales().length === 1) {
context.i18n.setCurrentLocale("default", locale);
context.i18n.setCurrentLocale("content", locale);
}

await onLocaleAfterCreate.publish({
context,
locale: result,
Expand Down
49 changes: 26 additions & 23 deletions packages/api-i18n/src/graphql/crud/system.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,34 @@ export const createSystemCrud = (params: CreateSystemCrudParams): SystemCRUD =>
}
},
async installSystem(this: SystemCRUD, { code }) {
const identity = context.security.getIdentity();
if (!identity) {
throw new NotAuthorizedError();
}
const { i18n } = context;
const version = await this.getSystemVersion();
if (version) {
throw new WebinyError("I18N is already installed.", "INSTALL_ERROR", {
version
});
}
await onSystemBeforeInstall.publish({
code
});

/**
* `i18n` installation needs to run with authorization disabled, because permission loading needs a locale.
* Since the locale doesn't exist yet, the system would assume the user has no permissions, and throw an error.
*
* @see packages/api-security/src/utils/getPermissionsFromSecurityGroupsForLocale.ts
*/
await context.security.withoutAuthorization(async () => {
return i18n.locales.createLocale({
code,
default: true
});
});
const identity = context.security.getIdentity();

if (!identity) {
throw new NotAuthorizedError();
}

const { i18n } = context;

const version = await this.getSystemVersion();

if (version) {
throw new WebinyError("I18N is already installed.", "INSTALL_ERROR", {
version
});
}
await onSystemBeforeInstall.publish({ code });

await i18n.locales.createLocale({ code, default: true });

await this.setSystemVersion(context.WEBINY_VERSION);
await onSystemAfterInstall.publish({
code
await this.setSystemVersion(context.WEBINY_VERSION);
await onSystemAfterInstall.publish({ code });
});
}
};
Expand Down

0 comments on commit 7c49c5b

Please sign in to comment.