Browse Source

fix(core): Correctly derive request language from active Channel

Michael Bromley 5 years ago
parent
commit
aae4aa977d

+ 7 - 3
packages/core/src/api/common/request-context.service.ts

@@ -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;
@@ -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 {

+ 5 - 0
packages/core/src/service/helpers/utils/translate-entity.ts

@@ -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) {