Integrate Google Translate API into your NestJS app
Integrate Google api-key
- Create API Key in Google Cloud on the site https://console.cloud.google.com/apis/credentials
- Integrate API Key in the Module
@Module({ imports: [ NestGoogleTranslatorModule.forRootAsync({ useFactory: () => ({ apiKey: "***************", }), }), ], })
Service included 3 async methods
Integrate service NestGoogleTranslatorService in your app for use methods:
- detectLanguage(text: string): Promise<Language | TranslatorError> - Automatic language detection based on text
- getSupportedLanguages(): Promise<Language[] | TranslatorError> - Get all supported languages
- translate(text: string, targetLanguageCode: string, options: TranslateOptions): Promise<TranslateResponse | TranslatorError> - Translates text
Errors (TranslatorError)
Error type
{
code: string;
name: string;
message: string;
}- Any error return
code = 'error'