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.

188 lines
7.6KB

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