Skip to content

Commit

Permalink
Fixes #113: changing a language name do no longer break text-to-speec…
Browse files Browse the repository at this point in the history
…h (not always).
  • Loading branch information
HugoFara committed Dec 26, 2023
1 parent a270782 commit 164c570
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
12 changes: 1 addition & 11 deletions do_test_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,21 +699,11 @@ function do_test_test_interaction_globals($wb1, $wb2, $wb3)
* @param string $save Word or sentence to use for the test
*
* @return void
*
* @global string $tbpref Database table prefix
* @global string $angDefs Languages definition array
*/
function do_test_test_javascript_clickable($wo_record, $solution)
{
global $tbpref, $langDefs;

$wid = $wo_record['WoID'];
$lang = get_first_value(
'SELECT LgName AS value FROM ' . $tbpref . 'languages
WHERE LgID = ' . $wo_record['WoLgID'] . '
LIMIT 1'
);
$abbr = $langDefs[$lang][1];
$abbr = getLanguageCode($wo_record['WoLgID'], LWT_LANGUAGES_ARRAY);
$phoneticText = phonetic_reading($wo_record['WoText'], $abbr);
?>
<script type="text/javascript">
Expand Down
24 changes: 11 additions & 13 deletions do_text_header.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,21 @@ function do_settings($textid): void
* @param string $text Text to read
* @param string $languageName Full name of the language (i. e.: "English")
*
* @global array $langDefs Definition of all languages. Normally $langDefs[$languageName][1] -> $languageCode
*
* @global string $tbpref
*
* @since 2.0.3-fork
* @since 2.9.1-fork Function may work even when the language name was manually changed.
*/
function browser_tts($text, $languageName): void
{
global $langDefs;

/**
* @var string $languageCode BCP 47 convention (i. e.: en-US) is suggested.
* Two-letter language code is enough (i. e. "en")
*/
$languageCode = $langDefs[$languageName][1];
/**
* @var string $phoneticText
* Phonetic reading for this text
*/
global $tbpref;
$lg_id = (int) get_first_value(
"SELECT LgID as value
FROM {$tbpref}languages
WHERE LgName = " . convert_string_to_sqlsyntax($languageName)
);
$languageCode = getLanguageCode($lg_id, LWT_LANGUAGES_ARRAY);
// Phonetic reading for this text
$phoneticText = phonetic_reading($text, $languageCode);
?>
<script type="text/javascript">
Expand Down
10 changes: 7 additions & 3 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ ones are marked like "v1.0.0-fork".
* New tools for documentation:
* New JavaScript documentation with [JSDoc](https://jsdoc.app/)! You will find it under `docs/js/`.
* Better PHP documentation using [phpDocumentor](https://phpdoc.org/). The new documentation is at `docs/php/`.
* New constant `LWT_LANG_DEFS`, that replaces `$langDefs` (in `inc/langdefs.php`). Part of [#163](https://github.com/HugoFara/lwt/issues/163).
* New constant `LWT_LANGUAGES_ARRAY`, that replaces `$langDefs` (in `inc/langdefs.php`). Part of [#163](https://github.com/HugoFara/lwt/issues/163).

## Changed
### Changed

* PHP files header docblocks are expanded, notably with a PHP version, package and category name.

### Fixed

* Fixes [#113](https://github.com/HugoFara/lwt/issues/113): changing a language name was making text-to-speech unusable and may prompt a warning.

### Deprecated

* PHP documentation using Doxygen (under `/docs/html`) is now deprecated and will be removed in 3.0.0.
* Using `$langDefs` is deprecated in favor of `LWT_LANG_DEFS` ([#163](https://github.com/HugoFara/lwt/issues/163)).
* Using `$langDefs` is deprecated in favor of `LWT_LANGUAGES_ARRAY` ([#163](https://github.com/HugoFara/lwt/issues/163)).

## 2.9.0-fork (December 24 2023)

Expand Down
10 changes: 7 additions & 3 deletions docs/info.html
Original file line number Diff line number Diff line change
Expand Up @@ -2175,16 +2175,20 @@ <h4>Added</h4>
<li>Better PHP documentation using <a href="https://phpdoc.org/">phpDocumentor</a>. The new documentation is at <code>docs/php/</code>.</li>
</ul>
</li>
<li>New constant <code>LWT_LANG_DEFS</code>, that replaces <code>$langDefs</code> (in <code>inc/langdefs.php</code>). Part of <a href="https://github.com/HugoFara/lwt/issues/163">#163</a>.</li>
<li>New constant <code>LWT_LANGUAGES_ARRAY</code>, that replaces <code>$langDefs</code> (in <code>inc/langdefs.php</code>). Part of <a href="https://github.com/HugoFara/lwt/issues/163">#163</a>.</li>
</ul>
<h3>Changed</h3>
<h4>Changed</h4>
<ul>
<li>PHP files header docblocks are expanded, notably with a PHP version, package and category name.</li>
</ul>
<h4>Fixed</h4>
<ul>
<li>Fixes <a href="https://github.com/HugoFara/lwt/issues/113">#113</a>: changing a language name was making text-to-speech unusable and may prompt a warning.</li>
</ul>
<h4>Deprecated</h4>
<ul>
<li>PHP documentation using Doxygen (under <code>/docs/html</code>) is now deprecated and will be removed in 3.0.0.</li>
<li>Using <code>$langDefs</code> is deprecated in favor of <code>LWT_LANG_DEFS</code> (<a href="https://github.com/HugoFara/lwt/issues/163">#163</a>).</li>
<li>Using <code>$langDefs</code> is deprecated in favor of <code>LWT_LANGUAGES_ARRAY</code> (<a href="https://github.com/HugoFara/lwt/issues/163">#163</a>).</li>
</ul>
<h3>2.9.0-fork (December 24 2023)</h3>
<h4>Added in 2.9.0-fork</h4>
Expand Down
8 changes: 4 additions & 4 deletions inc/langdefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @package Lwt
* @author LWT Project <[email protected]>
* @since 2.9.1 Use LWT_LANG_DEFS instead of $langDefs
* @since 2.9.1 Use LWT_LANGUAGES_ARRAY instead of $langDefs
*/

/**
Expand All @@ -19,7 +19,7 @@
* "sentSplRegExp", makeCharacterWord, removeSpaces, rightToLeft)
*/
define(
'LWT_LANG_DEFS',
'LWT_LANGUAGES_ARRAY',
array(
"Afrikaans" => array(
"af", "af", false,
Expand Down Expand Up @@ -300,9 +300,9 @@
);

/**
* @var array $langDefs Old name for LWT_LANG_DEFS.
* @var array $langDefs Old name for LWT_LANGUAGES_ARRAY.
*
* @deprecated 2.9.1-fork, will be removed in 3.0.0
*/
$langDefs = LWT_LANG_DEFS;
$langDefs = LWT_LANGUAGES_ARRAY;
?>
49 changes: 45 additions & 4 deletions inc/session_utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -4006,17 +4006,21 @@ function get_languages(): array
/**
* Get language name from its ID
*
* @param string $lid Language ID
* @param string|int $lid Language ID
*
* @return string Language name
* @global string $tbpref Table name prefix
*/
function getLanguage($lid)
{
global $tbpref;
if (!isset($lid) || trim($lid) == '' || !is_numeric($lid)) {
return '';
if (is_int($lid)) {
$lg_id = $lid;
} else if (isset($lid) && trim($lid) != '' && ctype_digit($lid)) {
$lg_id = (int) $lid;
} else {
return '';
}
$lg_id = (int) $lid;
$r = get_first_value(
"SELECT LgName AS value
FROM {$tbpref}languages
Expand All @@ -4028,6 +4032,43 @@ function getLanguage($lid)
return '';
}


/**
* Try to get language code from its ID
*
* @param int $lg_id Language ID
* @param array $languages_table Table of languages, usually LWT_LANGUAGES_ARRAY
*
* @return string If found, two-letter code (e. g. BCP 47) or four-letters for the langugae. '' otherwise.
*
* @global string $tbpref
*/
function getLanguageCode($lg_id, $languages_table)
{
global $tbpref;
$query = "SELECT LgName, LgGoogleTranslateURI
FROM {$tbpref}languages
WHERE LgID = $lg_id";

$res = do_mysqli_query($query);
$record = mysqli_fetch_assoc($res);
mysqli_free_result($res);
$lg_name = (string) $record["LgName"];
$translator_uri = (string) $record["LgGoogleTranslateURI"];

// If we are using a standard language name, use it
if (array_key_exists($lg_name, $languages_table)) {
return $languages_table[$lg_name][1];
}

// Otherwise, use the translator URL
$lgFromDict = langFromDict($translator_uri);
if ($lgFromDict != '') {
return $lgFromDict;
}
return '';
}

/**
* Return a right-to-left direction indication in HTML if language is right-to-left.
*
Expand Down

0 comments on commit 164c570

Please sign in to comment.