npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nhost/google-translation

v0.2.1

Published

Google Translation GraphQL API

Downloads

5

Readme

This package creates a Google Translation GraphQL API.

query {
  # Detect the initial language automatically, and set the destination language to the user's default, or the server's default
  first: googleTranslation(text: "le service est disponible")
  # Specify the destination language
  second: googleTranslation(text: "le service est disponible", to: "es")
  # Specify both initial and destination languages
  third: googleTranslation(text: "le service est disponible", from: "fr", to: "it")
}

Result:

{
  "data": {
    "first": "service is available",
    "second": "el servicio está disponible",
    "third": "il servizio è disponibile"
  }
}

You can also user the Google Translation GraphQL API with Hasura Remote Schema Relationships and connect data from your database and the Google Translation API. This allows you to request data from your database and the Google Translation API in a single GraphQL query:

query {
  books {
    title # a text column in the books table
    translatedTitle # title translated into the user's  default locale
    italianTitle: translatedTitle(to: "it") # title translated into italian
  }
}

Result:

{
  "data": {
    "books": [
      {
        "title": "Guerre et Paix",
        "translatedTitle": "War and peace",
        "italianTitle": "Guerra e Pace"
      },
      {
        "title": "Le Bruit et la Fureur",
        "translatedTitle": "The Sound and the Fury",
        "italianTitle": "Il suono e la furia"
      }
    ]
  }
}

Install

npm install @nhost/google-translation

Quick Start

Serverless Function Setup

Create a new Serverless Function functions/graphql/google-translation.ts:

import { createGoogleTranslationGraphQLServer } from '@nhost/google-translation'

export default createGoogleTranslationGraphQLServer()

You can run the Google Translation GraphQL API in any JS environment because it's built using GraphQL Yoga.

Google Project ID and API Key

Add GOOGLE_TRANSLATION_PROJECT_ID as an environment variable. If you're using Nhost, add GOOGLE_TRANSLATION_API_KEY to .env.development like this:

GOOGLE_TRANSLATION_PROJECT_ID=project-id
GOOGLE_TRANSLATION_API_KEY=xxxxxxx

Learn more about Google Projects and API keys.

Start Nhost

nhost up

Learn more about the Nhost CLI.

Test

Test the Google Translation GraphQL API in the browser:

https://local.functions.nhost.run/v1/graphql/google-translation

Remote Schema

Add the Google Translation GraphQL API as a Remote Schema in Hasura.

URL

https://local.functions.nhost.run/v1/graphql/google-translation

Headers

x-nhost-webhook-secret: NHOST_WEBHOOK_SECRET (from env var)

Remote Schema Relationships

You can use the GraphQL API to translate values from other columns.

Settings

Default language

It is possible to configure a default language by setting the getDefaultLanguage option, which is a function that gets the context as first argument, and returns either the language code, or null. The getDefaultLanguage option is preconfigured to get the user locale from the authenticated user, using the auth.users.locale value as per defined in Hasura Auth.

If the getDefaultLanguage option returns null, the default language falls back to the defaultLanguage string option, which is preconfigured as en.

Permissions

The canTranslate options is a function that accepts the GraphQL Yoga context as an argument, augmented with the useLanguage string value that has been set by the getDefaultLanguage method. By default, the canTranslate method returns true when:

  1. the x-nhost-webhook-secret header is equal to the NHOST_WEBHOOK_SECRET environment variable; and
  2. the user is an admin (either valid x-hasura-admin-secret is passed on as a header or x-hasura-role is admin), OR the user is authenticated (the request Authorization has a valid JWT)

Server settings

Other options are available to configure the GraphQL server:

  • Google Project API and API Key can be passed on with the projectId and apiKey parameters. When not set, they will fall back respectively to process.env.GOOGLE_TRANSLATION_PROJECT_ID and process.env.GOOGLE_TRANSLATION_API_KEY.
  • graphiql defaults to true. Set it to false if you don't want to serve the GraphiQL UI.
  • Custom cors configuration. See GraphQL Yoga documentation for further information.

Development

Install dependencies:

pnpm install

Start the development server:

pnpm run start

The GraphQL Server will reload every time the code changes.

Open GraphiQL:

http://0.0.0.0:4000