Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
elitan committed Nov 5, 2023
1 parent f3c7fa3 commit b40783e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 9 deletions.
8 changes: 8 additions & 0 deletions web/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ export function Layout({ children }: { children: React.ReactNode }) {
className="mt-1 block w-full rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-indigo-600 sm:text-sm sm:leading-6"
defaultValue="Canada"
onChange={(e) => {
// TODO: Get the articles slug of the given language and redirect there
// const currentPath = router.asPath;
// if (router.asPath.startsWith(`/${lang}`)) {
// return router.push(
// currentPath.replace(`/${lang}`, `/${e.target.value}`),
// );
// }

router.push(`/${e.target.value}`);
}}
>
Expand Down
1 change: 1 addition & 0 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const getServerSideProps = async () => {
'at.slug',
'at.body',
'at.category',
'at.language',
'ai.imageUrl',
])
.where('at.title', 'is not', null)
Expand Down
2 changes: 1 addition & 1 deletion web/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const languages: Record<string, LanguageProps> = {
slogan: 'Шведські новини українською',
},
ckb: {
name: 'Kurdish (Sorani)',
name: 'Kurdish',
slogan: 'هەواڵی سویدی بە زمانی کوردی',
rtl: true,
},
Expand Down
1 change: 1 addition & 0 deletions worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"codegen": "kysely-codegen --dialect postgres --camel-case --out-file ./src/utils/kysely-types.d.ts",
"backfill-images": "ts-node src/scripts/move-images-to-article-images-table.ts",
"social-media": "ts-node src/50-social-media.ts",
"tmpbackfill": "ts-node src/tmp-backfill-translations.ts",
"backup": "ts-node src/100-backup.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
18 changes: 13 additions & 5 deletions worker/src/5-piper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { put } from './utils/blob';
import fs from 'fs';
import { runCommand } from './utils/helpers';

(async () => {
const languageVoices = {
en: '/en/en_US/joe/medium/en_US-joe-medium.onnx',
};

const supportedLanguages = Object.keys(languageVoices);

async function main() {
const articlesToRefine = await db
.selectFrom('articles')
.select(['id', 'title', 'body'])
.where('title', 'is not', null)
.selectFrom('articleTranslations')
.select(['id', 'body'])
.where('body', 'is not', null)
.where('audioUrl', 'is', null)
.where('language', 'in', supportedLanguages)
.execute();

for (const article of articlesToRefine) {
Expand Down Expand Up @@ -61,4 +67,6 @@ import { runCommand } from './utils/helpers';
console.log('done');

process.exit(0);
})();
}

main();
94 changes: 94 additions & 0 deletions worker/src/tmp-backfill-translations.ts
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();
4 changes: 1 addition & 3 deletions worker/src/utils/kysely-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;

export type LanguageEnum = "ar" | "en" | "fa" | "fi" | "kur" | "ru" | "so" | "uk";

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

export interface ArticleImages {
Expand Down Expand Up @@ -54,7 +52,7 @@ export interface ArticleTranslations {
createdAt: Generated<Timestamp | null>;
updatedAt: Generated<Timestamp | null>;
articleId: number | null;
language: LanguageEnum;
language: string;
title: string | null;
slug: string | null;
body: string | null;
Expand Down

0 comments on commit b40783e

Please sign in to comment.