translations.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- coding: utf-8 -*-
  2. ########################################################################
  3. # Searx-Qt - Lightweight desktop application for Searx.
  4. # Copyright (C) 2020-2022 CYBERDEViL
  5. #
  6. # This file is part of Searx-Qt.
  7. #
  8. # Searx-Qt is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # Searx-Qt is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #
  21. ########################################################################
  22. import gettext
  23. import locale
  24. import os
  25. from time import strftime, localtime
  26. from searxqt.core.log import warning
  27. __SYSTEM_LOCALE_FOUND = False
  28. # List all available languages.
  29. Languages = [
  30. ('de_DE', 'Deutsch'),
  31. ('nl_NL', 'Nederlands'),
  32. ('es_ES', 'Español')
  33. ]
  34. # Set searx-qt default language
  35. Language = None
  36. # Find system default
  37. defaultLanguage, defaultEncoding = locale.getlocale()
  38. # Set current language to system default if found.
  39. index = 0
  40. for key, name in Languages:
  41. if defaultLanguage == key:
  42. Language = Languages[index]
  43. break
  44. index += 1
  45. def timeToString(time_, fmt="%a %d %b %Y %H:%M:%S"):
  46. """
  47. @param time: Time in seconds to format to string.
  48. @type time: uint
  49. @param fmt: Format; see
  50. https://docs.python.org/3/library/time.html#time.strftime
  51. @type fmt: str
  52. """
  53. if __SYSTEM_LOCALE_FOUND:
  54. locale.setlocale(locale.LC_TIME, normalize(Language[0]))
  55. return strftime(fmt, localtime(time_))
  56. def durationMinutesToString(minutes):
  57. """ Convert minutes to a string. Example:
  58. 3 years 30 days 1 hour 30 minutes
  59. @param minutes: Minutes to convert to duration string.
  60. @type minutes: int
  61. 60 * 24 = 1440 minutes in 1 day.
  62. 365 * 1440 = 525600 minutes in 1 year.
  63. """
  64. if not minutes:
  65. return "-"
  66. oneHour = 60
  67. oneDay = 1440
  68. oneYear = 525600
  69. str_ = [
  70. _("year"), _("years"),
  71. _("day"), _("days"),
  72. _("hour"), _("hours"),
  73. _("min"), _("mins")
  74. ]
  75. returnStr = ""
  76. years = int(minutes / oneYear)
  77. if years:
  78. minutes -= years * oneYear
  79. returnStr += f"{years} {str_[0] if years == 1 else str_[1]}"
  80. days = int(minutes / oneDay)
  81. if days:
  82. minutes -= days * oneDay
  83. returnStr += f"{days} {str_[2] if days == 1 else str_[3]}"
  84. hours = int(minutes / oneHour)
  85. if hours:
  86. minutes -= hours * oneHour
  87. returnStr += f"{hours} {str_[4] if hours == 1 else str_[5]}"
  88. if minutes:
  89. returnStr += f"{minutes} {str_[6] if minutes == 1 else str_[7]}"
  90. return returnStr
  91. def normalize(localeName):
  92. str_ = locale.normalize(localeName)
  93. enc = locale.getpreferredencoding(do_setlocale=True)
  94. str_ = str_.split('.', 1)[0]
  95. return str_ + "." + enc
  96. def localeTest(normalizedLocaleName):
  97. try:
  98. locale.setlocale(locale.LC_TIME, normalizedLocaleName)
  99. except locale.Error:
  100. return False
  101. return True
  102. _ = gettext.gettext
  103. if Language:
  104. # Set LC_TIME if system locale found.
  105. __SYSTEM_LOCALE_FOUND = localeTest(
  106. normalize(Language[0])
  107. )
  108. if not __SYSTEM_LOCALE_FOUND:
  109. warning(f"`{normalize(Language[0])}` not found. " \
  110. "Cannot translate dates and time.")
  111. # Set .mo file.
  112. lang = None
  113. # First try XDG (~/.local/)
  114. xdgDataHome = os.getenv('XDG_DATA_HOME')
  115. if xdgDataHome is not None:
  116. localeDir = os.path.join(xdgDataHome, 'locale')
  117. try:
  118. lang = gettext.translation('searx-qt', localedir=localeDir,
  119. languages=[Language[0]])
  120. except FileNotFoundError:
  121. pass
  122. # Not found, check system locale directory
  123. if not lang:
  124. try:
  125. lang = gettext.translation('searx-qt', languages=[Language[0]])
  126. except FileNotFoundError:
  127. pass
  128. if lang:
  129. lang.install()
  130. _ = lang.gettext
  131. else:
  132. warning(f"Locale file (.mo) for language `{Language[0]}` not found!")