You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'redmine'
  19. module Redmine
  20. module I18n
  21. include ActionView::Helpers::NumberHelper
  22. def self.included(base)
  23. base.extend Redmine::I18n
  24. end
  25. def l(*args)
  26. case args.size
  27. when 1
  28. ::I18n.t(*args)
  29. when 2
  30. if args.last.is_a?(Hash)
  31. ::I18n.t(*args.first, **args.last)
  32. elsif args.last.is_a?(String)
  33. ::I18n.t(args.first, :value => args.last)
  34. else
  35. ::I18n.t(args.first, :count => args.last)
  36. end
  37. else
  38. raise "Translation string with multiple values: #{args.first}"
  39. end
  40. end
  41. def l_or_humanize(s, options={})
  42. k = :"#{options[:prefix]}#{s}"
  43. ::I18n.t(k, :default => s.to_s.humanize)
  44. end
  45. def l_hours(hours)
  46. hours = hours.to_f
  47. l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => format_hours(hours))
  48. end
  49. def l_hours_short(hours)
  50. l(:label_f_hour_short, :value => format_hours(hours.to_f))
  51. end
  52. def ll(lang, str, arg=nil)
  53. options = arg.is_a?(Hash) ? arg : {:value => arg}
  54. locale = lang.to_s.gsub(%r{(.+)\-(.+)$}) {"#{$1}-#{$2.upcase}"}
  55. ::I18n.t(str.to_s, **options, locale: locale)
  56. end
  57. # Localizes the given args with user's language
  58. def lu(user, *args)
  59. lang = user.try(:language).presence || Setting.default_language
  60. ll(lang, *args)
  61. end
  62. def format_date(date)
  63. return nil unless date
  64. options = {}
  65. options[:format] = Setting.date_format unless Setting.date_format.blank?
  66. ::I18n.l(date.to_date, **options)
  67. end
  68. def format_time(time, include_date=true, user=nil)
  69. return nil unless time
  70. user ||= User.current
  71. options = {}
  72. options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
  73. time = time.to_time if time.is_a?(String)
  74. local = user.convert_time_to_user_timezone(time)
  75. (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, **options)
  76. end
  77. def format_hours(hours)
  78. return "" if hours.blank?
  79. if Setting.timespan_format == 'minutes'
  80. h = hours.floor
  81. m = ((hours - h) * 60).round
  82. "%d:%02d" % [h, m]
  83. else
  84. number_with_delimiter(sprintf('%.2f', hours.to_f), delimiter: nil)
  85. end
  86. end
  87. # Will consider language specific separator in user input
  88. # and normalize them to a unified format to be accepted by Kernel.Float().
  89. #
  90. # @param value [String] A string represenation of a float value.
  91. #
  92. # @note The delimiter cannot be used here if it is a decimal point since it
  93. # will clash with the dot separator.
  94. def normalize_float(value)
  95. separator = ::I18n.t('number.format.separator')
  96. value.gsub(/[#{separator}]/, separator => '.')
  97. end
  98. def day_name(day)
  99. ::I18n.t('date.day_names')[day % 7]
  100. end
  101. def abbr_day_name(day)
  102. ::I18n.t('date.abbr_day_names')[day % 7]
  103. end
  104. def day_letter(day)
  105. ::I18n.t('date.abbr_day_names')[day % 7].first
  106. end
  107. def month_name(month)
  108. ::I18n.t('date.month_names')[month]
  109. end
  110. def valid_languages
  111. ::I18n.available_locales
  112. end
  113. # Returns an array of languages names and code sorted by names, example:
  114. # [["Deutsch", "de"], ["English", "en"] ...]
  115. #
  116. # The result is cached to prevent from loading all translations files
  117. # unless :cache => false option is given
  118. def languages_options(options={})
  119. options =
  120. if options[:cache] == false
  121. available_locales = ::I18n.backend.available_locales
  122. valid_languages.
  123. select {|locale| available_locales.include?(locale)}.
  124. map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.
  125. sort_by(&:first)
  126. else
  127. ActionController::Base.cache_store.fetch "i18n/languages_options/#{Redmine::VERSION}" do
  128. languages_options :cache => false
  129. end
  130. end
  131. options.map {|name, lang| [name.force_encoding("UTF-8"), lang.force_encoding("UTF-8")]}
  132. end
  133. def find_language(lang)
  134. @@languages_lookup ||=
  135. valid_languages.inject({}) do |k, v|
  136. k[v.to_s.downcase] = v
  137. k
  138. end
  139. @@languages_lookup[lang.to_s.downcase]
  140. end
  141. def set_language_if_valid(lang)
  142. if l = find_language(lang)
  143. ::I18n.locale = l
  144. end
  145. end
  146. def current_language
  147. ::I18n.locale
  148. end
  149. # Custom backend based on I18n::Backend::Simple with the following changes:
  150. # * available_locales are determined by looking at translation file names
  151. class Backend < ::I18n::Backend::Simple
  152. module Implementation
  153. # Get available locales from the translations filenames
  154. def available_locales
  155. @available_locales ||= begin
  156. redmine_locales = Dir[Rails.root / 'config' / 'locales' / '*.yml'].map { |f| File.basename(f, '.yml').to_sym }
  157. super & redmine_locales
  158. end
  159. end
  160. end
  161. # Adds custom pluralization rules
  162. include ::I18n::Backend::Pluralization
  163. # Adds fallback to default locale for untranslated strings
  164. include ::I18n::Backend::Fallbacks
  165. end
  166. end
  167. end