FlatCAMTranslation.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 'hu': 'Hungarian',
  27. 'it': 'Italian',
  28. 'ro': 'Romanian',
  29. 'ru': 'Russian',
  30. 'pt_BR': 'Brazilian Portuguese',
  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. bt_yes = msgbox.addButton(_("Yes"), QtWidgets.QMessageBox.YesRole)
  87. bt_no = msgbox.addButton(_("No"), QtWidgets.QMessageBox.NoRole)
  88. msgbox.setDefaultButton(bt_yes)
  89. msgbox.exec_()
  90. response = msgbox.clickedButton()
  91. if response == bt_no:
  92. return
  93. else:
  94. settings = QSettings("Open Source", "FlatCAM")
  95. saved_language = name
  96. settings.setValue('language', saved_language)
  97. # This will write the setting to the platform specific storage.
  98. del settings
  99. restart_program(app=app)
  100. def apply_language(domain, lang=None):
  101. lang_code = ''
  102. if lang is None:
  103. settings = QSettings("Open Source", "FlatCAM")
  104. if settings.contains("language"):
  105. name = settings.value('language')
  106. else:
  107. name = settings.value('English')
  108. # in case the 'language' parameter is not in QSettings add it to QSettings and it's value is
  109. # the default language, English
  110. settings.setValue('language', 'English')
  111. # This will write the setting to the platform specific storage.
  112. del settings
  113. else:
  114. name = str(lang)
  115. for lang_code, lang_usable in load_languages().items():
  116. if lang_usable == name:
  117. # break and then use the current key as language
  118. break
  119. if lang_code == '':
  120. return "no language"
  121. else:
  122. try:
  123. current_lang = gettext.translation(str(domain), localedir=languages_dir(), languages=[lang_code])
  124. current_lang.install()
  125. except Exception as e:
  126. log.debug("FlatCAMTranslation.apply_language() --> %s. Perhaps is Cx_freeze-ed?" % str(e))
  127. try:
  128. current_lang = gettext.translation(str(domain),
  129. localedir=languages_dir_cx_freeze(),
  130. languages=[lang_code])
  131. current_lang.install()
  132. except Exception as e:
  133. log.debug("FlatCAMTranslation.apply_language() --> %s" % str(e))
  134. return name
  135. def restart_program(app, ask=None):
  136. """Restarts the current program.
  137. Note: this function does not return. Any cleanup action (like
  138. saving data) must be done before calling this function.
  139. """
  140. theme_settings = QSettings("Open Source", "FlatCAM")
  141. if theme_settings.contains("theme"):
  142. theme = theme_settings.value('theme', type=str)
  143. else:
  144. theme = 'white'
  145. if theme == 'white':
  146. resource_loc = 'assets/resources'
  147. else:
  148. resource_loc = 'assets/resources'
  149. # try to quit the Socket opened by ArgsThread class
  150. try:
  151. app.new_launch.stop.emit()
  152. except Exception as err:
  153. log.debug("FlatCAMTranslation.restart_program() --> %s" % str(err))
  154. # try to quit the QThread that run ArgsThread class
  155. try:
  156. app.th.quit()
  157. except Exception as err:
  158. log.debug("FlatCAMTranslation.restart_program() --> %s" % str(err))
  159. if app.should_we_save and app.collection.get_list() or ask is True:
  160. msgbox = QtWidgets.QMessageBox()
  161. msgbox.setText(_("There are files/objects modified in FlatCAM. "
  162. "\n"
  163. "Do you want to Save the project?"))
  164. msgbox.setWindowTitle(_("Save changes"))
  165. msgbox.setWindowIcon(QtGui.QIcon(resource_loc + '/save_as.png'))
  166. bt_yes = msgbox.addButton(_('Yes'), QtWidgets.QMessageBox.YesRole)
  167. bt_no = msgbox.addButton(_('No'), QtWidgets.QMessageBox.NoRole)
  168. msgbox.setDefaultButton(bt_yes)
  169. msgbox.exec_()
  170. response = msgbox.clickedButton()
  171. if response == bt_yes:
  172. app.on_file_saveprojectas(use_thread=True, quit_action=True)
  173. app.preferencesUiManager.save_defaults()
  174. python = sys.executable
  175. os.execl(python, python, *sys.argv)