项目作者: WohlSoft

项目描述 :
STL-compatible Implementation of Qt Translator core which returns translated string from source text or by trID key
高级语言: C++
项目地址: git://github.com/WohlSoft/QTranslator-STL.git
创建时间: 2016-10-07T00:04:07Z
项目社区:https://github.com/WohlSoft/QTranslator-STL

开源协议:GNU Lesser General Public License v3.0

下载


QTranslator-STL

STL-compatible implementation of Qt Translator core which returns translated string from source text or from trID key

Gives ability to use precompiled qm-translations with non-Qt projects.

Useful for a small console applications or for graphical applications that prefered to use SDL, OpenGL, DirecX, etc. than GUI and has small size.

This implementation was created with using the code of original Qt-Translator which processes qm-files which has been ported to pure STL.

Note: You still need use QMake-based project to allow lupdate correctly dig source files and spawn translation files.
However you can process translations update without using QMake-based project, just by manual specifying source to scan (files, directories, file lists, etc.) and targets to generate: lupdate cpp1.cpp cpp2.cpp -ts cpp_en.ts cpp_ru.ts.
More detail guide you can get by typing lupdate --help. Also possible to redefine names of translating functions!

Why license is LGPL, not a MIT? This implementation uses some of internal Qt code (qtbase/src/corelib/kernel/qtranslator.cpp) which licensed under GNU LGPL license.
If you wish use this in proprietary projects, you have to buy commercial Qt license from Digia, or if you already have Qt license, feel free to use this class everywhere you want.
To use this code statically linked with the non-GPL projects, you must have commercial license from the Qt rightholder

Main features

  • It’s tiny
  • Easy to use
  • No dependencies (even Qt itself doesn’t needed for runtime, but lupdate, lrelease and linguist are needed to work with translations)
  • You still be able to use a power of the Qt Lingust!
  • Allows you have alone global translator without having different translators in different places
  • Gives you a freedom for choisin output string format of tr and qtTrId functions

How to install

  • Copy QTranslatorX folder into your project directory
  • Enable C++11 support if not enabled

Example of usage (Tr-ID based)

  1. #include <string>
  2. #include "QTranslatorX/QTranslatorX"
  3. //! Globally defined translator
  4. QmTranslatorX translator;
  5. /**
  6. * @brief Example of fake qtTrId used for ID-based translations. To correctly compile tr-ID-based qm-file you must use -idbased flag for lrelease utility
  7. * @param trSrc source string
  8. * @return translated UTF8 string
  9. */
  10. std::string qtTrId(const char* trSrc)
  11. {
  12. std::string out = translator.do_translate8(0, trSrc, 0, -1);
  13. if(out.empty())
  14. return std::string(trSrc);
  15. else
  16. return out;
  17. }
  18. int err(const char* errMsg, int code)
  19. {
  20. std::cout << "\n" << errMsg << "\n";
  21. return code;
  22. }
  23. ///
  24. /// Opens qm-file from argument and prints translated phrase
  25. ///
  26. int main(int argc, char**argv)
  27. {
  28. if(argc<=1)
  29. return err("Missing argument! [must be a path to compiled translation file!]", 1);
  30. if(!translator.loadFile(argv[1]))
  31. return err("Can't load translation!", 1);
  32. //% "Hello international world!"
  33. std::cout << qtTrId("HelloWorldID") << "\n";
  34. return 0;
  35. }

Example of usage (Class-name context based)

  1. #include <string>
  2. #include "QTranslatorX/QTranslatorX"
  3. //! Globally defined translator
  4. QmTranslatorX translator;
  5. /**
  6. * @brief Example of the class with TR function (to make context-based non-ID based translations)
  7. */
  8. class Fake
  9. {
  10. //Fake Q_OBJECT macro to avoid "Class 'Fake' lacks Q_OBJECT macro" spawned from lupdate utility
  11. #define Q_OBJECT
  12. Q_OBJECT
  13. #undef Q_OBJECT
  14. public:
  15. /**
  16. * @brief Translating function of context-based translation.
  17. * @param trSrc source string
  18. * @return translated string
  19. *
  20. * Make own class which will contain tr(const char*) or tr(const char*, const char*=0) (wuth developer comments support) function\
  21. * Output you can provide any: std::string, std::u16string, std::u32string or others like std::wstring and any others
  22. */
  23. static std::string tr(const char* trSrc, const char* /*Developer comment*/ = 0)
  24. {
  25. std::string out = translator.do_translate8("Fake", trSrc, 0, -1);
  26. if(out.empty())
  27. return std::string(trSrc);
  28. else
  29. return out;
  30. }
  31. };
  32. int err(const char* errMsg, int code)
  33. {
  34. std::cout << "\n" << errMsg << "\n";
  35. return code;
  36. }
  37. ///
  38. /// Opens qm-file from argument and prints translated phrase
  39. ///
  40. int main(int argc, char**argv)
  41. {
  42. if(argc<=1)
  43. return err("Missing argument! [must be a path to compiled translation file!]", 1);
  44. if(!translator.loadFile(argv[1]))
  45. return err("Can't load translation!", 1);
  46. std::cout << Fake::tr("Hello international world!") << "\n";
  47. return 0;
  48. }