From a9b6f4e889f95b76eab3438fb0e4c06ee6cc9853 Mon Sep 17 00:00:00 2001 From: Stefan Vukovic Date: Sat, 3 Feb 2024 22:13:47 +0100 Subject: [PATCH] fix language index --- ext/js/background/backend.js | 2 -- ext/js/background/offscreen-proxy.js | 7 ------- ext/js/background/offscreen.js | 6 ------ ext/js/language/index.js | 12 ++++++++++-- ext/js/language/index.json | 4 ---- ext/js/language/language-util.js | 18 +++--------------- test/data/json.json | 5 ----- test/fixtures/translator-test.js | 1 - types/ext/language.d.ts | 17 +++++------------ types/ext/offscreen.d.ts | 4 ---- 10 files changed, 18 insertions(+), 58 deletions(-) delete mode 100644 ext/js/language/index.json diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 512197979e..709b15f344 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -290,8 +290,6 @@ export class Backend { this._defaultAnkiFieldTemplates = (await fetchText('/data/templates/default-anki-field-templates.handlebars')).trim(); this._options = await this._optionsUtil.load(); - await this._languageUtil.prepare(); - /** @type {import('language-transformer').LanguageTransformDescriptor} */ const descriptor = await fetchJson('/data/language/japanese-transforms.json'); this._translator.prepare(descriptor); diff --git a/ext/js/background/offscreen-proxy.js b/ext/js/background/offscreen-proxy.js index 095132b4ec..7aacdb6f82 100644 --- a/ext/js/background/offscreen-proxy.js +++ b/ext/js/background/offscreen-proxy.js @@ -158,13 +158,6 @@ export class LanguageUtilProxy { this._offscreen = offscreen; } - /** - * @returns {Promise} - */ - async prepare() { - await this._offscreen.sendMessagePromise({action: 'languageUtilPrepareOffscreen'}); - } - /** * @param {string} language * @returns {Promise} diff --git a/ext/js/background/offscreen.js b/ext/js/background/offscreen.js index 7f2635c76d..00851829d2 100644 --- a/ext/js/background/offscreen.js +++ b/ext/js/background/offscreen.js @@ -61,7 +61,6 @@ export class Offscreen { ['databasePurgeOffscreen', this._purgeDatabaseHandler.bind(this)], ['databaseGetMediaOffscreen', this._getMediaHandler.bind(this)], ['translatorPrepareOffscreen', this._prepareTranslatorHandler.bind(this)], - ['languageUtilPrepareOffscreen', this._prepareLanguageUtilHandler.bind(this)], ['getLanguagesOffscreen', this._getLanguagesHandler.bind(this)], ['getTextTransformationsOffscreen', this._getTextTransformationsHandler.bind(this)], ['findKanjiOffscreen', this._findKanjiHandler.bind(this)], @@ -126,11 +125,6 @@ export class Offscreen { this._translator.prepare(descriptor); } - /** @type {import('offscreen').ApiHandler<'languageUtilPrepareOffscreen'>}*/ - _prepareLanguageUtilHandler() { - this._languageUtil.prepare(); - } - /** @type {import('offscreen').ApiHandler<'getLanguagesOffscreen'>}*/ _getLanguagesHandler() { return this._languageUtil.getLanguages(); diff --git a/ext/js/language/index.js b/ext/js/language/index.js index 617bb1bd2e..397ecbeed7 100755 --- a/ext/js/language/index.js +++ b/ext/js/language/index.js @@ -18,12 +18,20 @@ import {textTransformations as textTransformationsEN} from './en/text-transformations.js'; import {textTransformations as textTransformationsJA} from './ja/text-transformations.js'; -/** @type {Map} */ -export const languageFeatures = new Map([ +/** @type {Map} */ +export const languages = new Map([ ['ja', { + name: 'Japanese', + iso: 'ja', + flag: '🇯🇵', + exampleText: '読め', textTransformations: textTransformationsJA }], ['en', { + name: 'English', + iso: 'en', + flag: '🇬🇧', + exampleText: 'read', textTransformations: textTransformationsEN }] ]); diff --git a/ext/js/language/index.json b/ext/js/language/index.json deleted file mode 100644 index 35f8aef3ba..0000000000 --- a/ext/js/language/index.json +++ /dev/null @@ -1,4 +0,0 @@ -[ - {"name": "English", "iso": "en", "flag": "🇬🇧", "exampleText": "read"}, - {"name": "Japanese", "iso": "ja", "flag": "🇯🇵", "exampleText": "読め"} -] \ No newline at end of file diff --git a/ext/js/language/language-util.js b/ext/js/language/language-util.js index 16ae007ef2..61f9688ed6 100644 --- a/ext/js/language/language-util.js +++ b/ext/js/language/language-util.js @@ -15,28 +15,16 @@ * along with this program. If not, see . */ -import {fetchJson} from '../core/utilities.js'; -import {languageFeatures} from './index.js'; +import {languages} from './index.js'; export class LanguageUtil { constructor() { - /** @type {import('language').LanguageMap}*/ - this.languages = new Map(); } - /** */ - async prepare() { - /** @type {import('language').Language[]} */ - const languages = await fetchJson('/js/language/index.json'); - for (const {iso, name, flag, exampleText} of languages) { - const features = languageFeatures.get(iso) || {textTransformations: []}; - this.languages.set(iso, {...features, iso, name, flag, exampleText}); - } - } /** @returns {import('language').Language[]}*/ getLanguages() { - return [...this.languages.values()]; + return [...languages.values()]; } /** @@ -44,7 +32,7 @@ export class LanguageUtil { * @returns {import('language').TextTransformation[]} */ getTextTransformations(iso) { - return this.languages.get(iso)?.textTransformations || []; + return languages.get(iso)?.textTransformations || []; } } diff --git a/test/data/json.json b/test/data/json.json index 10e03321d8..1f664587f0 100644 --- a/test/data/json.json +++ b/test/data/json.json @@ -95,11 +95,6 @@ "typeFile": "types/ext/language-transformer.d.ts", "type": "LanguageTransformDescriptor" }, - { - "path": "ext/js/language/index.json", - "typeFile": "types/ext/language.d.ts", - "type": "LanguagePropertiesArray" - }, { "path": "test/data/translator-test-inputs.json", "typeFile": "types/test/translator.d.ts", diff --git a/test/fixtures/translator-test.js b/test/fixtures/translator-test.js index 742b0a0e04..367cd636a2 100644 --- a/test/fixtures/translator-test.js +++ b/test/fixtures/translator-test.js @@ -55,7 +55,6 @@ async function createTranslatorContext(dictionaryDirectory, dictionaryName) { const dictionaryDatabase = new DictionaryDatabase(); await dictionaryDatabase.prepare(); const languageUtil = new LanguageUtil(); - languageUtil.prepare(); const {errors} = await dictionaryImporter.importDictionary( dictionaryDatabase, diff --git a/types/ext/language.d.ts b/types/ext/language.d.ts index 8ea46cae88..58f6e19069 100644 --- a/types/ext/language.d.ts +++ b/types/ext/language.d.ts @@ -17,15 +17,6 @@ import {TextSourceMap} from '../../ext/js/general/text-source-map.js'; -export type LanguageProperties = { - name: string; - iso: string; - flag: string; - exampleText: string; -}; - -export type LanguagePropertiesArray = LanguageProperties[]; - export type TextTransformationOption = [ value: string, label: string, @@ -40,10 +31,12 @@ export type TextTransformation = { transform: (str: string, setting: T, sourceMap?: TextSourceMap) => string; }; -export type LanguageFeatures = { +export type Language = { + name: string; + iso: string; + flag: string; + exampleText: string; textTransformations: TextTransformation[]; }; -export type Language = LanguageProperties & LanguageFeatures; - export type LanguageMap = Map; \ No newline at end of file diff --git a/types/ext/offscreen.d.ts b/types/ext/offscreen.d.ts index efec3e5617..77548c4dde 100644 --- a/types/ext/offscreen.d.ts +++ b/types/ext/offscreen.d.ts @@ -52,10 +52,6 @@ type ApiSurface = { }; return: DictionaryDatabase.Media[]; }; - languageUtilPrepareOffscreen: { - params: void; - return: void; - }; getLanguagesOffscreen: { params: void; return: Language.Language[];