Skip to content

Commit

Permalink
add lingua libre audio source (#1129)
Browse files Browse the repository at this point in the history
* add lingua libre audio source

* mvp

* run file requests in parallel

* remove redundant language var

* redundant api function

---------

Co-authored-by: Cashew <[email protected]>
  • Loading branch information
StefanVukovic99 and Casheeew authored Jun 27, 2024
1 parent 4e3f23e commit 603c2c7
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 49 deletions.
1 change: 1 addition & 0 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@
"jpod101",
"jpod101-alternate",
"jisho",
"lingua-libre",
"text-to-speech",
"text-to-speech-reading",
"custom",
Expand Down
7 changes: 4 additions & 3 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ export class Backend {
}

/** @type {import('api').ApiHandler<'getTermAudioInfoList'>} */
async _onApiGetTermAudioInfoList({source, term, reading}) {
return await this._audioDownloader.getTermAudioInfoList(source, term, reading);
async _onApiGetTermAudioInfoList({source, term, reading, languageSummary}) {
return await this._audioDownloader.getTermAudioInfoList(source, term, reading, languageSummary);
}

/** @type {import('api').ApiHandler<'sendMessageToFrame'>} */
Expand Down Expand Up @@ -2171,7 +2171,7 @@ export class Backend {
const {term, reading} = definitionDetails;
if (term.length === 0 && reading.length === 0) { return null; }

const {sources, preferredAudioIndex, idleTimeout} = details;
const {sources, preferredAudioIndex, idleTimeout, languageSummary} = details;
let data;
let contentType;
try {
Expand All @@ -2181,6 +2181,7 @@ export class Backend {
term,
reading,
idleTimeout,
languageSummary,
));
} catch (e) {
const error = this._getAudioDownloadError(e);
Expand Down
5 changes: 3 additions & 2 deletions ext/js/comm/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ export class API {
* @param {import('api').ApiParam<'getTermAudioInfoList', 'source'>} source
* @param {import('api').ApiParam<'getTermAudioInfoList', 'term'>} term
* @param {import('api').ApiParam<'getTermAudioInfoList', 'reading'>} reading
* @param {import('api').ApiParam<'getTermAudioInfoList', 'languageSummary'>} languageSummary
* @returns {Promise<import('api').ApiReturn<'getTermAudioInfoList'>>}
*/
getTermAudioInfoList(source, term, reading) {
return this._invoke('getTermAudioInfoList', {source, term, reading});
getTermAudioInfoList(source, term, reading, languageSummary) {
return this._invoke('getTermAudioInfoList', {source, term, reading, languageSummary});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ext/js/data/anki-note-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ export class AnkiNoteBuilder {
if (injectAudio && dictionaryEntryDetails.type !== 'kanji') {
const audioOptions = mediaOptions.audio;
if (typeof audioOptions === 'object' && audioOptions !== null) {
const {sources, preferredAudioIndex, idleTimeout} = audioOptions;
audioDetails = {sources, preferredAudioIndex, idleTimeout};
const {sources, preferredAudioIndex, idleTimeout, languageSummary} = audioOptions;
audioDetails = {sources, preferredAudioIndex, idleTimeout, languageSummary};
}
}
if (injectScreenshot) {
Expand Down
14 changes: 12 additions & 2 deletions ext/js/display/display-anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ export class DisplayAnki {
*/
_onOptionsUpdated({options}) {
const {
general: {resultOutputMode, glossaryLayoutMode, compactTags},
general: {
resultOutputMode,
glossaryLayoutMode,
compactTags,
},
dictionaries,
anki: {
tags,
Expand Down Expand Up @@ -883,7 +887,13 @@ export class DisplayAnki {
_getAnkiNoteMediaAudioDetails(details) {
if (details.type !== 'term') { return null; }
const {sources, preferredAudioIndex} = this._displayAudio.getAnkiNoteMediaAudioDetails(details.term, details.reading);
return {sources, preferredAudioIndex, idleTimeout: this._audioDownloadIdleTimeout};
const languageSummary = this._display.getLanguageSummary();
return {
sources,
preferredAudioIndex,
idleTimeout: this._audioDownloadIdleTimeout,
languageSummary,
};
}

// View note functions
Expand Down
4 changes: 3 additions & 1 deletion ext/js/display/display-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class DisplayAudio {
['jpod101', 'JapanesePod101'],
['jpod101-alternate', 'JapanesePod101 (Alternate)'],
['jisho', 'Jisho.org'],
['lingua-libre', 'Lingua Libre'],
['text-to-speech', 'Text-to-speech'],
['text-to-speech-reading', 'Text-to-speech (Kana reading)'],
['custom', 'Custom URL'],
Expand Down Expand Up @@ -677,7 +678,8 @@ export class DisplayAudio {
*/
async _getTermAudioInfoList(source, term, reading) {
const sourceData = this._getSourceData(source);
const infoList = await this._display.application.api.getTermAudioInfoList(sourceData, term, reading);
const languageSummary = this._display.getLanguageSummary();
const infoList = await this._display.application.api.getTermAudioInfoList(sourceData, term, reading, languageSummary);
return infoList.map((info) => ({info, audioPromise: null, audioResolved: false, audio: null}));
}

Expand Down
14 changes: 14 additions & 0 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ export class Display extends EventDispatcher {
this._onMenuButtonMenuCloseBind = this._onMenuButtonMenuClose.bind(this);
/** @type {ThemeController} */
this._themeController = new ThemeController(document.documentElement);
/** @type {import('language').LanguageSummary[]} */
this._languageSummaries = [];

/* eslint-disable @stylistic/no-multi-spaces */
this._hotkeyHandler.registerActions([
Expand Down Expand Up @@ -316,6 +318,8 @@ export class Display extends EventDispatcher {
documentElement.dataset.browser = browser;
}

this._languageSummaries = await this._application.api.getLanguageSummaries();

// Prepare
await this._hotkeyHelpController.prepare(this._application.api);
await this._displayGenerator.prepare();
Expand Down Expand Up @@ -397,6 +401,16 @@ export class Display extends EventDispatcher {
return this._options;
}

/**
* @returns {import('language').LanguageSummary}
* @throws {Error}
*/
getLanguageSummary() {
if (this._options === null) { throw new Error('Options is null'); }
const language = this._options.general.language;
return /** @type {import('language').LanguageSummary} */ (this._languageSummaries.find(({iso}) => iso === language));
}

/**
* @returns {import('settings').OptionsContext}
*/
Expand Down
31 changes: 31 additions & 0 deletions ext/js/language/language-descriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const capitalizationPreprocessors = {
const languageDescriptors = [
{
iso: 'ar',
iso639_3: 'ara',
name: 'Arabic',
exampleText: 'قَرَأَ',
textPreprocessors: {
Expand All @@ -58,6 +59,7 @@ const languageDescriptors = [
},
{
iso: 'de',
iso639_3: 'deu',
name: 'German',
exampleText: 'gelesen',
textPreprocessors: {
Expand All @@ -68,26 +70,30 @@ const languageDescriptors = [
},
{
iso: 'el',
iso639_3: 'ell',
name: 'Greek',
exampleText: 'διαβάζω',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'en',
iso639_3: 'eng',
name: 'English',
exampleText: 'read',
textPreprocessors: capitalizationPreprocessors,
languageTransforms: englishTransforms,
},
{
iso: 'es',
iso639_3: 'spa',
name: 'Spanish',
exampleText: 'leer',
textPreprocessors: capitalizationPreprocessors,
languageTransforms: spanishTransforms,
},
{
iso: 'fa',
iso639_3: 'fas',
name: 'Persian',
exampleText: 'خواندن',
textPreprocessors: {
Expand All @@ -96,18 +102,21 @@ const languageDescriptors = [
},
{
iso: 'fi',
iso639_3: 'fin',
name: 'Finnish',
exampleText: 'lukea',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'fr',
iso639_3: 'fra',
name: 'French',
exampleText: 'lire',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'grc',
iso639_3: 'grc',
name: 'Ancient Greek',
exampleText: 'γράφω',
textPreprocessors: {
Expand All @@ -117,24 +126,28 @@ const languageDescriptors = [
},
{
iso: 'hu',
iso639_3: 'hun',
name: 'Hungarian',
exampleText: 'olvasni',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'id',
iso639_3: 'ind',
name: 'Indonesian',
exampleText: 'membaca',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'it',
iso639_3: 'ita',
name: 'Italian',
exampleText: 'leggere',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'la',
iso639_3: 'lat',
name: 'Latin',
exampleText: 'legere',
textPreprocessors: {
Expand All @@ -145,11 +158,13 @@ const languageDescriptors = [
},
{
iso: 'lo',
iso639_3: 'lao',
name: 'Lao',
exampleText: 'ອ່ານ',
},
{
iso: 'ja',
iso639_3: 'jpn',
name: 'Japanese',
exampleText: '読め',
isTextLookupWorthy: isStringPartiallyJapanese,
Expand All @@ -165,11 +180,13 @@ const languageDescriptors = [
},
{
iso: 'km',
iso639_3: 'khm',
name: 'Khmer',
exampleText: 'អាន',
},
{
iso: 'ko',
iso639_3: 'kor',
name: 'Korean',
exampleText: '읽어',
textPreprocessors: {
Expand All @@ -182,24 +199,28 @@ const languageDescriptors = [
},
{
iso: 'nl',
iso639_3: 'nld',
name: 'Dutch',
exampleText: 'lezen',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'pl',
iso639_3: 'pol',
name: 'Polish',
exampleText: 'czytacie',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'pt',
iso639_3: 'por',
name: 'Portuguese',
exampleText: 'ler',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'ro',
iso639_3: 'ron',
name: 'Romanian',
exampleText: 'citit',
textPreprocessors: {
Expand All @@ -209,6 +230,7 @@ const languageDescriptors = [
},
{
iso: 'ru',
iso639_3: 'rus',
name: 'Russian',
exampleText: 'читать',
textPreprocessors: {
Expand All @@ -219,6 +241,7 @@ const languageDescriptors = [
},
{
iso: 'sga',
iso639_3: 'sga',
name: 'Old Irish',
exampleText: 'légaid',
textPreprocessors: {
Expand All @@ -229,6 +252,7 @@ const languageDescriptors = [
},
{
iso: 'sh',
iso639_3: 'hbs',
name: 'Serbo-Croatian',
exampleText: 'čitaše',
textPreprocessors: {
Expand All @@ -238,30 +262,35 @@ const languageDescriptors = [
},
{
iso: 'sq',
iso639_3: 'sqi',
name: 'Albanian',
exampleText: 'ndihmojme',
textPreprocessors: capitalizationPreprocessors,
languageTransforms: albanianTransforms,
},
{
iso: 'sv',
iso639_3: 'swe',
name: 'Swedish',
exampleText: 'läsa',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'th',
iso639_3: 'tha',
name: 'Thai',
exampleText: 'อ่าน',
},
{
iso: 'tr',
iso639_3: 'tur',
name: 'Turkish',
exampleText: 'okuyor',
textPreprocessors: capitalizationPreprocessors,
},
{
iso: 'vi',
iso639_3: 'vie',
name: 'Vietnamese',
exampleText: 'đọc',
textPreprocessors: {
Expand All @@ -271,11 +300,13 @@ const languageDescriptors = [
},
{
iso: 'yue',
iso639_3: 'yue',
name: 'Cantonese',
exampleText: '讀',
},
{
iso: 'zh',
iso639_3: 'zho',
name: 'Chinese',
exampleText: '读',
isTextLookupWorthy: isStringPartiallyChinese,
Expand Down
4 changes: 2 additions & 2 deletions ext/js/language/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {languageDescriptorMap} from './language-descriptors.js';
*/
export function getLanguageSummaries() {
const results = [];
for (const {name, iso, exampleText} of languageDescriptorMap.values()) {
results.push({name, iso, exampleText});
for (const {name, iso, iso639_3, exampleText} of languageDescriptorMap.values()) {
results.push({name, iso, iso639_3, exampleText});
}
return results;
}
Expand Down
Loading

0 comments on commit 603c2c7

Please sign in to comment.