appTranslation.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. import os
  8. import sys
  9. import logging
  10. from pathlib import Path
  11. from PyQt5 import QtWidgets, QtGui
  12. from PyQt5.QtCore import QSettings
  13. import gettext
  14. log = logging.getLogger('base')
  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. 'pt_BR': 'Brazilian Portuguese',
  28. 'ro': 'Romanian',
  29. 'ru': 'Russian',
  30. 'tr': 'Turkish',
  31. }
  32. translations = {}
  33. languages_path_search = ''
  34. def load_languages():
  35. available_translations = []
  36. languages_path_search = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
  37. try:
  38. available_translations = next(os.walk(languages_path_search))[1]
  39. except StopIteration:
  40. if not available_translations:
  41. languages_path_search = os.path.join(str(Path(__file__).parents[1]), 'locale')
  42. try:
  43. available_translations = next(os.walk(languages_path_search))[1]
  44. except StopIteration:
  45. pass
  46. for lang in available_translations:
  47. try:
  48. if lang in languages_dict.keys():
  49. translations[lang] = languages_dict[lang]
  50. except KeyError as e:
  51. log.debug("FlatCAMTranslations.load_languages() --> %s" % str(e))
  52. return translations
  53. def languages_dir():
  54. return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
  55. def languages_dir_cx_freeze():
  56. return os.path.join(Path(__file__).parents[1], 'locale')
  57. def on_language_apply_click(app, restart=False):
  58. """
  59. Using instructions from here:
  60. https://inventwithpython.com/blog/2014/12/20/translate-your-python-3-program-with-the-gettext-module/
  61. :return:
  62. """
  63. name = app.ui.general_defaults_form.general_app_group.language_cb.currentText()
  64. theme_settings = QSettings("Open Source", "FlatCAM")
  65. if theme_settings.contains("theme"):
  66. theme = theme_settings.value('theme', type=str)
  67. else:
  68. theme = 'white'
  69. if theme == 'white':
  70. resource_loc = 'assets/resources'
  71. else:
  72. resource_loc = 'assets/resources'
  73. # do nothing if trying to apply the language that is the current language (already applied).
  74. settings = QSettings("Open Source", "FlatCAM")
  75. if settings.contains("language"):
  76. current_language = settings.value('language', type=str)
  77. if current_language == name:
  78. return
  79. if restart:
  80. msgbox = QtWidgets.QMessageBox()
  81. msgbox.setText(_("The application will restart."))
  82. msgbox.setInformativeText('%s %s?' %
  83. (_("Are you sure do you want to change the current language to"), name.capitalize()))
  84. msgbox.setWindowTitle(_("Apply Language ..."))
  85. msgbox.setWindowIcon(QtGui.QIcon(resource_loc + '/language32.png'))
  86. msgbox.setIcon(QtWidgets.QMessageBox.Question)
  87. bt_yes = msgbox.addButton(_("Yes"), QtWidgets.QMessageBox.YesRole)
  88. bt_no = msgbox.addButton(_("No"), QtWidgets.QMessageBox.NoRole)
  89. msgbox.setDefaultButton(bt_yes)
  90. msgbox.exec_()
  91. response = msgbox.clickedButton()
  92. if response == bt_no:
  93. return
  94. else:
  95. settings = QSettings("Open Source", "FlatCAM")
  96. saved_language = name
  97. settings.setValue('language', saved_language)
  98. # This will write the setting to the platform specific storage.
  99. del settings
  100. restart_program(app=app)
  101. def apply_language(domain, lang=None):
  102. lang_code = ''
  103. if lang is None:
  104. settings = QSettings("Open Source", "FlatCAM")
  105. if settings.contains("language"):
  106. name = settings.value('language')
  107. else:
  108. name = 'English'
  109. # in case the 'language' parameter is not in QSettings add it to QSettings and it's value is
  110. # the default language, English
  111. settings.setValue('language', 'English')
  112. # This will write the setting to the platform specific storage.
  113. del settings
  114. else:
  115. name = str(lang)
  116. for lang_code, lang_usable in load_languages().items():
  117. if lang_usable == name:
  118. # break and then use the current key as language
  119. break
  120. if lang_code == '':
  121. return "no language"
  122. else:
  123. try:
  124. current_lang = gettext.translation(str(domain), localedir=languages_dir(), languages=[lang_code])
  125. current_lang.install()
  126. except Exception as e:
  127. log.debug("FlatCAMTranslation.apply_language() --> %s. Perhaps is Cx_freeze-ed?" % str(e))
  128. try:
  129. current_lang = gettext.translation(str(domain),
  130. localedir=languages_dir_cx_freeze(),
  131. languages=[lang_code])
  132. current_lang.install()
  133. except Exception as e:
  134. log.debug("FlatCAMTranslation.apply_language() --> %s" % str(e))
  135. return name
  136. def restart_program(app, ask=None):
  137. """Restarts the current program.
  138. Note: this function does not return. Any cleanup action (like
  139. saving data) must be done before calling this function.
  140. """
  141. log.debug("FlatCAMTranslation.restart_program()")
  142. theme_settings = QSettings("Open Source", "FlatCAM")
  143. if theme_settings.contains("theme"):
  144. theme = theme_settings.value('theme', type=str)
  145. else:
  146. theme = 'white'
  147. if theme == 'white':
  148. resource_loc = 'assets/resources'
  149. else:
  150. resource_loc = 'assets/resources'
  151. # try to quit the Socket opened by ArgsThread class
  152. try:
  153. app.new_launch.stop.emit()
  154. # app.new_launch.thread_exit = True
  155. # app.new_launch.listener.close()
  156. except Exception as err:
  157. log.debug("FlatCAMTranslation.restart_program() --> %s" % str(err))
  158. # try to quit the QThread that run ArgsThread class
  159. try:
  160. app.listen_th.quit()
  161. except Exception as err:
  162. log.debug("FlatCAMTranslation.restart_program() --> %s" % str(err))
  163. if app.should_we_save and app.collection.get_list() or ask is True:
  164. msgbox = QtWidgets.QMessageBox()
  165. msgbox.setText(_("There are files/objects modified in FlatCAM. "
  166. "\n"
  167. "Do you want to Save the project?"))
  168. msgbox.setWindowTitle(_("Save changes"))
  169. msgbox.setWindowIcon(QtGui.QIcon(resource_loc + '/save_as.png'))
  170. msgbox.setIcon(QtWidgets.QMessageBox.Question)
  171. bt_yes = msgbox.addButton(_('Yes'), QtWidgets.QMessageBox.YesRole)
  172. bt_no = msgbox.addButton(_('No'), QtWidgets.QMessageBox.NoRole)
  173. msgbox.setDefaultButton(bt_yes)
  174. msgbox.exec_()
  175. response = msgbox.clickedButton()
  176. if response == bt_yes:
  177. app.on_file_saveprojectas(use_thread=True, quit_action=True)
  178. app.preferencesUiManager.save_defaults()
  179. python = sys.executable
  180. os.execl(python, python, *sys.argv)