From 8375523680d81fef9a21e36ae8cc01e8ed7116c4 Mon Sep 17 00:00:00 2001 From: Robert Vitonsky Date: Tue, 8 Aug 2023 00:11:30 +0300 Subject: [PATCH] chore(#386): update translators package --- package-lock.json | 14 +++++++------- package.json | 2 +- src/app/Background/TranslatorManager/index.ts | 6 +++--- src/app/Background/index.ts | 4 ++-- src/lib/language.tsx | 5 ++--- src/pages/dictionary/layout/DictionaryPage.tsx | 4 +++- .../layout/OptionsPage.utils/generateTree.tsx | 4 +++- src/requests/backend/translate.ts | 8 ++++---- src/requests/backend/translators/index.ts | 6 ++++-- src/requests/backend/translators/utils.ts | 6 +++--- .../pageTranslation/enableTranslatePage.ts | 2 +- src/types/runtime.ts | 14 ++++++-------- 12 files changed, 39 insertions(+), 36 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3b029199..43f9a9f0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "@popperjs/core": "^2.11.5", "@react-aria/interactions": "^3.9.1", "@react-aria/utils": "^3.13.1", - "@translate-tools/core": "^0.2.16", + "@translate-tools/core": "^1.0.0", "colord": "^2.9.1", "domtranslator": "^0.0.1", "effector": "^22.4.1", @@ -4165,9 +4165,9 @@ } }, "node_modules/@translate-tools/core": { - "version": "0.2.16", - "resolved": "https://registry.npmjs.org/@translate-tools/core/-/core-0.2.16.tgz", - "integrity": "sha512-OUxyeLyKutxpB2Qph792IMQUxZ8Rbc3AyWGWo9PMpvFx4lJF5Vx8sd85DM8g6DfjB6EI2ZEt5ljtIUwq1BhYZw==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@translate-tools/core/-/core-1.0.0.tgz", + "integrity": "sha512-FmK4f0SZl81uVIslgNABkZYqticpVyWSMf+JwKwSd/1CDdZXRyYN0YacZdMd8Rcg0pF80zaPOkW6FgvUR8e1vw==", "dependencies": { "@xmldom/xmldom": "^0.8.1", "axios": "^0.23.0", @@ -26326,9 +26326,9 @@ } }, "@translate-tools/core": { - "version": "0.2.16", - "resolved": "https://registry.npmjs.org/@translate-tools/core/-/core-0.2.16.tgz", - "integrity": "sha512-OUxyeLyKutxpB2Qph792IMQUxZ8Rbc3AyWGWo9PMpvFx4lJF5Vx8sd85DM8g6DfjB6EI2ZEt5ljtIUwq1BhYZw==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@translate-tools/core/-/core-1.0.0.tgz", + "integrity": "sha512-FmK4f0SZl81uVIslgNABkZYqticpVyWSMf+JwKwSd/1CDdZXRyYN0YacZdMd8Rcg0pF80zaPOkW6FgvUR8e1vw==", "requires": { "@xmldom/xmldom": "^0.8.1", "axios": "^0.23.0", diff --git a/package.json b/package.json index f92c1575..b3e3ac13 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "@popperjs/core": "^2.11.5", "@react-aria/interactions": "^3.9.1", "@react-aria/utils": "^3.13.1", - "@translate-tools/core": "^0.2.16", + "@translate-tools/core": "^1.0.0", "colord": "^2.9.1", "domtranslator": "^0.0.1", "effector": "^22.4.1", diff --git a/src/app/Background/TranslatorManager/index.ts b/src/app/Background/TranslatorManager/index.ts index 11c39bea..440d6df3 100644 --- a/src/app/Background/TranslatorManager/index.ts +++ b/src/app/Background/TranslatorManager/index.ts @@ -1,9 +1,9 @@ -import { isLanguageCodeISO639v2 } from '@translate-tools/core/util/languages'; +import { isLanguageCodeISO639v1 } from '@translate-tools/core/languages'; import { IScheduler, Scheduler, SchedulerWithCache, -} from '@translate-tools/core/util/Scheduler'; +} from '@translate-tools/core/scheduling'; import { AppConfigType } from '../../../types/runtime'; import { RecordValues } from '../../../types/utils'; @@ -39,7 +39,7 @@ export class TranslatorManager isLanguageCodeISO639v2(lang)), + .filter((lang) => isLanguageCodeISO639v1(lang)), isSupportAutodetect: translatorClass.isSupportedAutoFrom(), }; } diff --git a/src/app/Background/index.ts b/src/app/Background/index.ts index aeeaede2..2670c486 100644 --- a/src/app/Background/index.ts +++ b/src/app/Background/index.ts @@ -1,8 +1,8 @@ import { isEqual } from 'lodash'; // Translators import { GoogleTranslator } from '@translate-tools/core/translators/GoogleTranslator'; +import { TranslatorConstructor } from '@translate-tools/core/translators/Translator'; import { YandexTranslator } from '@translate-tools/core/translators/YandexTranslator'; -import { TranslatorClass } from '@translate-tools/core/types/Translator'; import { createSelector } from '../../lib/effector/createSelector'; import { BergamotTranslator } from '../../lib/translators/bergamot/BergamotTranslator'; @@ -27,7 +27,7 @@ export const embeddedTranslators = { /** * Map where key is identifier of translator and value is translator constructor */ -export type TranslatorsMap = Record; +export type TranslatorsMap = Record; /** * Background features manager diff --git a/src/lib/language.tsx b/src/lib/language.tsx index c9e06305..bd7710f4 100644 --- a/src/lib/language.tsx +++ b/src/lib/language.tsx @@ -1,6 +1,6 @@ import React, { ComponentType, ReactNode } from 'react'; import browser from 'webextension-polyfill'; -import { langCodes } from '@translate-tools/core/util/languages'; +import { isLanguageCodeISO639v1 } from '@translate-tools/core/languages'; import { isMobileBrowser } from './browser'; @@ -100,5 +100,4 @@ export const detectLanguage = async (text: string, reliableOnly = false) => { }); }; -export const isValidLanguage = (language: string) => - (langCodes as readonly string[]).includes(language); +export const isValidLanguage = (language: string) => isLanguageCodeISO639v1(language); diff --git a/src/pages/dictionary/layout/DictionaryPage.tsx b/src/pages/dictionary/layout/DictionaryPage.tsx index 088bc2eb..f621ecef 100644 --- a/src/pages/dictionary/layout/DictionaryPage.tsx +++ b/src/pages/dictionary/layout/DictionaryPage.tsx @@ -2,7 +2,7 @@ import React, { FC, useCallback, useEffect, useMemo, useState } from 'react'; import { useImmutableCallback } from 'react-elegant-ui/esm/hooks/useImmutableCallback'; import Papa from 'papaparse'; import { cn } from '@bem-react/classname'; -import { langCodes } from '@translate-tools/core/util/languages'; +import { getLanguageCodesISO639 } from '@translate-tools/core/languages'; import { LayoutFlow } from '../../../components/layouts/LayoutFlow/LayoutFlow'; import { Page } from '../../../components/layouts/Page/Page'; @@ -30,6 +30,8 @@ import './DictionaryPage.css'; export const cnDictionaryPage = cn('DictionaryPage'); +const langCodes = getLanguageCodesISO639('v1'); + // TODO: implement as option export interface IDictionaryPageProps { confirmDelete?: boolean; diff --git a/src/pages/options/layout/OptionsPage.utils/generateTree.tsx b/src/pages/options/layout/OptionsPage.utils/generateTree.tsx index 68a24c56..03ddff30 100644 --- a/src/pages/options/layout/OptionsPage.utils/generateTree.tsx +++ b/src/pages/options/layout/OptionsPage.utils/generateTree.tsx @@ -1,5 +1,5 @@ import React, { FC } from 'react'; -import { langCodes } from '@translate-tools/core/util/languages'; +import { getLanguageCodesISO639 } from '@translate-tools/core/languages'; import { getLanguageNameByCode, @@ -11,6 +11,8 @@ import { capitalizeString } from '../../../../lib/utils'; import { OptionsGroup } from '../OptionsTree/OptionsTree'; +const langCodes = getLanguageCodesISO639('v1'); + type Options = { clearCacheProcess: boolean; translatorModules: Record; diff --git a/src/requests/backend/translate.ts b/src/requests/backend/translate.ts index f92ad943..6b4eb22e 100644 --- a/src/requests/backend/translate.ts +++ b/src/requests/backend/translate.ts @@ -1,5 +1,5 @@ -import { langCode, langCodeWithAuto } from '@translate-tools/core/types/Translator'; -import { ITranslateOptions } from '@translate-tools/core/util/Scheduler/IScheduler'; +import { ISchedulerTranslateOptions } from '@translate-tools/core/scheduling'; +import { langCode, langCodeWithAuto } from '@translate-tools/core/translators/Translator'; import { buildBackendRequest } from '../utils/requestBuilder'; @@ -8,7 +8,7 @@ export const [translateFactory, translateRequest] = buildBackendRequest< text: string; from: langCodeWithAuto; to: langCode; - options?: ITranslateOptions; + options?: ISchedulerTranslateOptions; }, string >('translate', { @@ -26,7 +26,7 @@ export const translate = ( text: string, from: string, to: string, - options?: ITranslateOptions, + options?: ISchedulerTranslateOptions, ) => translateRequest({ text, diff --git a/src/requests/backend/translators/index.ts b/src/requests/backend/translators/index.ts index f5ccfd11..b8fcd2c6 100644 --- a/src/requests/backend/translators/index.ts +++ b/src/requests/backend/translators/index.ts @@ -1,4 +1,4 @@ -import { TranslatorClass } from '@translate-tools/core/types/Translator'; +import { TranslatorConstructor } from '@translate-tools/core/translators/Translator'; import { embeddedTranslators, TranslatorsMap } from '../../../app/Background'; @@ -25,7 +25,9 @@ export const isCustomTranslatorId = (id: string) => id.startsWith('#'); * Return map with all available translators, where keys is translators id */ export const getTranslatorsClasses = async (): Promise => { - const translatorsMap: Record = { ...embeddedTranslators }; + const translatorsMap: Record = { + ...embeddedTranslators, + }; // Validate and collect custom translators const customTranslators = await getTranslators({ order: 'asc' }); diff --git a/src/requests/backend/translators/utils.ts b/src/requests/backend/translators/utils.ts index 6e1e0818..5c589ec5 100644 --- a/src/requests/backend/translators/utils.ts +++ b/src/requests/backend/translators/utils.ts @@ -1,5 +1,5 @@ -import { TranslatorClass } from '@translate-tools/core/types/Translator'; -import { BaseTranslator as ExternalBaseTranslator } from '@translate-tools/core/util/BaseTranslator'; +import { BaseTranslator as ExternalBaseTranslator } from '@translate-tools/core/translators/BaseTranslator'; +import { TranslatorConstructor } from '@translate-tools/core/translators/Translator'; export const loadTranslator = (code: string) => { // TODO: remove `BaseTranslator` @@ -51,5 +51,5 @@ export const loadTranslator = (code: string) => { } }); - return translatorClass as TranslatorClass; + return translatorClass as TranslatorConstructor; }; diff --git a/src/requests/contentscript/pageTranslation/enableTranslatePage.ts b/src/requests/contentscript/pageTranslation/enableTranslatePage.ts index 2e3394ed..b3bbb49d 100644 --- a/src/requests/contentscript/pageTranslation/enableTranslatePage.ts +++ b/src/requests/contentscript/pageTranslation/enableTranslatePage.ts @@ -1,4 +1,4 @@ -import { langCode, langCodeWithAuto } from '@translate-tools/core/types/Translator'; +import { langCode, langCodeWithAuto } from '@translate-tools/core/translators/Translator'; import { type } from '../../../lib/types'; import { LangCode, LangCodeWithAuto } from '../../../types/runtime'; diff --git a/src/types/runtime.ts b/src/types/runtime.ts index d43b9ad0..e6708b3b 100644 --- a/src/types/runtime.ts +++ b/src/types/runtime.ts @@ -1,6 +1,6 @@ import { TypeOf } from 'io-ts'; -import { langCode, langCodeWithAuto } from '@translate-tools/core/types/Translator'; -import { langCodes } from '@translate-tools/core/util/languages'; +import { isLanguageCodeISO639v1 } from '@translate-tools/core/languages'; +import { langCode, langCodeWithAuto } from '@translate-tools/core/translators/Translator'; import { StringLiteralType, type } from '../lib/types'; @@ -18,9 +18,9 @@ export const ArrayOfStrings = new type.Type( export const LangCode = new type.Type( 'LangCode', (input: unknown): input is langCode => - typeof input === 'string' && langCodes.findIndex((i) => i === input) > -1, + typeof input === 'string' && isLanguageCodeISO639v1(input), (input, context) => - typeof input === 'string' && langCodes.findIndex((i) => i === input) > -1 + typeof input === 'string' && isLanguageCodeISO639v1(input) ? type.success(input as langCode) : type.failure(input, context), type.identity, @@ -33,11 +33,9 @@ export const LangCodeWithAuto = new type.Type< >( 'LangCodeWithAuto', (input: unknown): input is langCodeWithAuto => - input === 'auto' || - (typeof input === 'string' && langCodes.findIndex((i) => i === input) > -1), + input === 'auto' || (typeof input === 'string' && isLanguageCodeISO639v1(input)), (input, context) => - input === 'auto' || - (typeof input === 'string' && langCodes.findIndex((i) => i === input) > -1) + input === 'auto' || (typeof input === 'string' && isLanguageCodeISO639v1(input)) ? type.success(input as langCodeWithAuto) : type.failure(input, context), type.identity,