Open In App

Language Translator using React

Last Updated : 29 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Translator is a web application software tool that facilitate the translation of text from one language to another language using ReactJS. This application allows users to give input Text and select input and output language Format from the list of languages and uses Microsoft Translator API. Users can also reverse the source and target languages and clear the input text. The component provides a user-friendly interface for language translation, with dynamic updates and error handling. This application is implemented using ReactJS and has a simple and responsive UI.

Final Output Preview:

gfg
Preview Image of Translator

Prerequisites and Technologies:

Approach/Functionalities:

This project basically implements functional components and manages states. This application uses the Microsoft Translator API for language translation, making POST requests with the input text and language preferences. Upon receiving the response, the translated text is updated in the component's state, and loading spinners are displayed during the API call. The component's interface allows users to select languages, input text, translate, and reverse language selection. If API call is rejected then it shows an error message during translation.

Steps to create the application:

Step 1: Set up React project using the command

npx create-react-app <<name of project>>

Step 2: Navigate to the project folder using

cd <<Name_of_project>>

Step 3: Create a folder “components” and add three new files in it and name them as Translator.js, language.json and Translator.css.

Project Structure:

Screenshot-2023-09-28-060912
Project Structure

The updated dependencies in package.json will look like this:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: Write the following code in respective files

  • App.js: This file imports the Translator components and exports it.
  • Translator.js: This file contains a logic to allow users to insert the input Text and select the input and output language format and uses the API to get the translated Text.
  • language.json: This file contains a list of languages and their corresponding names.
  • Translator.css: This file contains the design of the Translator elements.
CSS
/* Translator.css */

body {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.container {
  display: flex;
  align-items: center;
  min-height: 100vh;
  flex-direction: column;
  box-sizing: border-box;
  padding-top: 50px;
}

.row1 {
  width: calc(100% - 10px);
  max-width: 650px;
}

select {
  width: calc(50% - 20px);
  line-height: 40px;
  font-size: 14px;
  border: 1px solid #dadce0;
  border-radius: 8px;
  height: 40px;
  padding-left: 18px;
  position: relative;
  color: #1a0dab;
  cursor: pointer;
  outline: none;
  user-select: none;
}

.reversesvg {
  width: 20px;
  color: rgba(0, 0, 0, 0.26);
  line-height: 40px;
  opacity: 0.5;
  vertical-align: middle;
  cursor: pointer;
  margin: 0px 10px;
}

option {
  color: #202124;
}

.row2 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: calc(100% - 10px);
  max-width: 650px;
  margin-top: 10px;
  position: relative;
}

.inputText {
  width: calc(50% - 20px);
  position: relative;
}

.outputText {
  width: calc(50% - 20px);
  height: 169px;
  font-family: monospace;
  box-sizing: border-box;
  padding: 15px;
  font-size: 20px;
  border-radius: 8px;
  overflow: auto;
  background: #edeff0;
  text-align: left;
}

.row2 textarea {
  width: 100%;
  height: 150px;
  resize: none;
  box-sizing: border-box;
  padding: 0px 35px 10px 10px;
  margin-top: 15px;
  border: none;
  font-size: 20px;
  overflow: auto;
  color: #202124;
  outline: none;
}

.removeinput {
  cursor: pointer;
  width: 20px;
  position: absolute;
  opacity: 0.5;
  top: 5px;
  right: 3px;
}

.row3 {
  width: calc(100% - 10px);
  max-width: 650px;
  margin-top: 15px;
  margin-bottom: 20px;
}

.btn {
  width: 100%;
  position: relative;
  padding: 10px;
  outline: none;
  background-color: #6d9eed;
  color: #fff;
  border: 2px solid #6d9eed;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  letter-spacing: 0.5px;
}

.btn i {
  display: none;
}

@media screen and (max-width: 500px) {
  .row2 {
    flex-direction: column;
  }
  .inputText {
    width: 100%;
  }
  .outputText {
    width: 100%;
    margin-top: 20px;
  }
}

::-webkit-scrollbar {
  width: 4px;
}
::-webkit-scrollbar-track {
  display: none;
}
::-webkit-scrollbar-thumb {
  background: rgb(173, 172, 172);
  border-radius: 4px;
}
JavaScript JavaScript JavaScript

Steps to run the application:

Step 1: Type the following command in terminal.

npm start

Step 2: Open web-browser and type the following URL

http://localhost:3000/

Output:


Next Article

Similar Reads

three90RightbarBannerImg