🚀 Socket Launch Week 🚀 Day 5: Introducing Socket Fix.Learn More
Socket
Sign inDemoInstall
Socket

react-google-translate

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-google-translate

Useful hook for Google Translate API.

1.0.12
latest
Source
npm
Version published
Weekly downloads
45
-2.17%
Maintainers
1
Weekly downloads
 
Created
Source

react-google-translate

Useful hook for Google Translate API.

NPM JavaScript Style Guide

Demo

See demo

Install

npm install --save react-google-translate @google-cloud/translate

Environment variables

Generate your credentials and project id in Google Cloud Platform. Read through the documentation for setting a service account.

After you acquired your credentials and project id, add it to your environment variables.

GCP_PRIVATE_KEY=[private_key]
GCP_CLIENT_EMAIL=[client_email]
GCP_PROJECT_ID=[project_id]

Setting up the config

In your index.js,

import { setConfig } from 'react-google-translate'

setConfig({
  clientEmail: process.env.REACT_APP_GCP_CLIENT_EMAIL ?? '',
  privateKey: process.env.REACT_APP_GCP_PRIVATE_KEY ?? '',
  projectId: process.env.REACT_APP_GCP_PROJECT_ID ?? ''
})

Usage

import React, { useState, useEffect } from 'react'

import { useLazyTranslate } from 'react-google-translate'

const Example = () => {

  const [text] = useState('test');
  const [language] = useState('zh-CN');

  const [translate, { data, loading }] = useLazyTranslate({
    language
  })

  useEffect(() => {
    if (text) {
      translate(text, language);
    }
  }, [translate, text])

  render() {
    return (
      <div>{loading ? 'Loading...' : data}</div>
    )
  }
}

Props

language: string | string[]

Set the default language for the translation.

skip: boolean

Skips the request if true.

API

translate: func

Calls the api to translate the given text and language.

loading: boolean

Indicates that loading state.

data: string | string[]

Translated text received from the hook.

called: boolean

Indicates that hook has been triggered.

License

MIT © geminstall23

FAQs

Package last updated on 26 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts