Add translation for product names (#74)
This commit is contained in:
@@ -28,12 +28,15 @@ import {
|
||||
} from './utils';
|
||||
import { UserInputError } from 'apollo-server';
|
||||
import { sendPathChangedCmd } from '../bernard/client';
|
||||
import { TextsAPI } from './texts-api';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
export class ProductAPI extends BaseSQLDataSource {
|
||||
constructor(config) {
|
||||
textsApi: TextsAPI;
|
||||
constructor(config, textsApi) {
|
||||
super(config);
|
||||
this.textsApi = textsApi;
|
||||
}
|
||||
|
||||
// async getMaterial(printId: number): Promise<any> {
|
||||
@@ -384,6 +387,14 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
sendPathChangedCmd(oldProd.path, uniquePath);
|
||||
}
|
||||
|
||||
if (oldProd.fields.name !== info.name) {
|
||||
await this.textsApi.translateProductName(
|
||||
info.name,
|
||||
oldProd.path,
|
||||
uniquePath,
|
||||
);
|
||||
}
|
||||
|
||||
const promises = [];
|
||||
promises.push(
|
||||
this.knex
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { SQLDataSource } from 'datasource-sql';
|
||||
import { TranslateInput, translateText } from '../aws/translate';
|
||||
import { Locale } from '../types/market-locale-types';
|
||||
import { MarketLocaleAPI } from './market-locale-api';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
// TODO: perhaps rename to MarketLocale API and add market_locales and locales here.
|
||||
export class TextsAPI extends SQLDataSource {
|
||||
marketLocaleApi: MarketLocaleAPI;
|
||||
|
||||
constructor(config, marketLocaleApi: MarketLocaleAPI) {
|
||||
super(config);
|
||||
this.marketLocaleApi = marketLocaleApi;
|
||||
}
|
||||
|
||||
async translateProductName(name: string, oldPath, newPath): Promise<void> {
|
||||
// Check if we have an entry already
|
||||
const textKey = await this.upsertTextId(newPath, oldPath);
|
||||
|
||||
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: name,
|
||||
targetLanguageCode: langCode,
|
||||
};
|
||||
const translated = await translateText(input);
|
||||
|
||||
await this.upsertTextDataEntry(textKey.textid, translated, locales[i].id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert translated entry to the i18n-texts_data table
|
||||
* @param text The translated text to insert
|
||||
* @param localeId The id of the locale
|
||||
*/
|
||||
private async upsertTextDataEntry(
|
||||
textId: number,
|
||||
text: string,
|
||||
localeId: number,
|
||||
) {
|
||||
// Check if we have an entry already
|
||||
const textDataEntry = await this.knex
|
||||
.select('*')
|
||||
.from('i18n-texts_data')
|
||||
.where({ textid: textId, locale_id: localeId })
|
||||
.first();
|
||||
|
||||
if (textDataEntry) {
|
||||
// Update existing entry
|
||||
await this.knex('i18n-texts_data')
|
||||
.where({ textid: textId, locale_id: localeId })
|
||||
.update({ text: text });
|
||||
} else {
|
||||
//Insert new entry
|
||||
await this.knex('i18n-texts_data').insert({
|
||||
text: text,
|
||||
textid: textId,
|
||||
locale_id: localeId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private getLanguageCode(locale: Locale) {
|
||||
let langCode = locale.value.substr(0, 2);
|
||||
return langCode == 'nn' ? 'no' : langCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert translated entry to the i18n-texts table
|
||||
* @param newKey the new name of the text key
|
||||
* @param localeId the old name of the text key
|
||||
*/
|
||||
private async upsertTextId(newKey: string, oldKey: string) {
|
||||
let textEntry = await this.knex
|
||||
.select('*')
|
||||
.from('i18n-texts')
|
||||
.where({ name: oldKey })
|
||||
.first();
|
||||
|
||||
let textId = textEntry?.textid;
|
||||
// No entry, create one
|
||||
if (!textId) {
|
||||
const res = await this.knex('i18n-texts')
|
||||
.insert({ name: newKey, category: 'productTitles' })
|
||||
.returning('*');
|
||||
|
||||
textEntry = res[0];
|
||||
}
|
||||
|
||||
// Update table if different
|
||||
if (oldKey !== newKey) {
|
||||
await this.knex
|
||||
.table('i18n-texts')
|
||||
.update({ name: newKey })
|
||||
.where({ name: oldKey });
|
||||
}
|
||||
return textEntry;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user