Translate keywords (#114)
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
import { SQLDataSource } from 'datasource-sql';
|
import { SQLDataSource } from 'datasource-sql';
|
||||||
import { Keyword, KeywordType } from '../types/keyword-types';
|
import { Keyword, KeywordType } from '../types/keyword-types';
|
||||||
|
import { TextsAPI } from './texts-api';
|
||||||
|
|
||||||
const MINUTE = 60;
|
const MINUTE = 60;
|
||||||
|
|
||||||
export class KeywordAPI extends SQLDataSource {
|
export class KeywordAPI extends SQLDataSource {
|
||||||
constructor(config) {
|
textsApi: TextsAPI;
|
||||||
|
constructor(config, textsApi) {
|
||||||
super(config);
|
super(config);
|
||||||
|
this.textsApi = textsApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
getType(data: any) {
|
getType(data: any) {
|
||||||
@@ -98,18 +101,8 @@ ORDER BY keywords.value
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
await this.textsApi.translateKeyword(keywordId, name);
|
||||||
|
|
||||||
return keywordId;
|
return keywordId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
category keywords:
|
|
||||||
SELECT category_keyword.category_id, keywords.value
|
|
||||||
FROM category_keyword
|
|
||||||
JOIN keywords ON category_keyword.keyword_id = keywords.id;
|
|
||||||
|
|
||||||
texts:
|
|
||||||
FROM categorydata cd
|
|
||||||
JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id
|
|
||||||
*/
|
|
||||||
|
|||||||
@@ -14,6 +14,31 @@ export class TextsAPI extends SQLDataSource {
|
|||||||
this.marketLocaleApi = marketLocaleApi;
|
this.marketLocaleApi = marketLocaleApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async translateKeyword(keywordId: number, keyword: string): Promise<void> {
|
||||||
|
const locales = await this.marketLocaleApi.getLocales();
|
||||||
|
for (let i = 0; i < locales.length; i++) {
|
||||||
|
let langCode = this.getLanguageCode(locales[i]);
|
||||||
|
// If we have a fallback locale we skip translating
|
||||||
|
if (locales[i].fallbackId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const input: TranslateInput = {
|
||||||
|
text: keyword,
|
||||||
|
targetLanguageCode: langCode,
|
||||||
|
sourceLanguageCode: 'en',
|
||||||
|
};
|
||||||
|
const translated = await translateText(input);
|
||||||
|
|
||||||
|
const res = await this.knex('keywords_i18n')
|
||||||
|
.insert({
|
||||||
|
value: translated,
|
||||||
|
keyword_id: keywordId,
|
||||||
|
locale_id: locales[i].id,
|
||||||
|
})
|
||||||
|
.returning('*');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async translateProductName(
|
async translateProductName(
|
||||||
name: string,
|
name: string,
|
||||||
oldPath: string,
|
oldPath: string,
|
||||||
|
|||||||
+1
-1
@@ -40,7 +40,7 @@ const imageServerApi = new ImageServerApi();
|
|||||||
const interiorsLambdaApi = new InteriorsLambdaAPI();
|
const interiorsLambdaApi = new InteriorsLambdaAPI();
|
||||||
const interiorApi = new InteriorAPI(knexConfig, imageServerApi);
|
const interiorApi = new InteriorAPI(knexConfig, imageServerApi);
|
||||||
const printProductDefaultsApi = new PrintProductDefaultsAPI(knexConfig);
|
const printProductDefaultsApi = new PrintProductDefaultsAPI(knexConfig);
|
||||||
const keywordApi = new KeywordAPI(knexConfig);
|
const keywordApi = new KeywordAPI(knexConfig, textsApi);
|
||||||
const orderApi = new OrderAPI(knexConfig);
|
const orderApi = new OrderAPI(knexConfig);
|
||||||
const productApi = new ProductAPI(
|
const productApi = new ProductAPI(
|
||||||
knexConfig,
|
knexConfig,
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
const {
|
||||||
|
TranslateClient,
|
||||||
|
TranslateTextCommand,
|
||||||
|
} = require('@aws-sdk/client-translate');
|
||||||
|
|
||||||
|
const db = require('knex')({
|
||||||
|
client: 'pg',
|
||||||
|
connection: 'postgresql://fondberg:@docker.for.mac.localhost/photowall',
|
||||||
|
searchPath: ['public'],
|
||||||
|
});
|
||||||
|
|
||||||
|
async function translateText(input) {
|
||||||
|
const translateClient = new TranslateClient({
|
||||||
|
region: 'eu-west-1',
|
||||||
|
});
|
||||||
|
|
||||||
|
const translateTextCommand = new TranslateTextCommand({
|
||||||
|
SourceLanguageCode: input.sourceLanguageCode ?? 'auto',
|
||||||
|
TargetLanguageCode: input.targetLanguageCode,
|
||||||
|
Text: input.text,
|
||||||
|
});
|
||||||
|
|
||||||
|
return translateClient
|
||||||
|
.send(translateTextCommand)
|
||||||
|
.then((resp) => resp.TranslatedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLanguageCode(locale) {
|
||||||
|
let langCode = locale.value.substr(0, 2);
|
||||||
|
return langCode == 'nn' ? 'no' : langCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function translate(locales, keyword) {
|
||||||
|
for (let i = 0; i < locales.length; i++) {
|
||||||
|
const langCode = getLanguageCode(locales[i]);
|
||||||
|
const input = {
|
||||||
|
text: keyword.value,
|
||||||
|
targetLanguageCode: langCode,
|
||||||
|
};
|
||||||
|
const translated = await translateText(input);
|
||||||
|
console.log(keyword.value + ' : ' + translated);
|
||||||
|
const res = await db('keywords_i18n')
|
||||||
|
.insert({
|
||||||
|
value: translated,
|
||||||
|
keyword_id: keyword.id,
|
||||||
|
locale_id: locales[i].id,
|
||||||
|
})
|
||||||
|
.returning('*');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const locales = await db.select().table('locales').whereNull('fallback_id');
|
||||||
|
const keywords = await db.select().table('keywords');
|
||||||
|
for (let i = 0; i < keywords.length; i++) {
|
||||||
|
await translate(locales, keywords[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
main();
|
||||||
Reference in New Issue
Block a user