ccTranslationManager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. //##########################################################################
  3. //# #
  4. //# CLOUDCOMPARE #
  5. //# #
  6. //# This program is free software; you can redistribute it and/or modify #
  7. //# it under the terms of the GNU General Public License as published by #
  8. //# the Free Software Foundation; version 2 or later of the License. #
  9. //# #
  10. //# This program is distributed in the hope that it will be useful, #
  11. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  12. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  13. //# GNU General Public License for more details. #
  14. //# #
  15. //# COPYRIGHT: CloudCompare project #
  16. //# #
  17. //##########################################################################
  18. #include "CCAppCommon.h"
  19. //Qt
  20. #include <QMenu>
  21. #include <QPair>
  22. #include <QVector>
  23. //! Translation manager
  24. class CCAPPCOMMON_LIB_API ccTranslationManager : public QObject
  25. {
  26. Q_OBJECT
  27. public:
  28. static ccTranslationManager& Get();
  29. ~ccTranslationManager() override = default;
  30. //! Register a file prefix for translation files.
  31. /** The files should be named <prefix>_<lang>.ts where <lang> is the 2-letter ISO 639
  32. language code in lowercase.
  33. e.g. CloudCompare_fr.ts
  34. \param prefix The prefix of the file to register
  35. \param path The path to look for the files in
  36. **/
  37. void registerTranslatorFile(const QString& prefix, const QString& path);
  38. //! Using the translation file prefixes that were registered, load the actual translations
  39. inline void loadTranslations() { loadTranslation(languagePref()); }
  40. //! Using the translation file prefixes that were registered, load the actual
  41. //! translation by the 2-letter ISO 639 language code in lowercase.
  42. void loadTranslation(QString language);
  43. //! Populate the menu with a list of languages found using files in 'pathToTranslationFiles'
  44. void populateMenu(QMenu *menu, const QString &pathToTranslationFiles);
  45. protected:
  46. explicit ccTranslationManager() = default;
  47. private: // methods
  48. struct CCAPPCOMMON_LIB_API TranslatorFile
  49. {
  50. QString prefix;
  51. QString path;
  52. };
  53. using TranslatorFileList = QVector<TranslatorFile>;
  54. using TranslationInfo = QPair<QString, QString>;
  55. using LanguageList = QVector<TranslationInfo>;
  56. QString languagePref() const;
  57. //! Generate a list of available languages based on the files in the "translation" directory.
  58. LanguageList availableLanguages(const QString& appName, const QString& pathToTranslationFiles) const;
  59. void setLanguagePref(const QString& languageCode);
  60. private: // members
  61. TranslatorFileList mTranslatorFileInfo;
  62. };