Skip to content

Commit

Permalink
update translate
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcibotari committed Mar 1, 2025
1 parent fe0251b commit 08c216a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 6 additions & 1 deletion functions/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpsError, onCall } from 'firebase-functions/v2/https';
import { canPerform } from './utils/security-utils';
import { TranslateData, UserPermission } from './models';
import { translateCloud } from './services/translate.service';
import { isEmulatorEnabled } from './config';

export const translate = onCall<TranslateData>(async request => {
logger.info('[translate] data: ' + JSON.stringify(request.data));
Expand All @@ -11,5 +12,9 @@ export const translate = onCall<TranslateData>(async request => {
if (!canPerform(UserPermission.TRANSLATION_UPDATE, request.auth) || !canPerform(UserPermission.CONTENT_UPDATE, request.auth)) {
throw new HttpsError('permission-denied', 'permission-denied');
}
return await translateCloud(content, sourceLocale, targetLocale);
if (isEmulatorEnabled) {
return `${content} : ${sourceLocale} -> ${targetLocale}`;
} else {
return await translateCloud(content, sourceLocale, targetLocale);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export class TranslationsComponent implements OnInit {
.subscribe({
next: value => {
// make sure the component is updated
this.translateValue.set('');
this.translateValue.set(undefined);
this.notificationService.info('Translated');
this.translateValue.set(value);
},
Expand All @@ -433,6 +433,7 @@ export class TranslationsComponent implements OnInit {
]);
},
complete: () => {
console.log('complete');
setTimeout(() => {
this.isTranslateLoading.set(false);
this.cd.markForCheck();
Expand Down
10 changes: 3 additions & 7 deletions src/app/shared/services/translate.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Observable } from 'rxjs';
import { traceUntilFirst } from '@angular/fire/performance';
import { Functions, httpsCallableData } from '@angular/fire/functions';
import { TranslateData } from '@shared/models/translate.model';
import { environment } from '../../../environments/environment';
import { tap } from 'rxjs/operators';

@Injectable()
export class TranslateService {
constructor(private readonly functions: Functions) {}

translate(data: TranslateData): Observable<string | undefined> {
const translate = httpsCallableData<TranslateData, string | undefined>(this.functions, 'translate');
if (environment.production) {
return translate(data).pipe(traceUntilFirst('Functions:Translate:translate'));
} else {
return of(`${data.content} : ${data.sourceLocale} -> ${data.targetLocale}`);
}
return translate(data).pipe(tap(console.log), traceUntilFirst('Functions:Translate:translate'));
}
}

0 comments on commit 08c216a

Please sign in to comment.