The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../../Utility/Helpers/jucer_TranslationHelpers.h"
  21. //==============================================================================
  22. class TranslationToolComponent : public Component,
  23. public Button::Listener
  24. {
  25. public:
  26. TranslationToolComponent()
  27. : editorOriginal (documentOriginal, nullptr),
  28. editorPre (documentPre, nullptr),
  29. editorPost (documentPost, nullptr),
  30. editorResult (documentResult, nullptr)
  31. {
  32. instructionsLabel.setText (
  33. "This utility converts translation files to/from a format that can be passed to automatic translation tools."
  34. "\n\n"
  35. "First, choose whether to scan the current project for all TRANS() macros, or "
  36. "pick an existing translation file to load:", dontSendNotification);
  37. addAndMakeVisible (instructionsLabel);
  38. label1.setText ("..then copy-and-paste this annotated text into Google Translate or some other translator:", dontSendNotification);
  39. addAndMakeVisible (label1);
  40. label2.setText ("...then, take the translated result and paste it into the box below:", dontSendNotification);
  41. addAndMakeVisible (label2);
  42. label3.setText ("Finally, click the 'Generate' button, and a translation file will be created below. "
  43. "Remember to update its language code at the top!", dontSendNotification);
  44. addAndMakeVisible (label3);
  45. label4.setText ("If you load an existing file the already translated strings will be removed. Ensure this box is empty to create a fresh translation", dontSendNotification);
  46. addAndMakeVisible (label4);
  47. addAndMakeVisible (editorOriginal);
  48. addAndMakeVisible (editorPre);
  49. addAndMakeVisible (editorPost);
  50. addAndMakeVisible (editorResult);
  51. generateButton.setButtonText (TRANS("Generate"));
  52. addAndMakeVisible (generateButton);
  53. scanProjectButton.setButtonText ("Scan Project for TRANS macros");
  54. addAndMakeVisible (scanProjectButton);
  55. scanFolderButton.setButtonText ("Scan Folder for TRANS macros");
  56. addAndMakeVisible (scanFolderButton);
  57. loadTranslationButton.setButtonText ("Load existing translation File...");
  58. addAndMakeVisible (loadTranslationButton);
  59. generateButton.addListener (this);
  60. scanProjectButton.addListener (this);
  61. scanFolderButton.addListener (this);
  62. loadTranslationButton.addListener (this);
  63. }
  64. void paint (Graphics& g) override
  65. {
  66. g.fillAll (findColour (backgroundColourId));
  67. }
  68. void resized() override
  69. {
  70. const int m = 6;
  71. const int textH = 44;
  72. const int extraH = (7 * textH);
  73. const int editorH = (getHeight() - extraH) / 4;
  74. const int numButtons = 3;
  75. Rectangle<int> r (getLocalBounds().withTrimmedBottom (m));
  76. const int buttonWidth = r.getWidth() / numButtons;
  77. instructionsLabel.setBounds (r.removeFromTop (textH * 2).reduced (m));
  78. r.removeFromTop (m);
  79. Rectangle<int> r2 (r.removeFromTop (textH - (2 * m)));
  80. scanProjectButton .setBounds (r2.removeFromLeft (buttonWidth).reduced (m, 0));
  81. scanFolderButton .setBounds (r2.removeFromLeft (buttonWidth).reduced (m, 0));
  82. loadTranslationButton.setBounds (r2.reduced (m, 0));
  83. label1 .setBounds (r.removeFromTop (textH) .reduced (m));
  84. editorPre.setBounds (r.removeFromTop (editorH).reduced (m, 0));
  85. label2 .setBounds (r.removeFromTop (textH) .reduced (m));
  86. editorPost.setBounds (r.removeFromTop (editorH).reduced (m, 0));
  87. r2 = r.removeFromTop (textH);
  88. generateButton.setBounds (r2.removeFromRight (152).reduced (m));
  89. label3 .setBounds (r2.reduced (m));
  90. editorResult .setBounds (r.removeFromTop (editorH).reduced (m, 0));
  91. label4 .setBounds (r.removeFromTop (textH).reduced (m));
  92. editorOriginal.setBounds (r.reduced (m, 0));
  93. }
  94. private:
  95. CodeDocument documentOriginal, documentPre, documentPost, documentResult;
  96. CodeEditorComponent editorOriginal, editorPre, editorPost, editorResult;
  97. Label label1, label2, label3, label4;
  98. TextButton generateButton;
  99. Label instructionsLabel;
  100. TextButton scanProjectButton;
  101. TextButton scanFolderButton;
  102. TextButton loadTranslationButton;
  103. void buttonClicked (Button* b) override
  104. {
  105. if (b == &generateButton) generate();
  106. else if (b == &scanProjectButton) scanProject();
  107. else if (b == &scanFolderButton) scanFolder();
  108. else if (b == &loadTranslationButton) loadFile();
  109. else
  110. jassertfalse;
  111. }
  112. void generate()
  113. {
  114. StringArray preStrings (TranslationHelpers::breakApart (documentPre.getAllContent()));
  115. StringArray postStrings (TranslationHelpers::breakApart (documentPost.getAllContent()));
  116. if (postStrings.size() != preStrings.size())
  117. {
  118. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  119. TRANS("Error"),
  120. TRANS("The pre- and post-translation text doesn't match!\n\n"
  121. "Perhaps it got mangled by the translator?"));
  122. return;
  123. }
  124. const LocalisedStrings originalTranslation (documentOriginal.getAllContent(), false);
  125. documentResult.replaceAllContent (TranslationHelpers::createFinishedTranslationFile (preStrings, postStrings, originalTranslation));
  126. }
  127. void scanProject()
  128. {
  129. if (Project* project = ProjucerApplication::getApp().mainWindowList.getFrontmostProject())
  130. setPreTranslationText (TranslationHelpers::getPreTranslationText (*project));
  131. else
  132. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon, "Translation Tool",
  133. "This will only work when you have a project open!");
  134. }
  135. void scanFolder()
  136. {
  137. FileChooser fc ("Choose the root folder to search for the TRANS macros",
  138. File(), "*");
  139. if (fc.browseForDirectory())
  140. {
  141. StringArray strings;
  142. TranslationHelpers::scanFolderForTranslations (strings, fc.getResult());
  143. setPreTranslationText (TranslationHelpers::mungeStrings(strings));
  144. }
  145. }
  146. void loadFile()
  147. {
  148. FileChooser fc ("Choose a translation file to load",
  149. File(), "*");
  150. if (fc.browseForFileToOpen())
  151. {
  152. const LocalisedStrings loadedStrings (fc.getResult(), false);
  153. documentOriginal.replaceAllContent (fc.getResult().loadFileAsString().trim());
  154. setPreTranslationText (TranslationHelpers::getPreTranslationText (loadedStrings));
  155. }
  156. }
  157. void setPreTranslationText (const String& text)
  158. {
  159. documentPre.replaceAllContent (text);
  160. editorPre.grabKeyboardFocus();
  161. editorPre.selectAll();
  162. }
  163. };