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.

189 lines
7.6KB

  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. {
  24. public:
  25. TranslationToolComponent()
  26. : editorOriginal (documentOriginal, nullptr),
  27. editorPre (documentPre, nullptr),
  28. editorPost (documentPost, nullptr),
  29. editorResult (documentResult, nullptr)
  30. {
  31. instructionsLabel.setText (
  32. "This utility converts translation files to/from a format that can be passed to automatic translation tools."
  33. "\n\n"
  34. "First, choose whether to scan the current project for all TRANS() macros, or "
  35. "pick an existing translation file to load:", dontSendNotification);
  36. addAndMakeVisible (instructionsLabel);
  37. label1.setText ("..then copy-and-paste this annotated text into Google Translate or some other translator:", dontSendNotification);
  38. addAndMakeVisible (label1);
  39. label2.setText ("...then, take the translated result and paste it into the box below:", dontSendNotification);
  40. addAndMakeVisible (label2);
  41. label3.setText ("Finally, click the 'Generate' button, and a translation file will be created below. "
  42. "Remember to update its language code at the top!", dontSendNotification);
  43. addAndMakeVisible (label3);
  44. 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);
  45. addAndMakeVisible (label4);
  46. addAndMakeVisible (editorOriginal);
  47. addAndMakeVisible (editorPre);
  48. addAndMakeVisible (editorPost);
  49. addAndMakeVisible (editorResult);
  50. addAndMakeVisible (generateButton);
  51. generateButton.onClick = [this] { generate(); };
  52. addAndMakeVisible (scanProjectButton);
  53. scanProjectButton.onClick = [this] { scanProject(); };
  54. addAndMakeVisible (scanFolderButton);
  55. scanFolderButton.onClick = [this] { scanFolder(); };
  56. addAndMakeVisible (loadTranslationButton);
  57. loadTranslationButton.onClick = [this] { loadFile(); };
  58. }
  59. void paint (Graphics& g) override
  60. {
  61. g.fillAll (findColour (backgroundColourId));
  62. }
  63. void resized() override
  64. {
  65. const int m = 6;
  66. const int textH = 44;
  67. const int extraH = (7 * textH);
  68. const int editorH = (getHeight() - extraH) / 4;
  69. const int numButtons = 3;
  70. Rectangle<int> r (getLocalBounds().withTrimmedBottom (m));
  71. const int buttonWidth = r.getWidth() / numButtons;
  72. instructionsLabel.setBounds (r.removeFromTop (textH * 2).reduced (m));
  73. r.removeFromTop (m);
  74. Rectangle<int> r2 (r.removeFromTop (textH - (2 * m)));
  75. scanProjectButton .setBounds (r2.removeFromLeft (buttonWidth).reduced (m, 0));
  76. scanFolderButton .setBounds (r2.removeFromLeft (buttonWidth).reduced (m, 0));
  77. loadTranslationButton.setBounds (r2.reduced (m, 0));
  78. label1 .setBounds (r.removeFromTop (textH) .reduced (m));
  79. editorPre.setBounds (r.removeFromTop (editorH).reduced (m, 0));
  80. label2 .setBounds (r.removeFromTop (textH) .reduced (m));
  81. editorPost.setBounds (r.removeFromTop (editorH).reduced (m, 0));
  82. r2 = r.removeFromTop (textH);
  83. generateButton.setBounds (r2.removeFromRight (152).reduced (m));
  84. label3 .setBounds (r2.reduced (m));
  85. editorResult .setBounds (r.removeFromTop (editorH).reduced (m, 0));
  86. label4 .setBounds (r.removeFromTop (textH).reduced (m));
  87. editorOriginal.setBounds (r.reduced (m, 0));
  88. }
  89. private:
  90. CodeDocument documentOriginal, documentPre, documentPost, documentResult;
  91. CodeEditorComponent editorOriginal, editorPre, editorPost, editorResult;
  92. Label label1, label2, label3, label4;
  93. Label instructionsLabel;
  94. TextButton generateButton { TRANS("Generate") };
  95. TextButton scanProjectButton { "Scan project for TRANS macros" };
  96. TextButton scanFolderButton { "Scan folder for TRANS macros" };
  97. TextButton loadTranslationButton { "Load existing translation file..."};
  98. void generate()
  99. {
  100. StringArray preStrings (TranslationHelpers::breakApart (documentPre.getAllContent()));
  101. StringArray postStrings (TranslationHelpers::breakApart (documentPost.getAllContent()));
  102. if (postStrings.size() != preStrings.size())
  103. {
  104. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  105. TRANS("Error"),
  106. TRANS("The pre- and post-translation text doesn't match!\n\n"
  107. "Perhaps it got mangled by the translator?"));
  108. return;
  109. }
  110. const LocalisedStrings originalTranslation (documentOriginal.getAllContent(), false);
  111. documentResult.replaceAllContent (TranslationHelpers::createFinishedTranslationFile (preStrings, postStrings, originalTranslation));
  112. }
  113. void scanProject()
  114. {
  115. if (Project* project = ProjucerApplication::getApp().mainWindowList.getFrontmostProject())
  116. setPreTranslationText (TranslationHelpers::getPreTranslationText (*project));
  117. else
  118. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon, "Translation Tool",
  119. "This will only work when you have a project open!");
  120. }
  121. void scanFolder()
  122. {
  123. FileChooser fc ("Choose the root folder to search for the TRANS macros",
  124. File(), "*");
  125. if (fc.browseForDirectory())
  126. {
  127. StringArray strings;
  128. TranslationHelpers::scanFolderForTranslations (strings, fc.getResult());
  129. setPreTranslationText (TranslationHelpers::mungeStrings(strings));
  130. }
  131. }
  132. void loadFile()
  133. {
  134. FileChooser fc ("Choose a translation file to load",
  135. File(), "*");
  136. if (fc.browseForFileToOpen())
  137. {
  138. const LocalisedStrings loadedStrings (fc.getResult(), false);
  139. documentOriginal.replaceAllContent (fc.getResult().loadFileAsString().trim());
  140. setPreTranslationText (TranslationHelpers::getPreTranslationText (loadedStrings));
  141. }
  142. }
  143. void setPreTranslationText (const String& text)
  144. {
  145. documentPre.replaceAllContent (text);
  146. editorPre.grabKeyboardFocus();
  147. editorPre.selectAll();
  148. }
  149. };