2 * This file is part of Gnome Subtitles.
3 * Copyright (C) 2007-2008 Pedro Castro
5 * Gnome Subtitles is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Gnome Subtitles is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 using GnomeSubtitles
.Ui
.Edit
;
24 using SubLib
.Exceptions
;
26 using GnomeSubtitles
.Dialog
;
27 using GnomeSubtitles
.Dialog
.Unmanaged
;
30 namespace GnomeSubtitles
.Core
.Command
{
32 public class TranslatorCommand
: FixedSingleSelectionCommand
{
33 private string translatedText
; // translated string
34 private string formerText
; // former text replaced by translation
35 private bool leftToRight
; // direction of translating
36 private static string description
= Catalog
.GetString("Translating text using Google Translate.");
38 public TranslatorCommand (bool leftToRight
) : base(description
, false, false) {
39 this.translatedText
= String
.Empty
;
40 this.formerText
= String
.Empty
;
41 this.leftToRight
= leftToRight
;
43 SetStopsGrouping(true);
48 public override bool CanGroupWith (Command command
) {
52 public override Command
MergeWith (Command command
) {
56 protected override bool ChangeValues () {
57 if (Base
.Ui
.View
.Selection
.Count
!= 1)
61 formerText
= Base
.Ui
.View
.Selection
.Subtitle
.Translation
.Get();
63 formerText
= Base
.Ui
.View
.Selection
.Subtitle
.Text
.Get();
67 if (!Base
.SpellLanguages
.HasActiveTextLanguage
)
69 Base
.Dialogs
.Get(typeof(SetTextLanguageDialog
)).Show();
70 if (!Base
.SpellLanguages
.HasActiveTextLanguage
)
73 if (!Base
.SpellLanguages
.HasActiveTranslationLanguage
)
75 Base
.Dialogs
.Get(typeof(SetTranslationLanguageDialog
)).Show();
76 if (!Base
.SpellLanguages
.HasActiveTranslationLanguage
)
82 translated
= Translator
.TranslateText(Base
.Ui
.View
.Selection
.Subtitle
.Text
.Get(), /*"en_US", "de_DE",*/ Base
.SpellLanguages
.ActiveTextLanguage
.ID
, Base
.SpellLanguages
.ActiveTranslationLanguage
.ID
, Translator
.TIMEOUT
);
84 translated
= Translator
.TranslateText(Base
.Ui
.View
.Selection
.Subtitle
.Translation
.Get(), Base
.SpellLanguages
.ActiveTextLanguage
.ID
, Base
.SpellLanguages
.ActiveTranslationLanguage
.ID
, Translator
.TIMEOUT
);
86 translatedText
= translated
;
88 Base
.Ui
.View
.Selection
.Subtitle
.Translation
.Set(translated
);
90 Base
.Ui
.View
.Selection
.Subtitle
.Text
.Set(translated
);
94 catch (TranslatorException e
)
96 Gtk
.MessageDialog md
= new Gtk
.MessageDialog(Base
.Ui
.Window
,
97 DialogFlags
.DestroyWithParent
,
99 ButtonsType
.Close
, e
.Message
);
107 public override void Undo () {
109 Base
.Ui
.View
.Selection
.Subtitle
.Translation
.Set(formerText
);
111 Base
.Ui
.View
.Selection
.Subtitle
.Text
.Set(formerText
);
116 public override void Redo () {
118 Base
.Ui
.View
.Selection
.Subtitle
.Translation
.Set(translatedText
);
120 Base
.Ui
.View
.Selection
.Subtitle
.Text
.Set(translatedText
);
125 /* Protected members */
127 protected override void PostProcess () {
128 Base
.Ui
.View
.Selection
.Reselect();
129 Base
.Ui
.View
.Refresh();
133 protected bool GetLeftToRight () {
137 protected string GetTranslatedText () {
138 return translatedText
;
141 protected string GetFormerText () {