FlatCAMTranslation.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 PyQt5 import QtWidgets, QtGui
  11. from PyQt5.QtCore import QSettings
  12. from flatcamGUI.GUIElements import log
  13. import gettext
  14. # ISO639-1 codes from here: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  15. languages_dict = {
  16. 'zh': 'Chinese',
  17. 'de': 'German',
  18. 'en': 'English',
  19. 'es': 'Spanish',
  20. 'fr': 'French',
  21. 'it': 'Italian',
  22. 'ro': 'Romanian',
  23. 'ru': 'Russian',
  24. }
  25. translations = {}
  26. languages_path_search = ''
  27. def load_languages():
  28. languages_path_search = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
  29. available_translations = next(os.walk(languages_path_search))[1]
  30. for lang in available_translations:
  31. try:
  32. if lang in languages_dict.keys():
  33. translations[lang] = languages_dict[lang]
  34. except KeyError as e:
  35. log.debug("FlatCAMTranslations.load_languages() --> %s" % str(e))
  36. return translations
  37. def languages_dir():
  38. return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'locale')
  39. def on_language_apply_click(app, restart=False):
  40. """
  41. Using instructions from here:
  42. https://inventwithpython.com/blog/2014/12/20/translate-your-python-3-program-with-the-gettext-module/
  43. :return:
  44. """
  45. name = app.ui.general_defaults_form.general_app_group.language_cb.currentText()
  46. # do nothing if trying to apply the language that is the current language (already applied).
  47. settings = QSettings("Open Source", "FlatCAM")
  48. if settings.contains("language"):
  49. current_language = settings.value('language', type=str)
  50. if current_language == name:
  51. return
  52. if restart:
  53. msgbox = QtWidgets.QMessageBox()
  54. msgbox.setText("The application will restart.")
  55. msgbox.setInformativeText("Are you sure do you want to change the current language to %s?" % name.capitalize())
  56. msgbox.setWindowTitle("Apply Language ...")
  57. msgbox.setWindowIcon(QtGui.QIcon('share/language32.png'))
  58. msgbox.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel)
  59. msgbox.setDefaultButton(QtWidgets.QMessageBox.Yes)
  60. response = msgbox.exec_()
  61. if response == QtWidgets.QMessageBox.Cancel:
  62. return
  63. else:
  64. settings = QSettings("Open Source", "FlatCAM")
  65. saved_language = name
  66. settings.setValue('language', saved_language)
  67. # This will write the setting to the platform specific storage.
  68. del settings
  69. restart_program(app=app)
  70. def apply_language(domain, lang=None):
  71. lang_code = ''
  72. if lang is None:
  73. settings = QSettings("Open Source", "FlatCAM")
  74. if settings.contains("language"):
  75. name = settings.value('language')
  76. else:
  77. name = settings.value('English')
  78. else:
  79. name = str(lang)
  80. for lang_code, lang_usable in load_languages().items():
  81. if lang_usable == name:
  82. # break and then use the current key as language
  83. break
  84. if lang_code == '':
  85. return "no language"
  86. else:
  87. try:
  88. current_lang = gettext.translation(str(domain), localedir=languages_dir(), languages=[lang_code])
  89. current_lang.install()
  90. except Exception as e:
  91. log.debug("FlatCAMTranslation.apply_language() --> %s" % str(e))
  92. return name
  93. def restart_program(app):
  94. """Restarts the current program.
  95. Note: this function does not return. Any cleanup action (like
  96. saving data) must be done before calling this function.
  97. """
  98. app.save_defaults()
  99. python = sys.executable
  100. os.execl(python, python, *sys.argv)