diff --git a/api.php b/api.php index 8bef8e5e..088e9c58 100644 --- a/api.php +++ b/api.php @@ -12,6 +12,7 @@ require_once __DIR__ . '/inc/ajax_save_text_position.php'; require_once __DIR__ . '/inc/ajax_show_imported_terms.php'; require_once __DIR__ . '/inc/ajax_edit_impr_text.php'; +require_once __DIR__ . '/inc/langdefs.php'; /** @@ -52,6 +53,9 @@ function endpoint_exits($method, $requestUri) { // Set up API endpoints $endpoints = [ + 'languages' => [ 'GET' ], + //'languages/(?\d+)/reading-configuration' => [ 'GET' ], + 'media-files' => [ 'GET' ], 'phonetic-reading' => [ 'GET' ], @@ -153,6 +157,36 @@ function media_files($get_req) return get_media_paths(); } + +/** + * The way text should be read + */ +function readingConfiguration($get_req): array +{ + global $tbpref; + // language, voiceAPI, abbr + $req = do_mysqli_query( + "SELECT LgName, LgTTSVoiceAPI, LgRegexpWordCharacters FROM {$tbpref}languages + WHERE LgID = " . $get_req["lang_id"] + ); + $record = mysqli_fetch_assoc($req); + $abbr = getLanguageCode($get_req["lang_id"], LWT_LANGUAGES_ARRAY); + if ($record["LgTTSVoiceAPI"] != '') { + $readingMode = "external"; + } elseif ($record["LgRegexpWordCharacters"] == "mecab") { + $readingMode = "internal"; + } else { + $readingMode = "direct"; + } + return array( + "name" => $record["LgName"], + "voiceapi" => $record["LgTTSVoiceAPI"], + "word_parsing" => $record["LgRegexpWordCharacters"], + "abbreviation" => $abbr, + "reading_mode" => $readingMode + ); +} + /** * Get the phonetic reading of a word based on it's language. * @@ -161,10 +195,16 @@ function media_files($get_req) * @return string[] JSON-encoded result * * @psalm-return array{phonetic_reading: string} + * + * @since 2.10.0-fork Can also accept a language ID with "lgid" parameter */ function get_phonetic_reading($get_req): array { - $data = phonetic_reading($get_req['text'], $get_req['lang']); + if (array_key_exists("lang_id", $get_req)) { + $data = phoneticReading($get_req['text'], $get_req['lang_id']); + } else { + $data = phonetic_reading($get_req['text'], $get_req['lang']); + } return array("phonetic_reading" => $data); } @@ -634,6 +674,27 @@ function request_handler($method, $requestUri, $post_param) parse_str($uri_query, $req_param); } switch ($endpoint_fragments[0]) { + case 'languages': + if (ctype_digit($endpoint_fragments[1])) { + if ($endpoint_fragments[2] == 'reading-configuration') { + $req_param['lang_id'] = (int) $endpoint_fragments[1]; + $answer = readingConfiguration($req_param); + send_response(200, $answer); + } else { + send_response( + 404, + ['error' => 'Expected "reading-configuration", Got ' . + $endpoint_fragments[2]] + ); + } + } else { + send_response( + 404, + ['error' => 'Expected Language ID, found ' . + $endpoint_fragments[1]] + ); + } + break; case 'media-files': $answer = media_files($req_param); send_response(200, $answer); diff --git a/do_test_test.php b/do_test_test.php index 2b87ceba..be2661da 100644 --- a/do_test_test.php +++ b/do_test_test.php @@ -722,8 +722,6 @@ function do_test_test_javascript_clickable($wo_record, $solution) { global $tbpref; $wid = $wo_record['WoID']; - $abbr = getLanguageCode($wo_record['WoLgID'], LWT_LANGUAGES_ARRAY); - $phoneticText = phonetic_reading($wo_record['WoText'], $abbr); $voiceApi = get_first_value( "SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages WHERE LgID = " . $wo_record['WoLgID'] @@ -734,14 +732,13 @@ function do_test_test_javascript_clickable($wo_record, $solution) * Read the word aloud */ function read_word() { - if (('speechSynthesis' in window) && - document.getElementById('utterance-allowed').checked) { - const text = ; - const lang = ; - readRawTextAloud(text, lang); - } + speechDispatcher( + , + + ); } + LWT_DATA.language.id = ; LWT_DATA.test.solution = ; LWT_DATA.word.id = ; LWT_DATA.language.ttsVoiceApi = ; diff --git a/do_text_header.php b/do_text_header.php index 697a987e..37279f60 100644 --- a/do_text_header.php +++ b/do_text_header.php @@ -187,8 +187,8 @@ function browser_tts($text, $languageName): void WHERE LgName = " . convert_string_to_sqlsyntax($languageName) ); $languageCode = getLanguageCode($lg_id, LWT_LANGUAGES_ARRAY); - // Phonetic reading for this text - $phoneticText = phonetic_reading($text, $languageCode); + // Phonetic reading for this text. As it can be long do not use AJAX + $phoneticText = phoneticReading($text, $lg_id); $voiceApi = get_first_value( "SELECT LgTTSVoiceAPI AS value FROM {$tbpref}languages WHERE LgID = $lg_id" diff --git a/do_text_text.php b/do_text_text.php index 4d5ce2dd..cb145b4a 100644 --- a/do_text_text.php +++ b/do_text_text.php @@ -797,33 +797,34 @@ function do_text_text_content($textid, $only_body = true): void 'LWT_DATA' => array( 'language' => array( - 'dict_link1' => $wb1, - 'dict_link2' => $wb2, + 'id' => $langid, + 'dict_link1' => $wb1, + 'dict_link2' => $wb2, 'translator_link' => $wb3, - 'delimiter' => tohtml( + 'delimiter' => tohtml( str_replace( array('\\',']','-','^'), array('\\\\','\\]','\\-','\\^'), getSettingWithDefault('set-term-translation-delimiters') ) ), - 'word_parsing' => $record['LgRegexpWordCharacters'], - 'rtl' => $rtlScript + 'word_parsing' => $record['LgRegexpWordCharacters'], + 'rtl' => $rtlScript ), 'text' => array( - 'id' => $textid, + 'id' => $textid, 'reading_position' => $pos, - 'annotations' => json_decode(annotation_to_json($ann)) + 'annotations' => json_decode(annotation_to_json($ann)) ), 'settings' => array( - 'jQuery_tooltip' => ( + 'jQuery_tooltip' => ( getSettingWithDefault('set-tooltip-mode') == 2 ? 1 : 0 ), - 'hts' => getSettingWithDefault('set-hts'), + 'hts' => getSettingWithDefault('set-hts'), 'word_status_filter' => makeStatusClassFilter((int)$visit_status), - 'annotations_mode' => $mode_trans + 'annotations_mode' => $mode_trans ), ) ); diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1d9723f7..fe063772 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -45,7 +45,7 @@ Many thanks to [@ProgramComputer](https://github.com/ProgramComputer)! See [#160](https://github.com/HugoFara/lwt/issues/160). It adds `README.md` and `UNLICENSE.md`. * Adds `docs/info.html` and `docs/index.html` from [#146](https://github.com/HugoFara/lwt/pull/146). -* Multi-word creation was reviewed to use a simpler code. +* Multi-word creation was reviewed to use a simpler JS code. * On word review, status + 1 can be set only after word display. Before, you could press key up at any time to increase term status, but keydown was effective only after solution display ([#159](https://github.com/HugoFara/lwt/issues/159)). @@ -58,8 +58,10 @@ after solution display ([#159](https://github.com/HugoFara/lwt/issues/159)). It defines interactions with a text on reading. * For audio URI, raises the characters length limit from 200 to 2048 ([#144](https://github.com/HugoFara/lwt/issues/144)). -* Database creation and migration is now handled directly in SQL, for a safer +* Database: + * Database creation and migration is now handled directly in SQL, for a safer behavior. + * You do no longer need to give to LWT SYSTEM_VARIABLES_ADMIN for text reparsing. (new function `checkExpressions`) ([#167](https://github.com/HugoFara/lwt/issues/167)). It is still necessary for words import. * Docker changes: * By default, the images now runs at localhot/lwt and no longer in the root folder ([installer #4](https://github.com/HugoFara/lwt-docker-installer/pull/4) @@ -75,8 +77,6 @@ behavior. Solves [#129](https://github.com/HugoFara/lwt/issues/129), thanks to the help of PR [#168](https://github.com/HugoFara/lwt/pull/168). * Text reading position was not working consistently when adding several known words. -* Japanese was always requiring MeCab for TTS, even if it was not used -([#155](https://github.com/HugoFara/lwt/pull/155)). * Multi-words: * Tooltip was not properly saved ([#170](https://github.com/HugoFara/lwt/pull/170)). * Translation may be escaped two times ([#170](https://github.com/HugoFara/lwt/pull/170)). @@ -90,6 +90,8 @@ Solves [#129](https://github.com/HugoFara/lwt/issues/129), thanks to the help of * Parsing Japanese texts with MeCab was creating warnings when the text was not finishing by a punctation mark. * Multi-words were not saved with MeCab parsing on Japanese. + * Japanese was always requiring MeCab for TTS, even if it was not used + ([#155](https://github.com/HugoFara/lwt/pull/155)) and [#182](https://github.com/HugoFara/lwt/pull/182). * On word review (test): * the space keyboard shortcut may have been inoperating. * On `api.php`, tests were always set to "multi-word" due to a missing variable diff --git a/docs/api.md b/docs/api.md index 324e915c..1ca1f193 100644 --- a/docs/api.md +++ b/docs/api.md @@ -32,13 +32,21 @@ Retrieves the files paths to the audio and video files in the media folder. - **Method**: GET - **Arguments**: None +### Get Reading Configuration + +Retrieves how word should be read for a specific language. + +- **Endpoint**: `/languages/{lang-id}/reading-configuration` +- **Method**: GET +- **Arguments**: None + ### Get Phonetic Reading Retrieves the phonetic reading for any specified term. - **Endpoint**: `/phonetic-reading` - **Method**: GET -- **Arguments**: `text` and `lang`. +- **Arguments**: `text` and (`lang` or `lg_id`). ### Get Next Word to Review diff --git a/docs/info.html b/docs/info.html index 875de7a3..addffc46 100644 --- a/docs/info.html +++ b/docs/info.html @@ -1779,6 +1779,17 @@

Get Media Files Paths

  • Arguments: None
  • +

    Get Reading Configuration

    +

    Retrieves how word should be read for a specific language.

    +

    Get Phonetic Reading

    Retrieves the phonetic reading for any specified term.

    Get Next Word to Review

    Retrieves the next word to be reviewed.

    @@ -2254,8 +2265,9 @@

    Added

    db/schema/baseline.sql and no longer in PHP code.
  • You can choose to add romanization for languages that don't need it in the language settings (#119).
  • -
  • Docker images are now built for multiple platforms, see -#159. +
  • Docker images are now built for multiple platforms, see PR +#169, closing discussion +#141. Many thanks to @ProgramComputer!
  • Changed

    @@ -2268,7 +2280,7 @@

    Changed

  • Adds docs/info.html and docs/index.html from #146.
  • -
  • Multi-word creation was reviewed to use a simpler code.
  • +
  • Multi-word creation was reviewed to use a simpler JS code.
  • On word review, status + 1 can be set only after word display. Before, you could press key up at any time to increase term status, but keydown was effective only after solution display (#159).
  • @@ -2287,8 +2299,13 @@

    Changed

  • For audio URI, raises the characters length limit from 200 to 2048 (#144).
  • +
  • Database: + +
  • Docker changes: