Translate keywords (#114)
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { SQLDataSource } from 'datasource-sql';
|
||||
import { Keyword, KeywordType } from '../types/keyword-types';
|
||||
import { TextsAPI } from './texts-api';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
export class KeywordAPI extends SQLDataSource {
|
||||
constructor(config) {
|
||||
textsApi: TextsAPI;
|
||||
constructor(config, textsApi) {
|
||||
super(config);
|
||||
this.textsApi = textsApi;
|
||||
}
|
||||
|
||||
getType(data: any) {
|
||||
@@ -98,18 +101,8 @@ ORDER BY keywords.value
|
||||
});
|
||||
}
|
||||
}
|
||||
await this.textsApi.translateKeyword(keywordId, name);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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(
|
||||
name: string,
|
||||
oldPath: string,
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ const imageServerApi = new ImageServerApi();
|
||||
const interiorsLambdaApi = new InteriorsLambdaAPI();
|
||||
const interiorApi = new InteriorAPI(knexConfig, imageServerApi);
|
||||
const printProductDefaultsApi = new PrintProductDefaultsAPI(knexConfig);
|
||||
const keywordApi = new KeywordAPI(knexConfig);
|
||||
const keywordApi = new KeywordAPI(knexConfig, textsApi);
|
||||
const orderApi = new OrderAPI(knexConfig);
|
||||
const productApi = new ProductAPI(
|
||||
knexConfig,
|
||||
|
||||
Reference in New Issue
Block a user