-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from harmony-one/update-translate-module
Migrated translation module to google cloud service
- Loading branch information
Showing
5 changed files
with
177 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import config from '../config' | ||
import type { CredentialBody } from 'google-auth-library/build/src/auth/credentials' | ||
import type { google } from '@google-cloud/text-to-speech/build/protos/protos' | ||
import { v2 } from '@google-cloud/translate' | ||
import { type LanguageResult } from '@google-cloud/translate/build/src/v2' | ||
|
||
export interface TextToSpeechParams { | ||
text: string | ||
languageCode: string | ||
ssmlGender?: google.cloud.texttospeech.v1.SsmlVoiceGender | keyof typeof google.cloud.texttospeech.v1.SsmlVoiceGender | null | ||
voiceName?: string | null | ||
} | ||
|
||
class GcTextToSpeechClient { | ||
private readonly _client: v2.Translate | ||
constructor (credentials: CredentialBody) { | ||
this._client = new v2.Translate({ credentials }) | ||
} | ||
|
||
async translate (text: string, targetLang: string): Promise<string> { | ||
const [strList] = await this._client.translate([text], targetLang) | ||
|
||
return strList[0] | ||
} | ||
|
||
async getLanguageList (): Promise<LanguageResult[]> { | ||
const [languages] = await this._client.getLanguages() | ||
return languages | ||
} | ||
} | ||
|
||
const credentials = JSON.parse(Buffer.from(config.gc.credentials, 'base64').toString('utf-8')) | ||
|
||
export const gcTranslateClient = new GcTextToSpeechClient(credentials) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { gcTranslateClient } from './gcTranslateClient' | ||
|
||
async function main (): Promise<void> { | ||
const client = gcTranslateClient | ||
const languageList = await client.getLanguageList() | ||
|
||
console.log('### languageList', languageList.length) | ||
|
||
const codes = languageList.map(lang => lang.code) | ||
console.log('### codes', JSON.stringify(codes)) | ||
// for (const lang of languageList) { | ||
// console.log(lang) | ||
// } | ||
|
||
const r = await client.translate('你好', 'ru') | ||
console.log('### r', r) | ||
} | ||
|
||
main().then(() => { console.log('### finish') }).catch(console.log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters