Skip to content

Commit

Permalink
fix(core): Correctly derive request language from active Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Apr 6, 2020
1 parent 0120202 commit aae4aa9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/api/common/request-context.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class RequestContextService {
const apiType = getApiType(info);

const hasOwnerPermission = !!requiredPermissions && requiredPermissions.includes(Permission.Owner);
const languageCode = this.getLanguageCode(req);
const languageCode = this.getLanguageCode(req, channel);
const user = session && (session as AuthenticatedSession).user;
const isAuthorized = this.userHasRequiredPermissionsOnChannel(requiredPermissions, channel, user);
const authorizedAsOwnerOnly = !isAuthorized && hasOwnerPermission;
Expand Down Expand Up @@ -65,8 +65,12 @@ export class RequestContextService {
return channelToken;
}

private getLanguageCode(req: Request): LanguageCode | undefined {
return (req.query && req.query.languageCode) || this.configService.defaultLanguageCode;
private getLanguageCode(req: Request, channel: Channel): LanguageCode | undefined {
return (
(req.query && req.query.languageCode) ??
channel.defaultLanguageCode ??
this.configService.defaultLanguageCode
);
}

private isAuthenticatedSession(session?: Session): session is AuthenticatedSession {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/service/helpers/utils/translate-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export function translateEntity<T extends Translatable & VendureEntity>(
if (!translation && languageCode !== DEFAULT_LANGUAGE_CODE) {
translation = translatable.translations.find((t) => t.languageCode === DEFAULT_LANGUAGE_CODE);
}
if (!translation) {
// If we cannot find any suitable translation, just return the first one to at least
// prevent graphql errors when returning the entity.
translation = translatable.translations[0];
}
}

if (!translation) {
Expand Down

0 comments on commit aae4aa9

Please sign in to comment.