#289 [Suggestion] Notice translation with LibreTranslate

Open
opened 1 year ago by aab · 6 comments
aab commented 1 year ago

I installed LibreTranslate [https://github.com/LibreTranslate/LibreTranslate] about a month ago. I'm really impressed by the quality of the translations, and I think it would be a really handy tool to integrate with GNUSocial.

It offers an API, so, I think it should be easy to install in the same machine as GS and maybe add a button in every notice so it can be translated on the fly.

Anyway, I'm not a programmer, and maybe it is harder than it looks to me. Perhaps kim's https://notabug.org/kim/YandexTranslate could be reused for this?

I installed LibreTranslate [https://github.com/LibreTranslate/LibreTranslate] about a month ago. I'm really impressed by the quality of the translations, and I think it would be a really handy tool to integrate with GNUSocial. It offers an API, so, I think it should be easy to install in the same machine as GS and maybe add a button in every notice so it can be translated on the fly. Anyway, I'm not a programmer, and maybe it is harder than it looks to me. Perhaps kim's https://notabug.org/kim/YandexTranslate could be reused for this?
aab commented 1 year ago
Poster

P.D. I forgot the image :)

P.D. I forgot the image :)
aab commented 1 year ago
Poster

I've noticed today that there is a PHP Interface to LibreTranslate already. I'll toy with it to see if I can get something working: https://github.com/jefs42/libretranslate

I've noticed today that there is a PHP Interface to LibreTranslate already. I'll toy with it to see if I can get something working: https://github.com/jefs42/libretranslate
aab commented 1 year ago
Poster

Ok, I'm stuck (as expected :) What I've done so far:

  • Git clone Chimo's "Gs-translate" plugin, as it seems more complete and configurable than Kim's: https://github.com/chimo/gs-translate

  • In "TranslateNoticePlugin.php", line 15, changed to

function onRouterInitialized($m): bool

    {
  • In the same file, line 72, changed to

function onPluginVersion(array &$versions): bool

{

Both changes are necessary, or else the site fails to load. Also, changed lines 3 to 5 to "defined('GNUSOCIAL') || die();", as seen in current plugins.

  • Downloaded https://github.com/jefs42/libretranslate/blob/main/src/LibreTranslate.php, the PHP library for LibreTranslate, into /plugins/TranslateNotice/lib/. Added apiKey and apiPort in that file.

  • Changed every ocurrence in the plugin directory files form /local/plugins to /plugins.

  • Changed every ocurrence in the plugin directory files from /plugins/TranslateNotice/lib/YandexTranslator.php to /plugins/TranslateNotice/lib/LibreTranslate.php.

  • Added in GS config.php:

addPlugin('TranslateNotice', array(

'api_key'     => 'api_key'

));

with an api key from the local instance of LibreTranslate.

From here, there's a new empty space between the "delete" and "reply" buttons of every notice; it seems that icons are missing. Anyway, clicking in the empty space just returns # added at the end of the address, so the page scrolls up. Nice, but not the intended result :)

Using Firefox network inspector, it shows 404 errors when loading /plugins/TranslateNotice/css/gs-translate.css and /plugins/TranslateNotice/js/gs-translate.js. Both files exist, and they are in the correct location.

Also, a new entry called "Translate notice" appears on settings, but it just shows what you can see in the attached image.

Any hint about what should I change? Or is this just a crazy idea? (No feelings will be hurted if that's the case :)

Ok, I'm stuck (as expected :) What I've done so far: - Git clone Chimo's "Gs-translate" plugin, as it seems more complete and configurable than Kim's: https://github.com/chimo/gs-translate - In "TranslateNoticePlugin.php", line 15, changed to function onRouterInitialized($m): bool { - In the same file, line 72, changed to function onPluginVersion(array &$versions): bool { Both changes are necessary, or else the site fails to load. Also, changed lines 3 to 5 to "defined('GNUSOCIAL') || die();", as seen in current plugins. - Downloaded https://github.com/jefs42/libretranslate/blob/main/src/LibreTranslate.php, the PHP library for LibreTranslate, into /plugins/TranslateNotice/lib/. Added apiKey and apiPort in that file. - Changed every ocurrence in the plugin directory files form /local/plugins to /plugins. - Changed every ocurrence in the plugin directory files from /plugins/TranslateNotice/lib/YandexTranslator.php to /plugins/TranslateNotice/lib/LibreTranslate.php. - Added in GS config.php: addPlugin('TranslateNotice', array( 'api_key' => 'api_key' )); with an api key from the local instance of LibreTranslate. From here, there's a new empty space between the "delete" and "reply" buttons of every notice; it seems that icons are missing. Anyway, clicking in the empty space just returns # added at the end of the address, so the page scrolls up. Nice, but not the intended result :) Using Firefox network inspector, it shows 404 errors when loading /plugins/TranslateNotice/css/gs-translate.css and /plugins/TranslateNotice/js/gs-translate.js. Both files exist, and they are in the correct location. Also, a new entry called "Translate notice" appears on settings, but it just shows what you can see in the attached image. Any hint about what should I change? Or is this just a crazy idea? (No feelings will be hurted if that's the case :)
senooken commented 1 year ago

@aab I think you need following 2 steps. I do not yet install LibreTranslate in local.

  1. Copy or link TranslateNotice -> public/plugins/TranslateNotice
mkdir -p public/plugins/TranslateNotice
cd public/plugins/TranslateNotice

for i in css doc fonts img js locale; do
  ln -fns ../../../local/plugins/TranslateNotice/$i $i
done
  1. Fix code in forms/translatenoticesettings.php.
@@ -56,7 +58,7 @@ class TranslateNoticeSettingsForm extends Form
             $languages,                 // content
             null,                       // instructions
             true,                       // 1st <option> is blank
-            $targetLanguage             // selected <option> on pageload
+            !!$targetLanguage             // selected <option> on pageload
         );
         $this->unli();    

And I think you had better use markdown code block (3 backticks).

@aab I think you need following 2 steps. I do not yet install LibreTranslate in local. 1. Copy or link TranslateNotice -> public/plugins/TranslateNotice ``` mkdir -p public/plugins/TranslateNotice cd public/plugins/TranslateNotice for i in css doc fonts img js locale; do ln -fns ../../../local/plugins/TranslateNotice/$i $i done ``` 2. Fix code in `forms/translatenoticesettings.php`. ``` @@ -56,7 +58,7 @@ class TranslateNoticeSettingsForm extends Form $languages, // content null, // instructions true, // 1st <option> is blank - $targetLanguage // selected <option> on pageload + !!$targetLanguage // selected <option> on pageload ); $this->unli(); ``` And I think you had better use markdown code block (3 backticks).
aab commented 1 year ago
Poster

Thanks, @senooken !

About your suggestions:

1) I've created the directory; the script you provide only makes valid links for "css" and "js" directories, as the rest don't exist.

2) Changed the code in forms/translatenoticesettings.php.

After that, the missing icon between "delete" and "reply" buttons appears on every notice, and the 404 errors mentioned early dissapear. Sadly, the result is this: 500 Internal Server Error whenever I click in the translate icon.

A hint about this from gs debug logs:

GET /main/translatenotice/translatenotice?text=%40aab] action.php - Server error '500' on 'translatenotice': Call to undefined method GNUsocial::getActivePlugins()

"Translate Notice" page in settings keeps behaving the same way, and the firefox inspector gives no clue about the error. But GS logs say:

GET /settings/translatenotice] action.php - Server error '500' on 'translatenoticesettings': [Translate_notice] DB_DataObject error []: MDB2 Error: no such table

(Thanks for the markdown hint, I tend to forget it).

Thanks, @senooken ! About your suggestions: 1) I've created the directory; the script you provide only makes valid links for "css" and "js" directories, as the rest don't exist. 2) Changed the code in ```forms/translatenoticesettings.php```. After that, the missing icon between "delete" and "reply" buttons appears on every notice, and the 404 errors mentioned early dissapear. Sadly, the result is this: ```500 Internal Server Error``` whenever I click in the translate icon. A hint about this from gs debug logs: ```GET /main/translatenotice/translatenotice?text=%40aab] action.php - Server error '500' on 'translatenotice': Call to undefined method GNUsocial::getActivePlugins()``` "Translate Notice" page in settings keeps behaving the same way, and the firefox inspector gives no clue about the error. But GS logs say: ```GET /settings/translatenotice] action.php - Server error '500' on 'translatenoticesettings': [Translate_notice] DB_DataObject error []: MDB2 Error: no such table``` (Thanks for the markdown hint, I tend to forget it).
senooken commented 1 year ago

Sorry for late. I missed your modified comment after 2 (modifying is not notified watched user).

2) You need rename getActivePlugins -> getActiveModules.

diff --git a/actions/translatenotice.php b/actions/translatenotice.php
index 2530e35..bd63c60 100644
--- a/actions/translatenotice.php
+++ b/actions/translatenotice.php
@@ -27,7 +27,7 @@ class TranslatenoticeAction extends Action
         }

         // Get plugin settings
-        $plugins = GNUsocial::getActivePlugins();
+        $plugins = GNUsocial::getActiveModules();
         $transl_attrs = $plugins['TranslateNotice'];

I got another error on https://localhost/settings/translatenotice.

<b>Fatal error</b>
:  Cannot declare class Jefs42\LibreTranslate, because the name is already in use in 
<b>/var/www/local/plugins/TranslateNotice/lib/libretranslate.php</b>
 on line 
<b>11</b>

So I could not check MDB2 Error.

I think you should better to submit result of git diff from original because other people cannot understand your fix.

Sorry for late. I missed your modified comment after 2 (modifying is not notified watched user). 2) You need rename `getActivePlugins` -> `getActiveModules`. ``` diff --git a/actions/translatenotice.php b/actions/translatenotice.php index 2530e35..bd63c60 100644 --- a/actions/translatenotice.php +++ b/actions/translatenotice.php @@ -27,7 +27,7 @@ class TranslatenoticeAction extends Action } // Get plugin settings - $plugins = GNUsocial::getActivePlugins(); + $plugins = GNUsocial::getActiveModules(); $transl_attrs = $plugins['TranslateNotice']; ``` I got another error on `https://localhost/settings/translatenotice`. ``` <b>Fatal error</b> : Cannot declare class Jefs42\LibreTranslate, because the name is already in use in <b>/var/www/local/plugins/TranslateNotice/lib/libretranslate.php</b> on line <b>11</b> ``` So I could not check MDB2 Error. I think you should better to submit result of `git diff` from original because other people cannot understand your fix.
Sign in to join this conversation.
No Milestone
No assignee
2 Participants
Loading...
Cancel
Save
There is no content yet.