translations.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. ########################################################################
  3. # Searx-qt - Lightweight desktop application for SearX.
  4. # Copyright (C) 2020 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. __SYSTEM_LOCALE_FOUND = False
  27. # List all available languages.
  28. Languages = [
  29. ('de_DE', 'Deutsch'),
  30. ('nl_NL', 'Nederlands'),
  31. ('es_ES', 'Español')
  32. ]
  33. # Set searx-qt default language
  34. Language = None
  35. # Find system default
  36. defaultLanguage, defaultEncoding = locale.getlocale()
  37. # Set current language to system default if found.
  38. index = 0
  39. for key, name in Languages:
  40. if defaultLanguage == key:
  41. Language = Languages[index]
  42. break
  43. index += 1
  44. def timeToString(time_, fmt="%a %d %b %Y %H:%M:%S"):
  45. """
  46. @param time: Time in seconds to format to string.
  47. @type time: uint
  48. @param fmt: Format; see
  49. https://docs.python.org/3/library/time.html#time.strftime
  50. @type fmt: str
  51. """
  52. if __SYSTEM_LOCALE_FOUND:
  53. locale.setlocale(locale.LC_TIME, normalize(Language[0]))
  54. return strftime(fmt, localtime(time_))
  55. def durationMinutesToString(minutes):
  56. """ Convert minutes to a string. Example:
  57. 3 years 30 days 1 hour 30 minutes
  58. @param minutes: Minutes to convert to duration string.
  59. @type minutes: int
  60. 60 * 24 = 1440 minutes in 1 day.
  61. 365 * 1440 = 525600 minutes in 1 year.
  62. """
  63. if not minutes:
  64. return "-"
  65. oneHour = 60
  66. oneDay = 1440
  67. oneYear = 525600
  68. str_ = [
  69. _("year"), _("years"),
  70. _("day"), _("days"),
  71. _("hour"), _("hours"),
  72. _("min"), _("mins")
  73. ]
  74. returnStr = ""
  75. years = int(minutes / oneYear)
  76. if years:
  77. minutes -= years * oneYear
  78. returnStr += "{0} {1} ".format(
  79. years,
  80. str_[0] if years == 1 else str_[1]
  81. )
  82. days = int(minutes / oneDay)
  83. if days:
  84. minutes -= days * oneDay
  85. returnStr += "{0} {1} ".format(days, str_[2] if days == 1 else str_[3])
  86. hours = int(minutes / oneHour)
  87. if hours:
  88. minutes -= hours * oneHour
  89. returnStr += "{0} {1} ".format(
  90. hours,
  91. str_[4] if hours == 1 else str_[5]
  92. )
  93. if minutes:
  94. returnStr += "{0} {1} ".format(
  95. minutes,
  96. str_[6] if minutes == 1 else str_[7]
  97. )
  98. return returnStr
  99. def normalize(localeName):
  100. str_ = locale.normalize(localeName)
  101. enc = locale.getpreferredencoding(do_setlocale=True)
  102. str_ = str_.split('.', 1)[0]
  103. return str_ + "." + enc
  104. def localeTest(normalizedLocaleName):
  105. try:
  106. locale.setlocale(locale.LC_TIME, normalizedLocaleName)
  107. except locale.Error:
  108. return False
  109. return True
  110. _ = gettext.gettext
  111. if Language:
  112. # Set LC_TIME if system locale found.
  113. __SYSTEM_LOCALE_FOUND = localeTest(
  114. normalize(Language[0])
  115. )
  116. if not __SYSTEM_LOCALE_FOUND:
  117. print("Warning! `{0}` not found. Cannot translate dates and time."
  118. .format(normalize(Language[0])))
  119. # Set local .mo file.
  120. lang = None
  121. try:
  122. # First try system locale directory
  123. lang = gettext.translation('searx-qt', languages=[Language[0]])
  124. except FileNotFoundError:
  125. xdgDataHome = os.getenv('XDG_DATA_HOME')
  126. if xdgDataHome is not None:
  127. localeDir = os.path.join(xdgDataHome, 'locale')
  128. try:
  129. lang = gettext.translation('searx-qt', localedir=localeDir,
  130. languages=[Language[0]])
  131. except FileNotFoundError:
  132. pass
  133. if lang:
  134. lang.install()
  135. _ = lang.gettext
  136. else:
  137. print("Warning! Locale file (.mo) for language `{0}` not found!"
  138. .format(Language[0]))