First commit

master
Chimo 9 years ago
commit 387b858779
  1. 110
      TranslateNoticePlugin.php
  2. 3
      css/gs-translate.css
  3. 40
      js/gs-translate.js

@ -0,0 +1,110 @@
<?php
if (!defined('STATUSNET')) {
exit(1);
}
class TranslateNoticePlugin extends Plugin
{
const VERSION = '0.1';
public $id;
public $secret;
public $scope;
public $grantType;
function initialize() {
$this->scope = 'http://api.microsofttranslator.com/';
$this->grantType = 'client_credentials';
$this->authUrl = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13';
}
function getAccessToken() {
$params = http_build_query(
array(
'client_id' => $this->id,
'client_secret' => $this->secret,
'scope' => $this->scope,
'grant_type' => $this->grantType
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->authUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
$err = curl_error($ch);
throw new Exception($err);
}
curl_close($ch);
$json = json_decode($response);
if ($json->error) {
throw new Exception($json->error_description);
}
return $json->access_token;
}
function onEndShowNoticeOptionItems($item) {
if (!common_logged_in()) {
return;
}
// TODO: server-side fallback
$item->out->element('a', array('href' => '#', 'class' => 'gs-translate'), 'Translate this notice');
}
function onEndShowScripts($action) {
// No free-loaders...
if (!common_logged_in()) {
return;
}
try {
// TODO: Set this as cookie?
$accessToken = $this->getAccessToken();
$action->inlineScript('var gsTranslate = {}; gsTranslate.accessToken = "' . $accessToken . '"');
$action->script($this->path('js/gs-translate.js'));
} catch (Exception $e) {
$action->inlineScript('// ' . $e->getMessage());
}
return true;
}
function onEndShowStyles($action) {
$action->cssLink($this->path('css/gs-translate.css'));
}
/**
* Plugin version data
*
* @param array &$versions array of version data
*
* @return value
*/
function onPluginVersion(&$versions)
{
$versions[] = array('name' => 'TranslateNotice',
'version' => self::VERSION,
'author' => 'Stephane Berube',
'homepage' => 'https://github.com/chimo/gs-translate',
'description' =>
// TRANS: Plugin description.
_m('Translate notices.'));
return true;
}
}

@ -0,0 +1,3 @@
.gs-translate {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABGElEQVR4nK3RsSuFYRgF8N8l2SySu2Bi8g+oy0AWme5gsNssMopFQhJ/AIuRMjBYDHexmNhsMhDqSinuJT7Dfbvevr4vKafe5X2eczrnPPwzOrGPOj5wiyS8ZyygJY/chxucYBQjKOEsEklwjNY0uQ3n2EMh+t/GJ8pYQy2IrKYFpsOgnOFsGF0oYins1dEdL51G9vKwkYoyHw/vwueF7JKWU+QEu/HCazTY0eikoFHkOF4yBA5jgatoUMM6BvCFxwxygi2R3Uok1o45TGAG1ZxOKvycbBCXGfnfcBQiVjGFXlyjX+PETSzmWE1CHJjFO4ayLBWwEnLH5HuM4QBPmMyJ1ERJo+H4Mg/YRM9v5LSjIjr+QvozvgFEh1zD03dvBAAAAABJRU5ErkJggg==) no-repeat;
}

@ -0,0 +1,40 @@
(function () {
'use strict';
var token = window.gsTranslate.accessToken,
translate;
translate = function (text) {
var appId = 'Bearer ', // FIXME legacy: http://msdn.microsoft.com/en-us/library/hh454950.aspx
to = 'en',
callbackName = 'gsTranslate.callback',
encode = encodeURIComponent,
script;
script = document.createElement('script');
script.src = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate' +
'?appId=' + encode(appId) + encode(token) +
'&to=' + encode(to) +
'&text=' + encode(text) +
'&oncomplete=' + encode(callbackName);
document.body.appendChild(script);
};
$(document).on('click', '.gs-translate', function (e) {
e.preventDefault();
var $this = $(this),
text = $this.closest('.notice').find('.e-content').text();
translate(text);
});
window.gsTranslate.callback = function (response) {
// console.log(response);
alert(response);
};
}());