Skip to content

Commit

Permalink
fix language index
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Feb 3, 2024
1 parent b204d3e commit a9b6f4e
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 58 deletions.
2 changes: 0 additions & 2 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions ext/js/background/offscreen-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ export class LanguageUtilProxy {
this._offscreen = offscreen;
}

/**
* @returns {Promise<void>}
*/
async prepare() {
await this._offscreen.sendMessagePromise({action: 'languageUtilPrepareOffscreen'});
}

/**
* @param {string} language
* @returns {Promise<import('language').TextTransformation[]>}
Expand Down
6 changes: 0 additions & 6 deletions ext/js/background/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions ext/js/language/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@
import {textTransformations as textTransformationsEN} from './en/text-transformations.js';
import {textTransformations as textTransformationsJA} from './ja/text-transformations.js';

/** @type {Map<string, import('language').LanguageFeatures>} */
export const languageFeatures = new Map([
/** @type {Map<string, import('language').Language>} */
export const languages = new Map([
['ja', {
name: 'Japanese',
iso: 'ja',
flag: '🇯🇵',
exampleText: '読め',
textTransformations: textTransformationsJA
}],
['en', {
name: 'English',
iso: 'en',
flag: '🇬🇧',
exampleText: 'read',
textTransformations: textTransformationsEN
}]
]);
Expand Down
4 changes: 0 additions & 4 deletions ext/js/language/index.json

This file was deleted.

18 changes: 3 additions & 15 deletions ext/js/language/language-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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()];
}

/**
* @param {string} iso
* @returns {import('language').TextTransformation[]}
*/
getTextTransformations(iso) {
return this.languages.get(iso)?.textTransformations || [];
return languages.get(iso)?.textTransformations || [];
}
}

5 changes: 0 additions & 5 deletions test/data/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/translator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 5 additions & 12 deletions types/ext/language.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = unknown> = [
value: string,
label: string,
Expand All @@ -40,10 +31,12 @@ export type TextTransformation<T = unknown> = {
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<string, Language>;
4 changes: 0 additions & 4 deletions types/ext/offscreen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ type ApiSurface = {
};
return: DictionaryDatabase.Media<string>[];
};
languageUtilPrepareOffscreen: {
params: void;
return: void;
};
getLanguagesOffscreen: {
params: void;
return: Language.Language[];
Expand Down

0 comments on commit a9b6f4e

Please sign in to comment.