Skip to content

Commit

Permalink
[FIX] Autotranslate method should respect setting (#26549)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman authored Aug 25, 2022
1 parent 7823746 commit 5f20d50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 11 additions & 6 deletions apps/meteor/app/autotranslate/client/lib/autotranslate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const AutoTranslate = {
initialized: false,
providersMetadata: {} as { [providerNamer: string]: { name: string; displayName: string } },
messageIdsToWait: {} as { [messageId: string]: string },
supportedLanguages: [] as ISupportedLanguage[],
supportedLanguages: [] as ISupportedLanguage[] | undefined,

findSubscriptionByRid: mem((rid) => Subscriptions.findOne({ rid })),

Expand All @@ -43,7 +43,7 @@ export const AutoTranslate = {
}
const language = (subscription?.autoTranslateLanguage || userLanguage || window.defaultUserLanguage?.()) as string;
if (language.indexOf('-') !== -1) {
if (!this.supportedLanguages.some((supportedLanguage) => supportedLanguage.language === language)) {
if (!(this.supportedLanguages || []).some((supportedLanguage) => supportedLanguage.language === language)) {
return language.slice(0, 2);
}
}
Expand Down Expand Up @@ -100,10 +100,15 @@ export const AutoTranslate = {

c.stop();

[this.providersMetadata, this.supportedLanguages] = await Promise.all([
callWithErrorHandling('autoTranslate.getProviderUiMetadata'),
callWithErrorHandling('autoTranslate.getSupportedLanguages', 'en'),
]);
try {
[this.providersMetadata, this.supportedLanguages] = await Promise.all([
callWithErrorHandling('autoTranslate.getProviderUiMetadata'),
callWithErrorHandling('autoTranslate.getSupportedLanguages', 'en'),
]);
} catch (e: unknown) {
// Avoid unwanted error message on UI when autotranslate is disabled while fetching data
console.error((e as Error).message);
}
});

Subscriptions.find().observeChanges({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';

import { hasPermission } from '../../../authorization/server';
import { TranslationProviderRegistry } from '..';
import { settings } from '../../../settings/server';

Meteor.methods({
'autoTranslate.getSupportedLanguages'(targetLanguage) {
if (!settings.get('AutoTranslate_Enabled')) {
throw new Meteor.Error('error-autotranslate-disabled', 'Auto-Translate is disabled');
}

const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
Expand Down

0 comments on commit 5f20d50

Please sign in to comment.