Fireship · @fireship

Text Translation with Firebase Cloud Functions onWrite and Angular 4

Uploaded June 25, 2017 · Length 6:00

Text Translation with Firebase Cloud Functions onWrite and Angular 4

Summary

Building a text translation feature in Angular 4 using Firebase Cloud Functions and Google Translate API.

  • angular 4
  • firebase cloud functions
  • google translate api
  • text translation
  • on write callback
  • angular firebase

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    in this episode we're going to build a

  2. 0:02

    text translation feature in angular 4

  3. 0:04

    and we're going to do it completely in

  4. 0:06

    the background using firebase cloud

  5. 0:08

    functions and the Google Translate API

  6. 0:12

    this example is going to take a string

  7. 0:15

    of English text and translate it to

  8. 0:17

    French Spanish and Arabic we're able to

  9. 0:22

    make this work using the on write call

  10. 0:24

    back from the firebase database which

  11. 0:26

    will trigger a cloud function that

  12. 0:28

    handles the entire translation process

  13. 0:30

    in other words we're going to build a

  14. 0:32

    micro service specifically for

  15. 0:34

    translation and we're going to trigger

  16. 0:36

    this micro service to run every time new

  17. 0:38

    data is added to the database the first

  18. 0:43

    step is to model the data in this case

  19. 0:45

    we just have a collection of translation

  20. 0:47

    and each translation has a set of key

  21. 0:51

    value pairs where the key is the

  22. 0:53

    language and the value is the translated

  23. 0:56

    text so the user will enter the text in

  24. 0:59

    English and then the cloud function will

  25. 1:01

    write the data at every other language

  26. 1:03

    that we want to translate the first step

  27. 1:05

    is to allow the user to input their tags

  28. 1:08

    to the firebase database so we'll start

  29. 1:10

    building that in a service the service

  30. 1:14

    only needs one function called create

  31. 1:15

    translation it starts by pushing the

  32. 1:18

    user text to the database then it

  33. 1:20

    returns that as an object observable so

  34. 1:23

    we can update these or interface

  35. 1:24

    asynchronously

  36. 1:27

    [Music]

  37. 1:40

    now we can start building the component

  38. 1:45

    first we define a variable for the users

  39. 1:47

    text and then we define another variable

  40. 1:50

    for the current translation which is the

  41. 1:52

    observable returned to us from the

  42. 1:54

    service then we create a handle

  43. 1:56

    translation function that will trigger

  44. 1:59

    the entire process when the user submits