FlatCAMTranslation.py 4.0 KB

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