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.

206 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCER_TRANSLATIONTOOLCOMPONENT_H_INCLUDED
  18. #define JUCER_TRANSLATIONTOOLCOMPONENT_H_INCLUDED
  19. #include "jucer_TranslationHelpers.h"
  20. //==============================================================================
  21. class TranslationToolComponent : public Component,
  22. public ButtonListener
  23. {
  24. public:
  25. TranslationToolComponent()
  26. : editorOriginal (documentOriginal, nullptr),
  27. editorPre (documentPre, nullptr),
  28. editorPost (documentPost, nullptr),
  29. editorResult (documentResult, nullptr)
  30. {
  31. setLookAndFeel (&lf);
  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. ProjucerLookAndFeel::fillWithBackgroundTexture (*this, g);
  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. juce::Label label1, label2, label3, label4;
  98. juce::TextButton generateButton;
  99. juce::Label instructionsLabel;
  100. juce::TextButton scanProjectButton;
  101. juce::TextButton scanFolderButton;
  102. juce::TextButton loadTranslationButton;
  103. ProjucerLookAndFeel lf;
  104. void buttonClicked (Button* b) override
  105. {
  106. if (b == &generateButton) generate();
  107. else if (b == &scanProjectButton) scanProject();
  108. else if (b == &scanFolderButton) scanFolder();
  109. else if (b == &loadTranslationButton) loadFile();
  110. else
  111. jassertfalse;
  112. }
  113. void generate()
  114. {
  115. StringArray preStrings (TranslationHelpers::breakApart (documentPre.getAllContent()));
  116. StringArray postStrings (TranslationHelpers::breakApart (documentPost.getAllContent()));
  117. if (postStrings.size() != preStrings.size())
  118. {
  119. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  120. TRANS("Error"),
  121. TRANS("The pre- and post-translation text doesn't match!\n\n"
  122. "Perhaps it got mangled by the translator?"));
  123. return;
  124. }
  125. const LocalisedStrings originalTranslation (documentOriginal.getAllContent(), false);
  126. documentResult.replaceAllContent (TranslationHelpers::createFinishedTranslationFile (preStrings, postStrings, originalTranslation));
  127. }
  128. void scanProject()
  129. {
  130. if (Project* project = ProjucerApplication::getApp().mainWindowList.getFrontmostProject())
  131. setPreTranslationText (TranslationHelpers::getPreTranslationText (*project));
  132. else
  133. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon, "Translation Tool",
  134. "This will only work when you have a project open!");
  135. }
  136. void scanFolder()
  137. {
  138. FileChooser fc ("Choose the root folder to search for the TRANS macros",
  139. File(), "*");
  140. if (fc.browseForDirectory())
  141. {
  142. StringArray strings;
  143. TranslationHelpers::scanFolderForTranslations (strings, fc.getResult());
  144. setPreTranslationText (TranslationHelpers::mungeStrings(strings));
  145. }
  146. }
  147. void loadFile()
  148. {
  149. FileChooser fc ("Choose a translation file to load",
  150. File(), "*");
  151. if (fc.browseForFileToOpen())
  152. {
  153. const LocalisedStrings loadedStrings (fc.getResult(), false);
  154. documentOriginal.replaceAllContent (fc.getResult().loadFileAsString().trim());
  155. setPreTranslationText (TranslationHelpers::getPreTranslationText (loadedStrings));
  156. }
  157. }
  158. void setPreTranslationText (const String& text)
  159. {
  160. documentPre.replaceAllContent (text);
  161. editorPre.grabKeyboardFocus();
  162. editorPre.selectAll();
  163. }
  164. };
  165. #endif // JUCER_TRANSLATIONTOOLCOMPONENT_H_INCLUDED