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.

i18n.rb 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. def self.included(base)
  22. base.extend Redmine::I18n
  23. end
  24. def l(*args)
  25. case args.size
  26. when 1
  27. ::I18n.t(*args)
  28. when 2
  29. if args.last.is_a?(Hash)
  30. ::I18n.t(*args.first, **args.last)
  31. elsif args.last.is_a?(String)
  32. ::I18n.t(args.first, :value => args.last)
  33. else
  34. ::I18n.t(args.first, :count => args.last)
  35. end
  36. else
  37. raise "Translation string with multiple values: #{args.first}"
  38. end
  39. end
  40. def l_or_humanize(s, options={})
  41. k = "#{options[:prefix]}#{s}".to_sym
  42. ::I18n.t(k, :default => s.to_s.humanize)
  43. end
  44. def l_hours(hours)
  45. hours = hours.to_f
  46. l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => format_hours(hours))
  47. end
  48. def l_hours_short(hours)
  49. l(:label_f_hour_short, :value => format_hours(hours.to_f))
  50. end
  51. def ll(lang, str, arg=nil)
  52. options = arg.is_a?(Hash) ? arg : {:value => arg}
  53. locale = lang.to_s.gsub(%r{(.+)\-(.+)$}) {"#{$1}-#{$2.upcase}"}
  54. ::I18n.t(str.to_s, **options, locale: locale)
  55. end
  56. # Localizes the given args with user's language
  57. def lu(user, *args)
  58. lang = user.try(:language).presence || Setting.default_language
  59. ll(lang, *args)
  60. end
  61. def format_date(date)
  62. return nil unless date
  63. options = {}
  64. options[:format] = Setting.date_format unless Setting.date_format.blank?
  65. ::I18n.l(date.to_date, **options)
  66. end
  67. def format_time(time, include_date=true, user=nil)
  68. return nil unless time
  69. user ||= User.current
  70. options = {}
  71. options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
  72. time = time.to_time if time.is_a?(String)
  73. local = user.convert_time_to_user_timezone(time)
  74. (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, **options)
  75. end
  76. def format_hours(hours)
  77. return "" if hours.blank?
  78. if Setting.timespan_format == 'minutes'
  79. h = hours.floor
  80. m = ((hours - h) * 60).round
  81. "%d:%02d" % [h, m]
  82. else
  83. "%.2f" % hours.to_f
  84. end
  85. end
  86. def day_name(day)
  87. ::I18n.t('date.day_names')[day % 7]
  88. end
  89. def day_letter(day)
  90. ::I18n.t('date.abbr_day_names')[day % 7].first
  91. end
  92. def month_name(month)
  93. ::I18n.t('date.month_names')[month]
  94. end
  95. def valid_languages
  96. ::I18n.available_locales
  97. end
  98. # Returns an array of languages names and code sorted by names, example:
  99. # [["Deutsch", "de"], ["English", "en"] ...]
  100. #
  101. # The result is cached to prevent from loading all translations files
  102. # unless :cache => false option is given
  103. def languages_options(options={})
  104. options =
  105. if options[:cache] == false
  106. available_locales = ::I18n.backend.available_locales
  107. valid_languages.
  108. select {|locale| available_locales.include?(locale)}.
  109. map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.
  110. sort_by(&:first)
  111. else
  112. ActionController::Base.cache_store.fetch "i18n/languages_options/#{Redmine::VERSION}" do
  113. languages_options :cache => false
  114. end
  115. end
  116. options.map {|name, lang| [name.force_encoding("UTF-8"), lang.force_encoding("UTF-8")]}
  117. end
  118. def find_language(lang)
  119. @@languages_lookup ||=
  120. valid_languages.inject({}) do |k, v|
  121. k[v.to_s.downcase] = v
  122. k
  123. end
  124. @@languages_lookup[lang.to_s.downcase]
  125. end
  126. def set_language_if_valid(lang)
  127. if l = find_language(lang)
  128. ::I18n.locale = l
  129. end
  130. end
  131. def current_language
  132. ::I18n.locale
  133. end
  134. # Custom backend based on I18n::Backend::Simple with the following changes:
  135. # * available_locales are determined by looking at translation file names
  136. class Backend < ::I18n::Backend::Simple
  137. module Implementation
  138. # Get available locales from the translations filenames
  139. def available_locales
  140. @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
  141. end
  142. end
  143. # Adds custom pluralization rules
  144. include ::I18n::Backend::Pluralization
  145. # Adds fallback to default locale for untranslated strings
  146. include ::I18n::Backend::Fallbacks
  147. end
  148. end
  149. end