Text Translation with Firebase Cloud Functions onWrite and Angular 4

Fireship · Beginner ·💻 AI-Assisted Coding ·9y ago

Key Takeaways

This video demonstrates how to build a text translation feature in Angular 4 using Firebase Cloud Functions and the Google Translate API, triggered by the onWrite callback from the Firebase database.

Full Transcript

in this episode we're going to build a text translation feature in angular 4 and we're going to do it completely in the background using firebase cloud functions and the Google Translate API this example is going to take a string of English text and translate it to French Spanish and Arabic we're able to make this work using the on write call back from the firebase database which will trigger a cloud function that handles the entire translation process in other words we're going to build a micro service specifically for translation and we're going to trigger this micro service to run every time new data is added to the database the first step is to model the data in this case we just have a collection of translation and each translation has a set of key value pairs where the key is the language and the value is the translated text so the user will enter the text in English and then the cloud function will write the data at every other language that we want to translate the first step is to allow the user to input their tags to the firebase database so we'll start building that in a service the service only needs one function called create translation it starts by pushing the user text to the database then it returns that as an object observable so we can update these or interface asynchronously [Music] now we can start building the component first we define a variable for the users text and then we define another variable for the current translation which is the observable returned to us from the service then we create a handle translation function that will trigger the entire process when the user submits their form from the template I'm also creating a default message helper that will tell the user when the cloud function is running in the background it'd be a good idea to replace this with a loading spinner or possibly a progress bar and the template we start by creating a text area then we use ng model to bind it to the user text variable that we defined in the type script then we create a button and attach the click event to the handle translation function since our current translation isn't observable we can use the async pipe to unwrap that asynchronously in the template in this case we use the question mark to safely access the attributes before they've been defined otherwise an exception would be raised and we also use the or operator to display the default message if the translation is undefined now we can start building the cloud function if this is your first time using cloud functions in angular you'll need to run firebase in it from the command line from there we'll need to install the necessary packages in the cloud function environment I'm using lodash to make the java script a little more readable and also using the request promise library to send the request to the Google Translate API make sure you're in the functions directory and then run npm install and the index.jsp file we start by importing the necessary libraries then we run exports translate to you name our actual cloud function from there we make a reference to the database location we want to trigger the function then we call on write to run this callback whenever new data is added to the database at that particular location next we create a promise for each language that we want to translate we build the promise by making a request for the Google Translate API through this URL here URL self needs the source language the target language and the text itself when we send the request we need to make sure to resolve the promise with the full response if we get a successful 200 response we convert the response to JSON and then use the translated text is the data that we use to update firebase the final step is to update the firebase database as an admin user and if there's an error we want to throw that error to make sure it shows up in that cloud function logs if we go back into the app now we can see that our data is being updated and we're getting translations back from the cloud function pretty quickly looks like about 300 milliseconds for the function to execute [Music] that's it for this episode if you found the video helpful please like and subscribe and if you want to support the channel consider becoming a pro subscriber at angular firebase comm for just a few bucks a month you'll get access to exclusive content as well as free one-on-one project and Colton thanks for watching and I'll see you next time [Music]

Original Description

https://angularfirebase.com/lessons/text-translator-with-firebase-cloud-functions-onwrite-and-angular/ Learn how to translate text in the cloud as a background task with Firebase Cloud Functions and Angular 4.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 15 of 60

1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

Learn how to build a text translation feature in Angular 4 using Firebase Cloud Functions and the Google Translate API. This feature translates text in the background using the onWrite callback from the Firebase database.

Key Takeaways
  1. Model the data in the Firebase database
  2. Create a service to handle user input
  3. Build a component to display the translation
  4. Create a Cloud Function to handle the translation process
  5. Install necessary packages in the Cloud Function environment
  6. Use the Google Translate API to translate text
  7. Update the Firebase database with the translated text
💡 Using the onWrite callback from the Firebase database to trigger a Cloud Function allows for efficient and scalable text translation in the background.

Related AI Lessons

Up next
Azure Security Priorities for 2026: Identity, Governance, AI Security & Zero Trust
Valto Microsoft Specialists
Watch →