-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
119 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import slugify from 'slugify'; | ||
import { db } from './utils/db'; | ||
import { translate } from './utils/helpers'; | ||
import { LanguageEnum } from './utils/kysely-types'; | ||
|
||
async function main() { | ||
// get all articles that are not yet translated | ||
const articlesToTranslate = await db | ||
.selectFrom('articles') | ||
.select(['id', 'title', 'slug', 'category', 'body', 'audioUrl']) | ||
.where('title', 'is not', null) | ||
.where('isRelatedToSweden', '=', true) | ||
.execute(); | ||
|
||
for (const articleToTranslate of articlesToTranslate) { | ||
console.log({ articleToTranslate }); | ||
|
||
const { id, title, slug, body, category } = articleToTranslate; | ||
|
||
if (!title || !body || !category) { | ||
console.log('title, body or category is null'); | ||
continue; | ||
} | ||
|
||
// insert the en version | ||
|
||
await db | ||
.insertInto('articleTranslations') | ||
.values({ | ||
articleId: id, | ||
language: 'en', | ||
title, | ||
slug, | ||
body, | ||
category, | ||
isPublished: true, | ||
}) | ||
.execute(); | ||
|
||
const languages = [ | ||
'fi', | ||
'ar', | ||
'ckb', | ||
'so', | ||
'ru', | ||
'uk', | ||
'fa', | ||
'es', | ||
'de', | ||
'fr', | ||
]; | ||
for (const language of languages) { | ||
console.log(`generate article in ${language}`); | ||
|
||
const headlineTranslated = await translate({ | ||
from: 'en', | ||
to: language, | ||
text: title, | ||
}); | ||
|
||
const bodyTranslated = await translate({ | ||
from: 'en', | ||
to: language, | ||
text: body, | ||
}); | ||
|
||
const categoryTranslated = await translate({ | ||
from: 'en', | ||
to: language, | ||
text: category, | ||
}); | ||
|
||
await db | ||
.insertInto('articleTranslations') | ||
.values({ | ||
articleId: id, | ||
language: language as LanguageEnum, | ||
title: headlineTranslated, | ||
slug: slugify(headlineTranslated, { | ||
lower: true, | ||
strict: true, | ||
}), | ||
body: bodyTranslated, | ||
category: categoryTranslated, | ||
isPublished: true, | ||
}) | ||
.execute(); | ||
} | ||
} | ||
|
||
console.log(articlesToTranslate.length); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters