FlatCAMTranslation.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. '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. theme_settings = QSettings("Open Source", "FlatCAM")
  64. if theme_settings.contains("theme"):
  65. theme = theme_settings.value('theme', type=str)
  66. else:
  67. theme = 'white'
  68. if theme == 'white':
  69. resource_loc = 'share'
  70. else:
  71. resource_loc = 'share'
  72. # do nothing if trying to apply the language that is the current language (already applied).
  73. settings = QSettings("Open Source", "FlatCAM")
  74. if settings.contains("language"):
  75. current_language = settings.value('language', type=str)
  76. if current_language == name:
  77. return
  78. if restart:
  79. msgbox = QtWidgets.QMessageBox()
  80. msgbox.setText(_("The application will restart."))
  81. msgbox.setInformativeText('%s %s?' %
  82. (_("Are you sure do you want to change the current language to"), name.capitalize()))
  83. msgbox.setWindowTitle(_("Apply Language ..."))
  84. msgbox.setWindowIcon(QtGui.QIcon(resource_loc + '/language32.png'))
  85. bt_yes = msgbox.addButton(_("Yes"), QtWidgets.QMessageBox.YesRole)
  86. bt_no = msgbox.addButton(_("No"), QtWidgets.QMessageBox.NoRole)
  87. msgbox.setDefaultButton(bt_yes)
  88. msgbox.exec_()
  89. response = msgbox.clickedButton()
  90. if response == bt_no:
  91. return
  92. else:
  93. settings = QSettings("Open Source", "FlatCAM")
  94. saved_language = name
  95. settings.setValue('language', saved_language)
  96. # This will write the setting to the platform specific storage.
  97. del settings
  98. restart_program(app=app)
  99. def apply_language(domain, lang=None):
  100. lang_code = ''
  101. if lang is None:
  102. settings = QSettings("Open Source", "FlatCAM")
  103. if settings.contains("language"):
  104. name = settings.value('language')
  105. else:
  106. name = settings.value('English')
  107. # in case the 'language' parameter is not in QSettings add it to QSettings and it's value is
  108. # the default language, English
  109. settings.setValue('language', 'English')
  110. # This will write the setting to the platform specific storage.
  111. del settings
  112. else:
  113. name = str(lang)
  114. for lang_code, lang_usable in load_languages().items():
  115. if lang_usable == name:
  116. # break and then use the current key as language
  117. break
  118. if lang_code == '':
  119. return "no language"
  120. else:
  121. try:
  122. current_lang = gettext.translation(str(domain), localedir=languages_dir(), languages=[lang_code])
  123. current_lang.install()
  124. except Exception as e:
  125. log.debug("FlatCAMTranslation.apply_language() --> %s. Perhaps is Cx_freeze-ed?" % str(e))
  126. try:
  127. current_lang = gettext.translation(str(domain),
  128. localedir=languages_dir_cx_freeze(),
  129. languages=[lang_code])
  130. current_lang.install()
  131. except Exception as e:
  132. log.debug("FlatCAMTranslation.apply_language() --> %s" % str(e))
  133. return name
  134. def restart_program(app, ask=None):
  135. """Restarts the current program.
  136. Note: this function does not return. Any cleanup action (like
  137. saving data) must be done before calling this function.
  138. """
  139. theme_settings = QSettings("Open Source", "FlatCAM")
  140. if theme_settings.contains("theme"):
  141. theme = theme_settings.value('theme', type=str)
  142. else:
  143. theme = 'white'
  144. if theme == 'white':
  145. resource_loc = 'share'
  146. else:
  147. resource_loc = 'share'
  148. # close the Socket in ArgsThread class
  149. app.new_launch.listener.close()
  150. # close the QThread that runs ArgsThread class
  151. app.th.quit()
  152. if app.should_we_save and app.collection.get_list() or ask is True:
  153. msgbox = QtWidgets.QMessageBox()
  154. msgbox.setText(_("There are files/objects modified in FlatCAM. "
  155. "\n"
  156. "Do you want to Save the project?"))
  157. msgbox.setWindowTitle(_("Save changes"))
  158. msgbox.setWindowIcon(QtGui.QIcon(resource_loc + '/save_as.png'))
  159. bt_yes = msgbox.addButton(_('Yes'), QtWidgets.QMessageBox.YesRole)
  160. bt_no = msgbox.addButton(_('No'), QtWidgets.QMessageBox.NoRole)
  161. msgbox.setDefaultButton(bt_yes)
  162. msgbox.exec_()
  163. response = msgbox.clickedButton()
  164. if response == bt_yes:
  165. app.on_file_saveprojectas(use_thread=True, quit_action=True)
  166. app.save_defaults()
  167. python = sys.executable
  168. os.execl(python, python, *sys.argv)