FlatCAMTranslation.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. # ##########################################################
  8. import os
  9. import sys
  10. from pathlib import Path
  11. from PyQt5 import QtWidgets, QtGui
  12. from PyQt5.QtCore import QSettings
  13. from flatcamGUI.GUIElements import log
  14. import gettext
  15. # import builtins
  16. #
  17. # if '_' not in builtins.__dict__:
  18. # _ = gettext.gettext
  19. # ISO639-1 codes from here: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  20. languages_dict = {
  21. 'zh': 'Chinese',
  22. 'de': 'German',
  23. 'en': 'English',
  24. 'es': 'Spanish',
  25. 'fr': 'French',
  26. 'it': 'Italian',
  27. 'ro': 'Romanian',
  28. 'ru': 'Russian',
  29. 'pt_BR': 'Brazilian Portuguese',
  30. }
  31. translations = {}
  32. languages_path_search = ''
  33. def load_languages():
  34. available_translations = []
  35. languages_path_search = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
  36. try:
  37. available_translations = next(os.walk(languages_path_search))[1]
  38. except StopIteration:
  39. if not available_translations:
  40. languages_path_search = os.path.join(Path(__file__).parents[1], 'locale')
  41. try:
  42. available_translations = next(os.walk(languages_path_search))[1]
  43. except StopIteration:
  44. pass
  45. for lang in available_translations:
  46. try:
  47. if lang in languages_dict.keys():
  48. translations[lang] = languages_dict[lang]
  49. except KeyError as e:
  50. log.debug("FlatCAMTranslations.load_languages() --> %s" % str(e))
  51. return translations
  52. def languages_dir():
  53. return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
  54. def languages_dir_cx_freeze():
  55. return os.path.join(Path(__file__).parents[1], 'locale')
  56. def on_language_apply_click(app, restart=False):
  57. """
  58. Using instructions from here:
  59. https://inventwithpython.com/blog/2014/12/20/translate-your-python-3-program-with-the-gettext-module/
  60. :return:
  61. """
  62. name = app.ui.general_defaults_form.general_app_group.language_cb.currentText()
  63. # do nothing if trying to apply the language that is the current language (already applied).
  64. settings = QSettings("Open Source", "FlatCAM")
  65. if settings.contains("language"):
  66. current_language = settings.value('language', type=str)
  67. if current_language == name:
  68. return
  69. if restart:
  70. msgbox = QtWidgets.QMessageBox()
  71. msgbox.setText(_("The application will restart."))
  72. msgbox.setInformativeText(_("Are you sure do you want to change the current language to %s?") %
  73. name.capitalize())
  74. msgbox.setWindowTitle(_("Apply Language ..."))
  75. msgbox.setWindowIcon(QtGui.QIcon('share/language32.png'))
  76. bt_yes = msgbox.addButton(_("Yes"), QtWidgets.QMessageBox.YesRole)
  77. bt_no = msgbox.addButton(_("No"), QtWidgets.QMessageBox.NoRole)
  78. msgbox.setDefaultButton(bt_yes)
  79. msgbox.exec_()
  80. response = msgbox.clickedButton()
  81. if response == bt_no:
  82. return
  83. else:
  84. settings = QSettings("Open Source", "FlatCAM")
  85. saved_language = name
  86. settings.setValue('language', saved_language)
  87. # This will write the setting to the platform specific storage.
  88. del settings
  89. restart_program(app=app)
  90. def apply_language(domain, lang=None):
  91. lang_code = ''
  92. if lang is None:
  93. settings = QSettings("Open Source", "FlatCAM")
  94. if settings.contains("language"):
  95. name = settings.value('language')
  96. else:
  97. name = settings.value('English')
  98. # in case the 'language' parameter is not in QSettings add it to QSettings and it's value is
  99. # the default language, English
  100. settings.setValue('language', 'English')
  101. # This will write the setting to the platform specific storage.
  102. del settings
  103. else:
  104. name = str(lang)
  105. for lang_code, lang_usable in load_languages().items():
  106. if lang_usable == name:
  107. # break and then use the current key as language
  108. break
  109. if lang_code == '':
  110. return "no language"
  111. else:
  112. try:
  113. current_lang = gettext.translation(str(domain), localedir=languages_dir(), languages=[lang_code])
  114. current_lang.install()
  115. except Exception as e:
  116. log.debug("FlatCAMTranslation.apply_language() --> %s. Perhaps is Cx_freeze-ed?" % str(e))
  117. try:
  118. current_lang = gettext.translation(str(domain),
  119. localedir=languages_dir_cx_freeze(),
  120. languages=[lang_code])
  121. current_lang.install()
  122. except Exception as e:
  123. log.debug("FlatCAMTranslation.apply_language() --> %s" % str(e))
  124. return name
  125. def restart_program(app):
  126. """Restarts the current program.
  127. Note: this function does not return. Any cleanup action (like
  128. saving data) must be done before calling this function.
  129. """
  130. if app.should_we_save and app.collection.get_list():
  131. msgbox = QtWidgets.QMessageBox()
  132. msgbox.setText(_("There are files/objects modified in FlatCAM. "
  133. "\n"
  134. "Do you want to Save the project?"))
  135. msgbox.setWindowTitle(_("Save changes"))
  136. msgbox.setWindowIcon(QtGui.QIcon('share/save_as.png'))
  137. bt_yes = msgbox.addButton(_('Yes'), QtWidgets.QMessageBox.YesRole)
  138. bt_no = msgbox.addButton(_('No'), QtWidgets.QMessageBox.NoRole)
  139. msgbox.setDefaultButton(bt_yes)
  140. msgbox.exec_()
  141. response = msgbox.clickedButton()
  142. if response == bt_yes:
  143. app.on_file_saveprojectas(thread=True, quit=True)
  144. app.save_defaults()
  145. python = sys.executable
  146. os.execl(python, python, *sys.argv)