Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change api from yandex to libretranslate #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DictionaryService.vala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class DictionaryService : AsyncTaskExecuter {
try {
var word = Soup.URI.encode(_word, null);
var request = @"https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=$(API_KEY)&lang=$(_sourceLang)-$(_destLang)&text=$(word)&ui=en";
var root = WebJsonClient.Get(request);
var root = WebJsonClient.Get(request);

if (root != null) {
var defs = root.get_array_member("def");
Expand Down
27 changes: 16 additions & 11 deletions src/GlobalSettings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,32 @@ public class GlobalSettings : Object {
private void initLanguages() {
_langs = new Gee.ArrayList<LangInfo>();
addLang("en", _("English"));
addLang("ru", _("Russian"));
addLang("uk", _("Ukrainian"));
addLang("de", _("German"));
addLang("pl", _("Polish"));
addLang("fr", _("French"));
addLang("ar", _("Arabic"));
addLang("zh", _("Chinese"));
addLang("fr", _("French"));
addLang("de", _("German"));
addLang("hi", _("Hindi"));
addLang("ga", _("Irish"));
addLang("it", _("Italian"));
addLang("ja", _("Japanese"));
addLang("ko", _("Korean"));
addLang("pt", _("Portuguese"));
addLang("ru", _("Russian"));
addLang("es", _("Spanish"));
//Added from Yandex Arabic Hindi, Irish
//Langages lost from Yandex :
/*addLang("uk", _("Ukrainian"));
addLang("pl", _("Polish"));
addLang("nl", _("Dutch"));
addLang("it", _("Italian"));
addLang("la", _("Latin"));
addLang("el", _("Greek"));
addLang("fi", _("Finnish"));
addLang("sv", _("Swedish"));
addLang("tr", _("Turkish"));
addLang("zh", _("Chinese"));
addLang("ko", _("Korean"));
addLang("ja", _("Japanese"));
addLang("pt", _("Portuguese"));
addLang("cs", _("Czech"));
addLang("et", _("Estonian"));
addLang("sr", _("Serbian"));
addLang("sk", _("Slovak"));
addLang("sk", _("Slovak")); */

_langs.sort ((e1, e2) => {
if (e1.name > e2.name) return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Main : Object {
var destLang = global.LoadDestLang();

if (source != null) sourceLang = source;
if (dest != null) destLang = dest;
if ((dest != null) && (dest !=source)) destLang = dest;

global.setSourceStartLang(sourceLang);
global.setDestStartLang(destLang);
Expand Down
18 changes: 10 additions & 8 deletions src/TranslatorService.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Translation service that use Yandex translate
public class TranslateService : AsyncTaskExecuter {
private const string API_KEY = "trnsl.1.1.20150427T160217Z.69da78079263823e.0f0caa9724cee126c18028e70f227b633a3fe58f";
private string[] _result;
private string _from;
private string _to;
Expand All @@ -17,18 +16,21 @@ public class TranslateService : AsyncTaskExecuter {
/// Task main working method
public override void OnExecute() throws TranslatorError {
var ntext = Soup.URI.encode(_text, null);
var request = @"https://translate.yandex.net/api/v1.5/tr.json/translate?key=$(API_KEY)&lang=$(_from)-$(_to)&text=$(ntext)";
var root = WebJsonClient.Get(request);
//var request = @"https://translate.yandex.net/api/v1.5/tr.json/translate?key=$(API_KEY)&lang=$(_from)-$(_to)&text=$(ntext)";
var request= @"https://libretranslate.com/translate?q="+ntext+"&source="+_from+"&target="+_to;

var root = WebJsonClient.Get(request);
var data = new Gee.ArrayList<string>();

if (root != null) {
var sentences = root.get_array_member("text");
var sentences = root.get_string_member("translatedText");

if (sentences != null) {
foreach (var s in sentences.get_elements()) {
var el = s.get_string();
data.add(el);
}
//foreach (var s in sentences.get_elements()) {
// var el = s.get_string();
// data.add(el);
data.add (sentences.to_string());
//}
}
_result = data.to_array();
}
Expand Down
13 changes: 7 additions & 6 deletions src/TranslatorWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Main translator window
public class TranslateWindow : Gtk.ApplicationWindow {
/// Timeout before translate in milliseconds
const int TIMEOUT_BEFOR_TRANSLATE = 500;
const int TIMEOUT_BEFOR_TRANSLATE = 1000;

/// Service for translating
private TranslateService _translateService;
Expand Down Expand Up @@ -82,7 +82,9 @@ public class TranslateWindow : Gtk.ApplicationWindow {
/// Is translate in progress
private bool _isTranslating = false;

//public Gtk.CssProvider style_provider;
private void styleWindow() {

var style = @"
GtkTextView {
background-color: RGBA(255,0,0,0);
Expand All @@ -102,7 +104,7 @@ public class TranslateWindow : Gtk.ApplicationWindow {
background-color: @base_color;
}
";
Granite.Widgets.Utils.set_theming_for_screen (this.get_screen (), style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
Granite.Widgets.Utils.set_theming_for_screen (this.get_screen (), style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}

// Create language combos
Expand Down Expand Up @@ -417,8 +419,8 @@ public class TranslateWindow : Gtk.ApplicationWindow {

leftLangCombo.setLanguages (langs);
leftLangCombo.setActive (global.getSourceStartLang());
leftLangCombo.changed.connect(onLeftComboChange);

leftLangCombo.clicked.connect(onLeftComboChange);
leftLang = getLeftLang();
rightLang = getRightLang();
}
Expand Down Expand Up @@ -469,7 +471,6 @@ public class TranslateWindow : Gtk.ApplicationWindow {
private void onLangChange(bool isRight) {
var leftLa = getLeftLang();
var rightLa = getRightLang();

var needUpdate = true;

if (leftLa == rightLa) {
Expand All @@ -485,7 +486,7 @@ public class TranslateWindow : Gtk.ApplicationWindow {
topText.buffer.text = bottomText.buffer.text;
}

if (rightLa == rightLang)
if ((rightLa == rightLang) && (leftLa == leftLang))
needUpdate = false;

if (needUpdate) {
Expand Down
24 changes: 14 additions & 10 deletions src/WebJsonClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
public class WebJsonClient : GLib.Object {
/// No connection to server code
private const int NO_CONNECTION = 2;

private const int STATUS_OK = 200;

/// Json parser
private static Json.Parser _parser;
private static Json.Parser parser {
Expand All @@ -19,19 +20,22 @@ public class WebJsonClient : GLib.Object {
session.proxy_uri = GlobalSettings.getProxyUri();

var url = new Soup.URI(request);
var message = new Soup.Message.from_uri ("GET", url);
var message = new Soup.Message.from_uri ("POST", url);

var status = session.send_message(message);
if (status == NO_CONNECTION) {
throw new TranslatorError.NoConnection(_("No connection to server"));
}
try {
var mess = (string)message.response_body.data;
parser.load_from_data (mess);
}
catch (GLib.Error error) {
warning ("%s", error.message);
}
return parser.get_root ().get_object ();

if (status == STATUS_OK) {
try {
var mess = (string)message.response_body.data;
parser.load_from_data (mess);
}
catch (GLib.Error error) {
warning ("%s", error.message);
}
return parser.get_root ().get_object ();
} else { throw new TranslatorError.NoConnection("status "+status.to_string()); }
}
}
2 changes: 1 addition & 1 deletion src/widgets/PopoverCombo.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class PopoverCombo : Gtk.ToggleButton {
toggled.connect (() => {
if (active) {
popover.show_all ();
setActive (activeLang.info.id);
setActive (activeLang.info.id);
} else {
popover.hide ();
}
Expand Down