added comments
This commit is contained in:
@@ -1,24 +1,29 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { environment } from '../../environments/environment';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpParams} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {environment} from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DeepLService {
|
||||
|
||||
// Define the base URL for the DeepL API and the API key, taken from the environment configuration
|
||||
private apiUrl = 'https://api-free.deepl.com/v2/translate';
|
||||
private apiKey = environment.deepLApiKey;
|
||||
|
||||
constructor(private http: HttpClient) { }
|
||||
constructor(private http: HttpClient) {
|
||||
}
|
||||
|
||||
// Define the method for translating text, which takes the text to be translated and the target language as parameters
|
||||
translate(text: string, targetLang: string): Observable<any> {
|
||||
// Define the parameters to be passed to the API, including the API key and the text and target language to be translated
|
||||
const params = new HttpParams()
|
||||
.set('auth_key', this.apiKey)
|
||||
.set('text', text)
|
||||
.set('target_lang', targetLang);
|
||||
|
||||
// Make a POST request to the API using the HttpClient and the defined parameters, and return the response as an Observable
|
||||
return this.http.post(this.apiUrl, params);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,11 @@ import {listTranslations} from "../interface/translations";
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ReadTranslateJsonService {
|
||||
private homeTranslations: homeTranslations = {} as homeTranslations;
|
||||
private listTranslation: listTranslations = {} as listTranslations;
|
||||
private homeTranslations: homeTranslations = {} as homeTranslations; // declares a private object to hold the home translations
|
||||
private listTranslation: listTranslations = {} as listTranslations; // declares a private object to hold the list translations
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
// loads the home translations from the assets file for the English language
|
||||
this.http.get<homeTranslations>('assets/i18n/home/en.json').subscribe(data => {
|
||||
this.homeTranslations.alertMessage = data.alertMessage;
|
||||
this.homeTranslations.translate = data.translate;
|
||||
@@ -20,6 +21,7 @@ export class ReadTranslateJsonService {
|
||||
console.log("data loaded", this.homeTranslations)
|
||||
});
|
||||
|
||||
// loads the list translations from the assets file for the English language
|
||||
this.http.get<listTranslations>('assets/i18n/list/en.json').subscribe(data => {
|
||||
this.listTranslation.translate = data.translate;
|
||||
this.listTranslation.distance = data.distance;
|
||||
@@ -29,10 +31,12 @@ export class ReadTranslateJsonService {
|
||||
});
|
||||
}
|
||||
|
||||
// returns the home translations object
|
||||
getHomeTranslations(): homeTranslations {
|
||||
return this.homeTranslations;
|
||||
}
|
||||
|
||||
// returns the list translations object
|
||||
getListTransaltions(): listTranslations {
|
||||
return this.listTranslation;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,11 @@ import {Injectable} from "@angular/core";
|
||||
export class TranslateService {
|
||||
constructor(private deepLService: DeepLService) {}
|
||||
|
||||
// Method for translating the given input to the given language
|
||||
async getData(input: string, lang: string): Promise<string> {
|
||||
// Translating the input using the DeepLService and waiting for the response
|
||||
const response = await this.deepLService.translate(input, lang).toPromise();
|
||||
// Returning the translated text from the response
|
||||
return response.translations[0].text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user