Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic translations with google #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"typeorm": "ts-node node_modules/.bin/typeorm"
},
"dependencies": {
"@google-cloud/translate": "^6.0.3",
"@nest-middlewares/morgan": "^6.0.0",
"@nestjs/common": "^6.5.3",
"@nestjs/core": "^6.5.3",
Expand Down
3 changes: 2 additions & 1 deletion api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { User } from './entity/user.entity';
import { CustomExceptionFilter } from './filters/exception.filter';
import { AuthService } from './services/auth.service';
import AuthorizationService from './services/authorization.service';
import AutomaticTranslationService from './services/automatic_translation.service';
import { JwtStrategy } from './services/jwt.strategy';
import MailService from './services/mail.service';
import { UserService } from './services/user.service';
Expand Down Expand Up @@ -71,7 +72,7 @@ import ProjectStatsController from './controllers/project-stats.controller';
LocaleController,
IndexController,
],
providers: [UserService, AuthService, MailService, JwtStrategy, AuthorizationService],
providers: [UserService, AuthService, MailService, JwtStrategy, AuthorizationService, AutomaticTranslationService],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
Expand Down
5 changes: 5 additions & 0 deletions api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export const config = {
import: {
maxNestedLevels: parseInt(env.TR_IMPORT_MAX_NESTED_LEVELS, 10) || 100,
},
translators: {
google: {
projectId: env.TR_TRANSLATOR_GOOGLE_PROJECT_ID,
},
},
providers: {
google: {
active: env.TR_AUTH_GOOGLE_ENABLED,
Expand Down
88 changes: 87 additions & 1 deletion api/src/controllers/translation.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, NotFoundException, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
NotFoundException,
Param,
Patch,
Post,
Req,
UseGuards,
BadRequestException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiOAuth2Auth, ApiOperation, ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
Expand All @@ -19,6 +33,7 @@ import { Project } from '../entity/project.entity';
import { Term } from '../entity/term.entity';
import { Translation } from '../entity/translation.entity';
import AuthorizationService from '../services/authorization.service';
import AutomaticTranslationService from '../services/automatic_translation.service';

@Controller('api/v1/projects/:projectId/translations')
@UseGuards(AuthGuard())
Expand All @@ -27,6 +42,7 @@ import AuthorizationService from '../services/authorization.service';
export default class TranslationController {
constructor(
private auth: AuthorizationService,
private automaticTranslationService: AutomaticTranslationService,
@InjectRepository(Translation) private translationRepo: Repository<Translation>,
@InjectRepository(ProjectLocale) private projectLocaleRepo: Repository<ProjectLocale>,
@InjectRepository(Term) private termRepo: Repository<Term>,
Expand Down Expand Up @@ -112,6 +128,76 @@ export default class TranslationController {
};
}

@Get('autotranslate/:localeCode')
@ApiOperation({ title: `Request automatic translation for untranslated strings` })
@ApiResponse({ status: HttpStatus.NO_CONTENT, description: 'Deleted' })
@ApiResponse({ status: HttpStatus.NOT_FOUND, description: 'Project or locale not found' })
@ApiResponse({ status: HttpStatus.UNAUTHORIZED, description: 'Unauthorized' })
async autotranslate(@Req() req, @Param('projectId') projectId: string, @Param('localeCode') localeCode: string) {
const user = this.auth.getRequestUserOrClient(req);
// @TODO: Implement new authorization "Autotranslate"
const membership = await this.auth.authorizeProjectAction(user, projectId, ProjectAction.ViewTranslation);

// Ensure locale is requested project locale
const projectLocale = await this.projectLocaleRepo.findOneOrFail({
where: {
project: membership.project,
locale: {
code: localeCode,
},
},
});

const translations = await this.translationRepo.find({
where: {
projectLocale,
},
relations: ['term', 'labels'],
});

const result = translations.map(t => ({
termId: t.term.id,
original: t.term.value,
value: t.value,
labels: t.labels,
date: t.date,
projectLocaleId: t.projectLocaleId,
}));

const filteredResult = result.filter(t => !t.value || t.value.length === 0);

if (filteredResult.length == 0) {
// @TODO: If only I knew how to get this response to the client side. It
// always seems to default to the default response.
throw new BadRequestException('No untranslated strings to translate.');
}

try {
// The translationResult holds the same object as filteredResult passed in,
// but in the translationResult the value is populated with the translation.
const translationResult = await this.automaticTranslationService.translate(
filteredResult,
'en', // @TODO: There is no source language anywhere yet?
localeCode,
);

translationResult.forEach(async (translation: object) => {
// @TODO: Here it should ideally also save a boolean "tocheck" or "machinetranslated"
// which we can use to filter the results and allow a button to "validate"
// otherwise there is no way to see what has been machine translated and
// what not.
await this.translationRepo.save(translation);
});

// In the response, we for now just put the amount of successfull result.
// @TODO: I guess we can make this a prettier overview as well redirect
// to the resultset of non validated translations.
return { data: { count: translationResult.length } };
} catch (error) {
throw new BadRequestException('Something went wrong during the translation request', error);
}
}

@Get(':localeCode')
@ApiOperation({ title: `List translated terms for a locale` })
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ListTermTranslatonsResponse })
Expand Down
56 changes: 56 additions & 0 deletions api/src/services/automatic_translation.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Injectable } from '@nestjs/common';
import { config } from '../config';
import { TranslationServiceClient } from '@google-cloud/translate';

@Injectable()
export default class AutomaticTranslationService {
location: String;
projectId: String;

constructor() {
this.location = 'global';
this.projectId = config.translators.google.projectId;
}

// @TODO: I dont know how to typecast this? Do I have to create a custom object type?
async translate(requestTranslations: Array<any>, sourceLangCode: string, targetLangCode: string): Promise<any> {
const translationClient = new TranslationServiceClient();

return new Promise<Array<Object>>((resolve, reject) => {
// @TODO: The api is limited to 1024 strings. For a draft I just have
// it cut off at 1024, you can do multiple requests if you have more
// strings.
let arrayToTranslate: Array<string> = [];
requestTranslations.slice(0, 1024).forEach(translation => {
arrayToTranslate.push(translation.original);
});

const request = {
parent: `projects/${this.projectId}/locations/${this.location}`,
contents: arrayToTranslate,
mimeType: 'text/html', // @TOOD: Or text/plain, It could include html tags..?
sourceLanguageCode: sourceLangCode,
targetLanguageCode: targetLangCode,
};

translationClient
.translateText(request)
.then((googleResponse: any) => {
const translatedList: Array<object> = googleResponse[0].translations;

if (translatedList.length != arrayToTranslate.length) {
reject(Error('Response did not meet request amount.'));
}

translatedList.forEach((translation: any, index: number) => {
requestTranslations[index].value = translation.translatedText;
});

resolve(requestTranslations);
})
.catch(error => {
reject(error);
});
});
}
}