translations.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 += "{0} {1} ".format(
  80. years,
  81. str_[0] if years == 1 else str_[1]
  82. )
  83. days = int(minutes / oneDay)
  84. if days:
  85. minutes -= days * oneDay
  86. returnStr += "{0} {1} ".format(days, str_[2] if days == 1 else str_[3])
  87. hours = int(minutes / oneHour)
  88. if hours:
  89. minutes -= hours * oneHour
  90. returnStr += "{0} {1} ".format(
  91. hours,
  92. str_[4] if hours == 1 else str_[5]
  93. )
  94. if minutes:
  95. returnStr += "{0} {1} ".format(
  96. minutes,
  97. str_[6] if minutes == 1 else str_[7]
  98. )
  99. return returnStr
  100. def normalize(localeName):
  101. str_ = locale.normalize(localeName)
  102. enc = locale.getpreferredencoding(do_setlocale=True)
  103. str_ = str_.split('.', 1)[0]
  104. return str_ + "." + enc
  105. def localeTest(normalizedLocaleName):
  106. try:
  107. locale.setlocale(locale.LC_TIME, normalizedLocaleName)
  108. except locale.Error:
  109. return False
  110. return True
  111. _ = gettext.gettext
  112. if Language:
  113. # Set LC_TIME if system locale found.
  114. __SYSTEM_LOCALE_FOUND = localeTest(
  115. normalize(Language[0])
  116. )
  117. if not __SYSTEM_LOCALE_FOUND:
  118. warning(
  119. "`{0}` not found. Cannot translate dates and time."
  120. .format(normalize(Language[0]))
  121. )
  122. # Set local .mo file.
  123. lang = None
  124. try:
  125. # First try system locale directory
  126. lang = gettext.translation('searx-qt', languages=[Language[0]])
  127. except FileNotFoundError:
  128. xdgDataHome = os.getenv('XDG_DATA_HOME')
  129. if xdgDataHome is not None:
  130. localeDir = os.path.join(xdgDataHome, 'locale')
  131. try:
  132. lang = gettext.translation('searx-qt', localedir=localeDir,
  133. languages=[Language[0]])
  134. except FileNotFoundError:
  135. pass
  136. if lang:
  137. lang.install()
  138. _ = lang.gettext
  139. else:
  140. warning(
  141. "Locale file (.mo) for language `{0}` not found!"
  142. .format(Language[0])
  143. )