YandexTranslatePlugin.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // This file is part of YandexTranslate plugin for GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * @package Plugin
  19. * @maintainer kim <kim@surtdelcercle.cat>
  20. */
  21. class YandexTranslatePlugin extends Plugin
  22. {
  23. const PLUGIN_VERSION = '1.0.1';
  24. public static function settings($setting)
  25. {
  26. // Yandex API key see https://passport.yandex.com/
  27. $settings['api_key'] = '';
  28. // Language to translate notices (by default uses user's language)
  29. $settings['lang'] = common_language();
  30. // config.php settings override the settings in this file
  31. $configsettings = common_config('site', 'yandex') ?: [];
  32. foreach ($configsettings as $configsetting => $value) {
  33. $settings[$configsetting] = $value;
  34. }
  35. if (isset($settings[$setting])) {
  36. return $settings[$setting];
  37. } else {
  38. return false;
  39. }
  40. }
  41. public function initialize()
  42. {
  43. // show YandexTranslate link in the admin panel on the future
  44. //common_config_append('admin', 'panels', 'yandexadmin');
  45. }
  46. public function onEndShowScripts(Action $action)
  47. {
  48. //$action->script($this->path('jquery.min.js'));
  49. $script = "
  50. $(document).ready(function() {
  51. var apikey = '".static::settings('api_key')."'; //your Yandex apikey
  52. var lang = '".static::settings('lang')."';
  53. $('.ytranslate').on('click', function(e) {
  54. e.preventDefault();
  55. var id = $(this).attr('data-id');
  56. var status = $('#notice-'+id+' > .e-content');
  57. var text = status.html();
  58. var url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key='+apikey+'&text='+encodeURIComponent(text)+'&lang='+lang+'&format=html&options=1';
  59. $.ajax({
  60. type : 'GET',
  61. dataType : 'jsonp',
  62. url : url,
  63. callback: 'translate',
  64. success: function(data){
  65. status.html(data.text[0]);
  66. }
  67. });
  68. });
  69. });";
  70. $action->inlineScript($script);
  71. //write on log for debug
  72. //common_log(LOG_INFO, 'Message');
  73. return true;
  74. }
  75. public function onEndShowNoticeItem($item)
  76. {
  77. $item->out->element('a', ['href' => '#', 'class' => 'ytranslate', 'data-id' => $item->notice->id, 'title' => 'Translate'], 'Translate');
  78. return true;
  79. }
  80. public function onPluginVersion(array &$versions)
  81. {
  82. $versions[] = array('name' => 'Yandex Translate',
  83. 'version' => self::PLUGIN_VERSION,
  84. 'author' => 'Kim',
  85. 'homepage' => 'https://social.surtdelcercle.cat',
  86. 'rawdescription' =>
  87. // TRANS: Plugin description.
  88. _m('A simple script to translate notices.'));
  89. return true;
  90. }
  91. }