Services and API
In this section, we will use angular service and this API to actually translate our English text to Shakespeare text.

Let's use Angular CLI to create a services folder and translater.ts service
ng g service services/translater
Import HttpClient and declare in app.module.ts
import { HttpClient } from '@angular/common/http'
services/translater.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
@Injectable({
providedIn: 'root'
})
export class TranslaterService {
constructor(private http: HttpClient) { }
gettranslation(englishText: String) {
var api = `https://api.funtranslations.com/translate/shakespeare.json?text=${englishText}`;
return this.http.get<any>(api);
}
}
In translater.component.ts let us import the service and subscribe to it.
import { TranslaterService } from '../services/translater.service';
Now let us complete clicked() function
constructor(private translate: TranslaterService) { } //instantiating the service
clicked(): void {
this.translate.gettranslation(this.englishText).subscribe({
next: result => this.shakespeareText = result.contents.translated,
error: err => console.log(err)
});
4
