Skip to content

Commit af1a1df

Browse files
committed
Fixes some issues of undetected language code.
1 parent 7ac075d commit af1a1df

5 files changed

Lines changed: 65 additions & 27 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ For git tags, official releases are marked like "v1.0.0", while unofficial ones
55

66
## [Unreleased]
77

8+
### Added
9+
10+
* ``langFromDict`` and ``targetLangFromDict`` are now officially coming to the kernel utility functions.
11+
812
### Fixed
913

1014
* When editing an existing text, surrounding spaces are no longer inserted ([#92](https://github.com/HugoFara/lwt/issues/92)).
15+
* Language code is better detected from translator url when editing an existing language.
1116

1217
## 2.8.1-fork (April 14 2023)
1318

‎docs/info.html‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,9 +1842,15 @@ <h2 id="CHANGELOG" name="CHANGELOG">▶ Changelog - <a href="#">[↑]</a></h2>
18421842
<p>This project's changelog. Versions marked with "-fork" come from the community, other versions come from the canonical LWT ("official" branch on Git).
18431843
For git tags, official releases are marked like "v1.0.0", while unofficial ones are marked like "v1.0.0-fork".</p>
18441844
<h3>[Unreleased]</h3>
1845+
<h4>Added</h4>
1846+
<ul>
1847+
<li>
1848+
<code>langFromDict</code> and <code>targetLangFromDict</code> are now officially coming to the kernel utility functions.</li>
1849+
</ul>
18451850
<h4>Fixed</h4>
18461851
<ul>
18471852
<li>When editing an existing text, surrounding spaces are no longer inserted (<a href="https://github.com/HugoFara/lwt/issues/92">#92</a>).</li>
1853+
<li>Language code is better detected from translator url when editing an existing language.</li>
18481854
</ul>
18491855
<h3>2.8.1-fork (April 14 2023)</h3>
18501856
<h4>Changed</h4>

‎edit_languages.php‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -361,33 +361,6 @@ function load_language($lgid)
361361
return $language;
362362
}
363363

364-
function langFromDict($url) {
365-
if ($url == '') {
366-
return '';
367-
}
368-
$query = parse_url($url, PHP_URL_QUERY);
369-
parse_str($query, $parsed_query);
370-
if (array_key_exists("lwt_translator", $parsed_query) &&
371-
$parsed_query["lwt_translator"] == "libretranslate") {
372-
return $parsed_query["source"] || "";
373-
}
374-
// Fallback to Google Translate
375-
return $parsed_query["sl"] || "";
376-
}
377-
378-
function targetLangFromDict($url) {
379-
if ($url == '') {
380-
return '';
381-
}
382-
$query = parse_url($url, PHP_URL_QUERY);
383-
parse_str($query, $parsed_query);
384-
if (array_key_exists("lwt_translator", $parsed_query) &&
385-
$parsed_query["lwt_translator"] == "libretranslate") {
386-
return $parsed_query["target"] || "";
387-
}
388-
// Fallback to Google Translate
389-
return $parsed_query["tl"] || "";
390-
}
391364

392365
/**
393366
* Create the form for a language.

‎inc/kernel_utility.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,44 @@ function error_message_with_hide($msg, $noback): string
691691
return '<p id="hide3" class="msgblue">+++ ' . tohtml($msg) . ' +++</p>';
692692
}
693693

694+
/**
695+
* Get a two-letter language code from dictionary source language.
696+
*
697+
* @param string $url Input URL, usually Google Translate or LibreTranslate
698+
*/
699+
function langFromDict($url) {
700+
if ($url == '') {
701+
return '';
702+
}
703+
$query = parse_url($url, PHP_URL_QUERY);
704+
parse_str($query, $parsed_query);
705+
if (array_key_exists("lwt_translator", $parsed_query) &&
706+
$parsed_query["lwt_translator"] == "libretranslate") {
707+
return $parsed_query["source"] ?? "";
708+
}
709+
// Fallback to Google Translate
710+
return $parsed_query["sl"] ?? "";
711+
}
712+
713+
/**
714+
* Get a two-letter language code from dictionary target language
715+
*
716+
* @param string $url Input URL, usually Google Translate or LibreTranslate
717+
*/
718+
function targetLangFromDict($url) {
719+
if ($url == '') {
720+
return '';
721+
}
722+
$query = parse_url($url, PHP_URL_QUERY);
723+
parse_str($query, $parsed_query);
724+
if (array_key_exists("lwt_translator", $parsed_query) &&
725+
$parsed_query["lwt_translator"] == "libretranslate") {
726+
return $parsed_query["target"] ?? "";
727+
}
728+
// Fallback to Google Translate
729+
return $parsed_query["tl"] ?? "";
730+
}
731+
694732
/***************** Wrappers for PHP <8.0 ********************/
695733
if (!function_exists('str_starts_with')) {
696734
function str_starts_with($haystack, $needle)

‎tests/KernelUtilityTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,20 @@ public function testGetVersionNumber(): void
2727
$this->assertSame(10, strlen($version));
2828
}
2929

30+
/**
31+
* Test if the language from dictionary feature is properly working.
32+
*/
33+
public function testLangFromDict(): void
34+
{
35+
$urls = [
36+
'http://translate.google.com/lwt_term?ie=UTF-8&sl=ar&tl=en&text=&lwt_popup=true',
37+
'http://localhost/lwt/ggl.php/?sl=ar&tl=hr&text=',
38+
'http://localhost:5000/?lwt_translator=libretranslate&source=ar&target=en&q=lwt_term',
39+
'ggl.php?sl=ar&tl=en&text=###'
40+
];
41+
foreach ($urls as $url) {
42+
$this->assertSame("ar", langFromDict($url));
43+
}
44+
}
45+
3046
}

0 commit comments

Comments
 (0)