1- import { https , logger } from 'firebase-functions' ;
1+ import { firestore , https , logger } from 'firebase-functions' ;
22import { SecurityUtils } from './utils/security-utils' ;
3- import { BATCH_MAX , bucket , firestoreService , ROLE_ADMIN , ROLE_WRITE } from './config' ;
3+ import {
4+ BATCH_MAX ,
5+ bucket ,
6+ firebaseConfig ,
7+ firestoreService ,
8+ ROLE_ADMIN ,
9+ ROLE_WRITE ,
10+ SUPPORT_LOCALES ,
11+ translationService
12+ } from './config' ;
413import { Space } from './models/space.model' ;
514import {
615 PublishTranslationsData ,
@@ -13,6 +22,7 @@ import {
1322} from './models/translations.model' ;
1423import { FieldValue , QuerySnapshot , Timestamp , WriteBatch } from 'firebase-admin/firestore' ;
1524import axios from 'axios' ;
25+ import { protos } from '@google-cloud/translate' ;
1626
1727// Publish
1828export const translationsPublish = https . onCall ( async ( data : PublishTranslationsData , context ) => {
@@ -223,7 +233,6 @@ export const translationsImport = https.onCall(async (data: TranslationsImportDa
223233 // add label
224234 update . labels = value . locales
225235 }
226- logger . info ( 'sdgsdg' )
227236 if ( Object . getOwnPropertyNames ( update ) . length > 1 ) {
228237 batches [ batchIdx ] . update ( firestoreService . doc ( `spaces/${ data . spaceId } /translations/${ oid } ` ) , update ) ;
229238 totalChanges ++ ;
@@ -265,3 +274,59 @@ export const translationsImport = https.onCall(async (data: TranslationsImportDa
265274 throw new https . HttpsError ( 'not-found' , 'Space not found' ) ;
266275 }
267276} ) ;
277+
278+ export const onTranslationCreate = firestore . document ( 'spaces/{spaceId}/translations/{translationId}' )
279+ . onCreate ( async ( snapshot , context ) => {
280+ logger . info ( `[Translation::onCreate] id='${ snapshot . id } ' eventId='${ context . eventId } '` ) ;
281+ const spaceId : string = context . params [ 'spaceId' ] ;
282+ // const translationId: string = context.params.translationId
283+
284+ const spaceSnapshot = await firestoreService . doc ( `spaces/${ spaceId } ` ) . get ( ) ;
285+
286+ const space = spaceSnapshot . data ( ) as Space ;
287+ // is incoming locale supporting translation ?
288+ if ( ! SUPPORT_LOCALES . has ( space . localeFallback . id ) ) return ;
289+
290+ const translation = snapshot . data ( ) as Translation ;
291+ const localeValue = translation . locales [ space . localeFallback . id ]
292+
293+ const projectId = firebaseConfig . projectId
294+ let locationId ; //firebaseConfig.locationId || 'global'
295+ if ( firebaseConfig . locationId && firebaseConfig . locationId . startsWith ( 'us-' ) ) {
296+ locationId = 'us-central1'
297+ } else {
298+ locationId = 'global'
299+ }
300+
301+ const update : any = {
302+ translate : FieldValue . delete ( ) ,
303+ updatedOn : FieldValue . serverTimestamp ( ) ,
304+ } ;
305+
306+ for ( const locale of space . locales ) {
307+ // skip already filled data
308+ if ( locale . id === space . localeFallback . id ) continue ;
309+ // skip unsupported locale
310+ if ( ! SUPPORT_LOCALES . has ( locale . id ) ) continue ;
311+
312+ const request : protos . google . cloud . translation . v3 . ITranslateTextRequest = {
313+ parent : `projects/${ projectId } /locations/${ locationId } ` ,
314+ contents : [ localeValue ] ,
315+ mimeType : 'text/plain' ,
316+ sourceLanguageCode : space . localeFallback . id ,
317+ targetLanguageCode : locale . id ,
318+ } ;
319+ try {
320+ const [ responseTranslateText ] = await translationService . translateText ( request ) ;
321+ if ( responseTranslateText . translations && responseTranslateText . translations . length > 0 ) {
322+ update [ `locales.${ locale . id } ` ] = responseTranslateText . translations [ 0 ] . translatedText ;
323+ }
324+ } catch ( e ) {
325+ logger . error ( e )
326+ }
327+ }
328+ logger . info ( `[Translation::onCreate] Update : ${ JSON . stringify ( update ) } ` )
329+ await snapshot . ref . update ( update )
330+
331+ return
332+ } ) ;
0 commit comments