Services and API

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

GitHub code for service

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)
    });

 

Discussion
Cool! I am a full-stack developer with 8+ years of experience, passionate about the JavaScript ecosystem. I have a bachelor's degree in computer science. I am most skilled and passionate about Angular and React. I am able to provide meaningful contributions to the design, installation, testing, and maintenance of any type of software system. I like to challenge myself in new roles. I have built and successfully delivered applications in multiple domains. In my free time, I like to write blogs related to software development. I have the pleasure of working on exciting projects across industries. The applications that I developed were scalable, deployable, and maintainable. I have a vision of providing cutting-edge web solutions and services to enterprises. Developed zero-to-one products. I'm currently looking for passionate people who want to make some impact. feel free to reach out—I’d be happy to assist in any way I can! github.com/sunny7899

Which is the next one in the series?

1

Asynchronous Nature of JS

4

2