FlatCAMTranslation.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. import logging
  11. from pathlib import Path
  12. from PyQt5 import QtWidgets, QtGui
  13. from PyQt5.QtCore import QSettings
  14. import gettext
  15. log = logging.getLogger('base')
  16. # import builtins
  17. #
  18. # if '_' not in builtins.__dict__:
  19. # _ = gettext.gettext
  20. # ISO639-1 codes from here: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  21. languages_dict = {
  22. 'zh': 'Chinese',
  23. 'de': 'German',
  24. 'en': 'English',
  25. 'es': 'Spanish',
  26. 'fr': 'French',
  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(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. # do nothing if trying to apply the language that is the current language (already applied).
  65. settings = QSettings("Open Source", "FlatCAM")
  66. if settings.contains("language"):
  67. current_language = settings.value('language', type=str)
  68. if current_language == name:
  69. return
  70. if restart:
  71. msgbox = QtWidgets.QMessageBox()
  72. msgbox.setText(_("The application will restart."))
  73. msgbox.setInformativeText('%s %s?' %
  74. (_("Are you sure do you want to change the current language to"), name.capitalize()))
  75. msgbox.setWindowTitle(_("Apply Language ..."))
  76. msgbox.setWindowIcon(QtGui.QIcon('share/language32.png'))
  77. bt_yes = msgbox.addButton(_("Yes"), QtWidgets.QMessageBox.YesRole)
  78. bt_no = msgbox.addButton(_("No"), QtWidgets.QMessageBox.NoRole)
  79. msgbox.setDefaultButton(bt_yes)
  80. msgbox.exec_()
  81. response = msgbox.clickedButton()
  82. if response == bt_no:
  83. return
  84. else:
  85. settings = QSettings("Open Source", "FlatCAM")
  86. saved_language = name
  87. settings.setValue('language', saved_language)
  88. # This will write the setting to the platform specific storage.
  89. del settings
  90. restart_program(app=app)
  91. def apply_language(domain, lang=None):
  92. lang_code = ''
  93. if lang is None:
  94. settings = QSettings("Open Source", "FlatCAM")
  95. if settings.contains("language"):
  96. name = settings.value('language')
  97. else:
  98. name = settings.value('English')
  99. # in case the 'language' parameter is not in QSettings add it to QSettings and it's value is
  100. # the default language, English
  101. settings.setValue('language', 'English')
  102. # This will write the setting to the platform specific storage.
  103. del settings
  104. else:
  105. name = str(lang)
  106. for lang_code, lang_usable in load_languages().items():
  107. if lang_usable == name:
  108. # break and then use the current key as language
  109. break
  110. if lang_code == '':
  111. return "no language"
  112. else:
  113. try:
  114. current_lang = gettext.translation(str(domain), localedir=languages_dir(), languages=[lang_code])
  115. current_lang.install()
  116. except Exception as e:
  117. log.debug("FlatCAMTranslation.apply_language() --> %s. Perhaps is Cx_freeze-ed?" % str(e))
  118. try:
  119. current_lang = gettext.translation(str(domain),
  120. localedir=languages_dir_cx_freeze(),
  121. languages=[lang_code])
  122. current_lang.install()
  123. except Exception as e:
  124. log.debug("FlatCAMTranslation.apply_language() --> %s" % str(e))
  125. return name
  126. def restart_program(app, ask=None):
  127. """Restarts the current program.
  128. Note: this function does not return. Any cleanup action (like
  129. saving data) must be done before calling this function.
  130. """
  131. if app.should_we_save and app.collection.get_list() or ask is True:
  132. msgbox = QtWidgets.QMessageBox()
  133. msgbox.setText(_("There are files/objects modified in FlatCAM. "
  134. "\n"
  135. "Do you want to Save the project?"))
  136. msgbox.setWindowTitle(_("Save changes"))
  137. msgbox.setWindowIcon(QtGui.QIcon('share/save_as.png'))
  138. bt_yes = msgbox.addButton(_('Yes'), QtWidgets.QMessageBox.YesRole)
  139. bt_no = msgbox.addButton(_('No'), QtWidgets.QMessageBox.NoRole)
  140. msgbox.setDefaultButton(bt_yes)
  141. msgbox.exec_()
  142. response = msgbox.clickedButton()
  143. if response == bt_yes:
  144. app.on_file_saveprojectas(use_thread=True, quit_action=True)
  145. app.save_defaults()
  146. python = sys.executable
  147. os.execl(python, python, *sys.argv)