__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """Internationalization module for backend notifications."""
  2. from typing import Any
  3. # English translations
  4. EN = {
  5. "notification": {
  6. # Print events
  7. "print_started": "Print Started",
  8. "print_completed": "Print Completed",
  9. "print_failed": "Print Failed",
  10. "print_stopped": "Print Stopped",
  11. "print_ended": "Print Ended",
  12. "print_progress": "Print {progress}% Complete",
  13. "estimated": "Estimated",
  14. "time": "Time",
  15. "filament": "Filament",
  16. "reason": "Reason",
  17. "unknown": "Unknown",
  18. # Printer events
  19. "printer_offline": "Printer Offline",
  20. "printer_disconnected": "{printer} has disconnected",
  21. "printer_error": "Printer Error: {error_type}",
  22. # Filament
  23. "filament_low": "Filament Low",
  24. "slot_at_percent": "{printer}: Slot {slot} at {percent}%",
  25. # Maintenance
  26. "maintenance_due": "Maintenance Due",
  27. "overdue": "OVERDUE",
  28. "soon": "Soon",
  29. # Test notification
  30. "test_title": "BambuTrack Test",
  31. "test_message": "This is a test notification from BambuTrack. If you see this, notifications are working correctly!",
  32. }
  33. }
  34. # German translations
  35. DE = {
  36. "notification": {
  37. # Print events
  38. "print_started": "Druck gestartet",
  39. "print_completed": "Druck abgeschlossen",
  40. "print_failed": "Druck fehlgeschlagen",
  41. "print_stopped": "Druck gestoppt",
  42. "print_ended": "Druck beendet",
  43. "print_progress": "Druck {progress}% fertig",
  44. "estimated": "Geschätzt",
  45. "time": "Zeit",
  46. "filament": "Filament",
  47. "reason": "Grund",
  48. "unknown": "Unbekannt",
  49. # Printer events
  50. "printer_offline": "Drucker offline",
  51. "printer_disconnected": "{printer} wurde getrennt",
  52. "printer_error": "Druckerfehler: {error_type}",
  53. # Filament
  54. "filament_low": "Wenig Filament",
  55. "slot_at_percent": "{printer}: Slot {slot} bei {percent}%",
  56. # Maintenance
  57. "maintenance_due": "Wartung fällig",
  58. "overdue": "ÜBERFÄLLIG",
  59. "soon": "Bald",
  60. # Test notification
  61. "test_title": "BambuTrack Test",
  62. "test_message": "Dies ist eine Testbenachrichtigung von BambuTrack. Wenn Sie dies sehen, funktionieren die Benachrichtigungen!",
  63. }
  64. }
  65. # All available translations
  66. TRANSLATIONS = {
  67. "en": EN,
  68. "de": DE,
  69. }
  70. def get_translation(lang: str, key: str, **kwargs: Any) -> str:
  71. """
  72. Get a translation string by key with optional interpolation.
  73. Args:
  74. lang: Language code (e.g., 'en', 'de')
  75. key: Dot-separated key path (e.g., 'notification.print_started')
  76. **kwargs: Values to interpolate into the string
  77. Returns:
  78. Translated string, or the key if not found
  79. """
  80. # Fall back to English if language not found
  81. translations = TRANSLATIONS.get(lang, TRANSLATIONS["en"])
  82. # Navigate to the nested key
  83. keys = key.split(".")
  84. value = translations
  85. for k in keys:
  86. if isinstance(value, dict) and k in value:
  87. value = value[k]
  88. else:
  89. # Key not found, fall back to English
  90. value = TRANSLATIONS["en"]
  91. for k2 in keys:
  92. if isinstance(value, dict) and k2 in value:
  93. value = value[k2]
  94. else:
  95. return key # Return key if not found in fallback either
  96. break
  97. if isinstance(value, str):
  98. # Interpolate values
  99. try:
  100. return value.format(**kwargs)
  101. except KeyError:
  102. return value
  103. return key
  104. class Translator:
  105. """Helper class for translations with a specific language."""
  106. def __init__(self, lang: str = "en"):
  107. self.lang = lang if lang in TRANSLATIONS else "en"
  108. def t(self, key: str, **kwargs: Any) -> str:
  109. """Translate a key."""
  110. return get_translation(self.lang, key, **kwargs)