Skip to content

Commit

Permalink
Add option to call automatically DeepL #49
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGrosjean committed Sep 22, 2022
1 parent deba71d commit 88972c6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 18 deletions.
3 changes: 3 additions & 0 deletions AppInfo.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
AppVersion=3.4
- Add option and button to call free DeepL API (#47)

AppVersion=3.3
- Fix crash when translating CK3 with more than 2 languages (#45)

Expand Down
5 changes: 5 additions & 0 deletions src/config/ConfigStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ConfigStorage {
private static final String wsDirectoryAttribute = "directory";
private static final String wsSourceLanguageAttribute = "source";
private static final String wsDestinationLanguageAttribute = "destination";
private static final String wsAutomaticDeepLAttribute = "automaticDeepL";
private static final String wsAutomaticGoogleAttribute = "automaticGoogle";
private static final String wsAcceptAllCopies = "acceptAllCopies";

Expand Down Expand Up @@ -59,12 +60,14 @@ public ConfigStorage(String configurationFile) {
boolean modified = false;
while (it.hasNext()) {
Element wsElem = it.next();
String automaticDeepLTranslate = wsElem.getAttributeValue(wsAutomaticDeepLAttribute);
String automaticGoogleTranslate = wsElem.getAttributeValue(wsAutomaticGoogleAttribute);
String acceptAllCopies = wsElem.getAttributeValue(wsAcceptAllCopies);
WorkingSession ws = new WorkingSession(wsElem.getAttributeValue(wsNameAttribute),
wsElem.getAttributeValue(wsDirectoryAttribute),
WorkingSession.getLanguage(wsElem.getAttributeValue(wsSourceLanguageAttribute)),
WorkingSession.getLanguage(wsElem.getAttributeValue(wsDestinationLanguageAttribute)),
(automaticDeepLTranslate != null) ? Boolean.valueOf(automaticDeepLTranslate) : false,
(automaticGoogleTranslate != null) ? Boolean.valueOf(automaticGoogleTranslate) : false,
(acceptAllCopies != null) ? Boolean.valueOf(acceptAllCopies) : false);
if (!ws.getErrorMessage().equals("")) {
Expand Down Expand Up @@ -104,6 +107,8 @@ private Element workingSessionToElement(WorkingSession ws) {
workingSessionElem.setAttribute(new Attribute(wsSourceLanguageAttribute, ws.getSourceLanguage().getCode()));
workingSessionElem
.setAttribute(new Attribute(wsDestinationLanguageAttribute, ws.getDestinationLanguage().getCode()));
workingSessionElem
.setAttribute(new Attribute(wsAutomaticDeepLAttribute, Boolean.toString(ws.isAutomaticDeepLCall())));
workingSessionElem
.setAttribute(new Attribute(wsAutomaticGoogleAttribute, Boolean.toString(ws.isAutomaticGoogleCall())));
workingSessionElem
Expand Down
20 changes: 17 additions & 3 deletions src/config/WorkingSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class WorkingSession {
*/
private boolean automaticGoogleCall;

/**
* Call automatically DeepL translate when the destination text is empty
*/
private boolean automaticDeepLCall;

/**
* Do not consider copy text as a missing translation
*/
Expand All @@ -58,7 +63,7 @@ public class WorkingSession {
* @param destinationLanguage
*/
public WorkingSession(String name, String directory, Language sourceLanguage,
Language destinationLanguage, boolean automaticGoogleCall,
Language destinationLanguage, boolean automaticDeepLCall, boolean automaticGoogleCall,
boolean acceptAllCopies) {
if (!isAvailableLanguagesInitialized()) {
throw new IllegalArgumentException("The list of available languages was not initialized!");
Expand All @@ -83,15 +88,16 @@ public WorkingSession(String name, String directory, Language sourceLanguage,
RuntimeException e) {
errorMessage.append(e.getMessage() + System.lineSeparator());
}
setAutomaticDeepLCall(automaticDeepLCall);
setAutomaticGoogleCall(automaticGoogleCall);
setAcceptAllCopies(acceptAllCopies);

this.errorMessage = errorMessage.toString();
}

public WorkingSession(String name, String directory, Language sourceLanguage,
boolean automaticGoogleCall, boolean acceptAllCopies) {
this(name, directory, sourceLanguage, new Language(), automaticGoogleCall, acceptAllCopies);
boolean automaticDeepLCall, boolean automaticGoogleCall, boolean acceptAllCopies) {
this(name, directory, sourceLanguage, new Language(), automaticDeepLCall, automaticGoogleCall, acceptAllCopies);
}

public String getName() {
Expand Down Expand Up @@ -141,6 +147,14 @@ public void setDestinationLanguage(Language destinationLanguage) {
}
}

public boolean isAutomaticDeepLCall() {
return automaticDeepLCall;
}

public void setAutomaticDeepLCall(boolean automaticDeepLCall) {
this.automaticDeepLCall = automaticDeepLCall;
}

public boolean isAutomaticGoogleCall() {
return automaticGoogleCall;
}
Expand Down
9 changes: 6 additions & 3 deletions src/gui/DetailsButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class DetailsButton extends DefaultCellEditor {

public DetailsButton(JCheckBox checkBox, Language sourceLanguage,
boolean hasDestinationLanguage, Language destinationLanguage,
boolean automaticGoogleCall, Window window) {
boolean automaticDeepLCall, boolean automaticGoogleCall, Window window) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
bListener = new ButtonListener(sourceLanguage, hasDestinationLanguage,
destinationLanguage, automaticGoogleCall, window);
destinationLanguage, automaticDeepLCall, automaticGoogleCall, window);
button.addActionListener(bListener);
}

Expand Down Expand Up @@ -53,16 +53,19 @@ class ButtonListener implements ActionListener {
private Language sourceLanguage;
private boolean hasDestinationLanguage;
private Language destinationLanguage;
private boolean automaticDeepLCall;
private boolean automaticGoogleCall;
private Window window;

ButtonListener(Language sourceLanguage, boolean hasDestinationLanguage,
Language destinationLanguage,
boolean automaticDeepLCall,
boolean automaticGoogleCall,
Window window) {
this.sourceLanguage = sourceLanguage;
this.hasDestinationLanguage = hasDestinationLanguage;
this.destinationLanguage = destinationLanguage;
this.automaticDeepLCall = automaticDeepLCall;
this.automaticGoogleCall = automaticGoogleCall;
this.window = window;
}
Expand All @@ -77,7 +80,7 @@ public void actionPerformed(ActionEvent event) {
TranslatorParsedFile f = (TranslatorParsedFile)table.getValueAt(row, column-3);
new DetailsDialog(null, f.getName(), true, f, sourceLanguage,
hasDestinationLanguage, destinationLanguage,
automaticGoogleCall, window);
automaticDeepLCall, automaticGoogleCall, window);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/gui/DetailsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public DetailsDialog(JFrame parent, String title, boolean modal,
Language sourceLanguage,
boolean hasDestinationLanguage,
Language destinationLanguage,
boolean automaticDeepLCall,
boolean automaticGoogleCall,
Window window) {
// Create the JDialog
Expand Down Expand Up @@ -53,7 +54,7 @@ public DetailsDialog(JFrame parent, String title, boolean modal,
if (hasDestinationLanguage) {
JButton translateBtn = new JButton("Translate...");
translateBtn.addActionListener(new TranslateListener(title, file, sourceLanguage,
destinationLanguage, automaticGoogleCall, window));
destinationLanguage, automaticDeepLCall, automaticGoogleCall, window));
getContentPane().add(translateBtn, BorderLayout.SOUTH);
}
setVisible(true);
Expand All @@ -76,19 +77,22 @@ class TranslateListener implements ActionListener {
private ITranslator file;
private Language sourceLanguage;
private Language destinationLanguage;
private boolean automaticDeepLCall;
private boolean automaticGoogleCall;
private Window window;


public TranslateListener(String fileName, ITranslator file,
Language sourceLanguage,
Language destinationLanguage,
boolean automaticDeepLCall,
boolean automaticGoogleCall,
Window window) {
this.fileName = fileName;
this.file = file;
this.sourceLanguage = sourceLanguage;
this.destinationLanguage = destinationLanguage;
this.automaticDeepLCall = automaticDeepLCall;
this.automaticGoogleCall = automaticGoogleCall;
this.window = window;
}
Expand All @@ -100,7 +104,7 @@ public void actionPerformed(ActionEvent event) {
// Open a new one
new TranslatorDialog(null, fileName, true, file,
sourceLanguage, destinationLanguage,
automaticGoogleCall);
automaticDeepLCall, automaticGoogleCall);

// Refresh working session
window.refreshWorkingSession();
Expand Down
10 changes: 7 additions & 3 deletions src/gui/TranslatorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class TranslatorDialog extends JDialog {
private GoogleTranslate google;
private DeeplTranslate deepL;
private String destinationLanguageCode;
private boolean automaticDeepLCall;
private boolean automaticGoogleCall;
private String oldSourceText;
private String oldDestinationText;
Expand All @@ -74,13 +75,15 @@ public class TranslatorDialog extends JDialog {

public TranslatorDialog(JFrame parent, String fileName, boolean modal,
ITranslator file, Language sourceLanguage,
Language destinationLanguage, boolean automaticGoogleCall) {
Language destinationLanguage,
boolean automaticDeepLCall, boolean automaticGoogleCall) {
// Create the JDialog
super(parent, "", modal);
setSize(1150, 620);
setLocationRelativeTo(null);

this.fileName = fileName;
this.automaticDeepLCall = automaticDeepLCall;
this.automaticGoogleCall = automaticGoogleCall;
google = new GoogleTranslate(sourceLanguage.getLocale().toString(),
destinationLanguage.getLocale().toString());
Expand Down Expand Up @@ -358,8 +361,9 @@ private void updateTextAreaAndTitle() {
setTitle(fileName + " - " + entry.getId() + " (line " + entry.getDestLineNumber() + ")");
oldSourceText = entry.getSource();
oldDestinationText = entry.getDestination();
// TODO Call DeepL before
if (automaticGoogleCall && destTextPane.getText().equals("")) {
if (automaticDeepLCall && destTextPane.getText().equals("")) {
callTranslateAPI(deepL);
} else if (automaticGoogleCall && destTextPane.getText().equals("")) {
callTranslateAPI(google);
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/gui/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public String getToolTipText(MouseEvent e) {
table.getColumn(columnTitles[4]).setCellEditor(new DetailsButton(new JCheckBox(), ws.getSourceLanguage(),
!ws.getDestinationLanguage().isNone(),
ws.getDestinationLanguage(),
ws.isAutomaticDeepLCall(),
ws.isAutomaticGoogleCall(),
this));
table.getColumn(columnTitles[4]).setPreferredWidth(50);
Expand Down Expand Up @@ -455,6 +456,7 @@ public void actionPerformed(ActionEvent e) {
ws.isAcceptAllCopies());
new TranslatorDialog(null, f.getName(), true, file,
ws.getSourceLanguage(), ws.getDestinationLanguage(),
ws.isAutomaticDeepLCall(),
ws.isAutomaticGoogleCall());

// Refresh working session
Expand Down
22 changes: 16 additions & 6 deletions src/gui/WorkingSessionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class WorkingSessionDialog extends JDialog {
private JTextField localisationDirectoryTF;
private JComboBox<Language> sourceLanguageSourceComboBox;
private JComboBox<Language> destinationLanguageComboBox;
private JCheckBox automaticDeepLCall;
private JCheckBox automaticGoogleCall;
private JCheckBox acceptAllCopiesCB;
private JPanel container;
Expand All @@ -59,10 +60,11 @@ public class WorkingSessionDialog extends JDialog {
*/
public WorkingSessionDialog(JFrame parent, String title, boolean modal, String name,
String directory, Language sourceLanguage, Language destinationLanguage,
boolean automaticGoogleTranslate, boolean acceptAllCopies) {
boolean automaticDeepLTranslate, boolean automaticGoogleTranslate,
boolean acceptAllCopies) {
// Create the JDialog
super(parent, title, modal);
setSize(500, 450);
setSize(500, 520);
setLocationRelativeTo(null);
setResizable(false);

Expand All @@ -86,6 +88,9 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n
destinationLanguageComboBox = new JComboBox<Language>(avalaibleDestinationLanguages);
destinationLanguageComboBox.setSelectedItem(destinationLanguage);

automaticDeepLCall = new JCheckBox("Call automatically DeepL when destination text is empty");
automaticDeepLCall.setSelected(automaticDeepLTranslate);

automaticGoogleCall = new JCheckBox("Call automatically Google Translate when destination text is empty");
automaticGoogleCall.setSelected(automaticGoogleTranslate);

Expand Down Expand Up @@ -113,6 +118,9 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n
languagePanel.add(destinationLanguageLabel);
languagePanel.add(destinationLanguageComboBox);
languagePanel.setBorder(BorderFactory.createTitledBorder("Translation"));
JPanel deepLPanel = new JPanel();
deepLPanel.setBorder(BorderFactory.createTitledBorder("DeepL"));
deepLPanel.add(automaticDeepLCall);
JPanel googlePanel = new JPanel();
googlePanel.setBorder(BorderFactory.createTitledBorder("Google translate"));
googlePanel.add(automaticGoogleCall);
Expand All @@ -125,10 +133,11 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n

// Add to the container
container = new JPanel();
container.setLayout(new GridLayout(6, 1, 5, 5));
container.setLayout(new GridLayout(7, 1, 5, 5));
container.add(wsNamePanel);
container.add(localisationDirPanel);
container.add(languagePanel);
container.add(deepLPanel);
container.add(googlePanel);
container.add(copyPanel);
container.add(validCancelPanel);
Expand All @@ -143,14 +152,15 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n
}

public WorkingSessionDialog(JFrame parent, String title, boolean modal) {
this(parent, title, modal, "", "", new Language(), new Language(), false, false);
this(parent, title, modal, "", "", new Language(), new Language(), false, false, false);
}

public WorkingSessionDialog(JFrame parent, String title, boolean modal,
WorkingSession ws) {
this(parent, title, modal, ws.getName(), ws.getDirectory(),
ws.getSourceLanguage(), ws.getDestinationLanguage(),
ws.isAutomaticGoogleCall(), ws.isAcceptAllCopies());
ws.isAutomaticDeepLCall(), ws.isAutomaticGoogleCall(),
ws.isAcceptAllCopies());
}

public WorkingSession getWorkingSession() {
Expand All @@ -165,7 +175,7 @@ public WorkingSession getWorkingSession() {
return new WorkingSession(wsName.getText(), localisationDirectoryTF.getText(),
sourceLanguageSourceComboBox.getItemAt(sourceLanguageSourceComboBox.getSelectedIndex()),
destinationLanguageComboBox.getItemAt(destinationLanguageComboBox.getSelectedIndex()),
automaticGoogleCall.isSelected(), acceptAllCopiesCB.isSelected());
automaticDeepLCall.isSelected(), automaticGoogleCall.isSelected(), acceptAllCopiesCB.isSelected());
}

class FileExplorer implements ActionListener {
Expand Down
2 changes: 1 addition & 1 deletion src/versionning/OnlineVersionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class OnlineVersionChecker {
private static String URL_APP_INFO_TXT = "https://raw.githubusercontent.com/NicolasGrosjean/Translate_helper/master/AppInfo.txt";
private static String VERSION = "3.3";
private static String VERSION = "3.4";

private int lastestOnlineMajorVersionNumber;
private int lastestOnlineMinorVersionNumber;
Expand Down

0 comments on commit 88972c6

Please sign in to comment.