Translate Content
Introduction
In this recipe, you'll learn how to translate existing content from your supergraph into another language. This is great for taking care of translations on the API-end to reach users worldwide. Your supergraph's consumers can choose which language(s) they want to return, all without worrying about various language configurations on the client-side.
Before continuing, ensure you have:
- A local Hasura DDN project.
- A lambda connector added to your project.
- A Google Cloud Translation API key.
NB: This API key is free for the first 500,000 characters each month.
Recipe
Step 1. Write the function
- TypeScript
- Python
npm install @google-cloud/translate
import { v2 } from "@google-cloud/translate";
// This can also be stored as an environment variable
// in the connector's .env file.
const CLOUD_TRANSLATION_API_KEY = "your_cloud_translation_api_key";
/**
* @readonly
*/
export async function translateText(targetLanguage: string, content: string): Promise<string> {
const translate = new v2.Translate({ key: CLOUD_TRANSLATION_API_KEY });
const [translation] = await translate.translate(content, targetLanguage);
return translation;
}
google-api-python-client==v2.146.0
from hasura_ndc import start
from hasura_ndc.function_connector import FunctionConnector
from googleapiclient.discovery import build
# This can also be stored as an environment variable
# in the connector's .env file.
API_KEY = "your_cloud_translation_api_key"
connector = FunctionConnector()
@connector.register_query
def translate_text(target_language: str, content: str) -> str:
service = build("translate", "v2", developerKey=API_KEY)
# Make the translation request
response = service.translations().list(
target=target_language,
q=[content]
).execute()
# Return the translated text
return response['translations'][0]['translatedText']
if __name__ == "__main__":
start(connector)
Step 2. Track your function
To add your function, generate the related metadata that will link together any functions in your lambda connector's source files and your API:
ddn connector introspect <connector_name>
Then, you can generate an hml
file for the function using the following command:
ddn command add <connector_name> "*"
Step 3. Create a relationship (optional)
Assuming the input argument's type matches that of a type belonging to one or more of your models, you can create a relationship to the command. This will enable you to make nested queries that will invoke your custom business logic using the value of the field from the related model!
Create a relationship in the corresponding model's HML file.
---
kind: Relationship
version: v1
definition:
name: translatedReview
sourceType: Reviews
target:
command:
name: TranslateText
mapping:
- source:
fieldPath:
- fieldName: text
target:
argument:
argumentName: content
Step 4. Test your function
Create a new build of your supergraph:
ddn supergraph build local
In your project's explorer, you should see the new function exposed as a type and should be able to make a query like this:
If you created a relationship, you can make a query like this, too: