Skip to content

Commit

Permalink
Merge pull request #387 from translate-tools/386-update-translators-p…
Browse files Browse the repository at this point in the history
…ackage

Update translators package
  • Loading branch information
vitonsky authored Aug 7, 2023
2 parents bcc8c47 + 8375523 commit cd42033
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 36 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/app/Background/TranslatorManager/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class TranslatorManager<Translators extends TranslatorsMap = TranslatorsM
return {
supportedLanguages: translatorClass
.getSupportedLanguages()
.filter((lang) => isLanguageCodeISO639v2(lang)),
.filter((lang) => isLanguageCodeISO639v1(lang)),
isSupportAutodetect: translatorClass.isSupportedAutoFrom(),
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/Background/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -27,7 +27,7 @@ export const embeddedTranslators = {
/**
* Map where key is identifier of translator and value is translator constructor
*/
export type TranslatorsMap = Record<string, TranslatorClass>;
export type TranslatorsMap = Record<string, TranslatorConstructor>;

/**
* Background features manager
Expand Down
5 changes: 2 additions & 3 deletions src/lib/language.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
4 changes: 3 additions & 1 deletion src/pages/dictionary/layout/DictionaryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/pages/options/layout/OptionsPage.utils/generateTree.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,6 +11,8 @@ import { capitalizeString } from '../../../../lib/utils';

import { OptionsGroup } from '../OptionsTree/OptionsTree';

const langCodes = getLanguageCodesISO639('v1');

type Options = {
clearCacheProcess: boolean;
translatorModules: Record<string, string>;
Expand Down
8 changes: 4 additions & 4 deletions src/requests/backend/translate.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -8,7 +8,7 @@ export const [translateFactory, translateRequest] = buildBackendRequest<
text: string;
from: langCodeWithAuto;
to: langCode;
options?: ITranslateOptions;
options?: ISchedulerTranslateOptions;
},
string
>('translate', {
Expand All @@ -26,7 +26,7 @@ export const translate = (
text: string,
from: string,
to: string,
options?: ITranslateOptions,
options?: ISchedulerTranslateOptions,
) =>
translateRequest({
text,
Expand Down
6 changes: 4 additions & 2 deletions src/requests/backend/translators/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<TranslatorsMap> => {
const translatorsMap: Record<string, TranslatorClass> = { ...embeddedTranslators };
const translatorsMap: Record<string, TranslatorConstructor> = {
...embeddedTranslators,
};

// Validate and collect custom translators
const customTranslators = await getTranslators({ order: 'asc' });
Expand Down
6 changes: 3 additions & 3 deletions src/requests/backend/translators/utils.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -51,5 +51,5 @@ export const loadTranslator = (code: string) => {
}
});

return translatorClass as TranslatorClass;
return translatorClass as TranslatorConstructor;
};
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
14 changes: 6 additions & 8 deletions src/types/runtime.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -18,9 +18,9 @@ export const ArrayOfStrings = new type.Type<string[], string[], unknown>(
export const LangCode = new type.Type<langCode, langCode, unknown>(
'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,
Expand All @@ -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,
Expand Down

0 comments on commit cd42033

Please sign in to comment.