File: lib/redmine/i18n.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Redmine#1
  module: I18n#2
has properties
module method: included / 1 #3
method: l / 1 #7
method: l_or_humanize / 2 #24
method: l_hours / 1 #29
method: ll / 3 #34
method: format_date / 1 #38
method: format_time / 2 #43
method: day_name / 1 #52
method: month_name / 1 #56
method: valid_languages #60
method: find_language / 1 #64
method: set_language_if_valid / 1 #69
method: current_language #75

Code

   1  module Redmine
   2    module I18n
   3      def self.included(base)
   4        base.extend Redmine::I18n
   5      end
   6 
   7      def l(*args)
   8        case args.size
   9        when 1
  10          ::I18n.t(*args)
  11        when 2
  12          if args.last.is_a?(Hash)
  13            ::I18n.t(*args)
  14          elsif args.last.is_a?(String)
  15            ::I18n.t(args.first, :value => args.last)
  16          else
  17            ::I18n.t(args.first, :count => args.last)
  18          end
  19        else
  20          raise "Translation string with multiple values: #{args.first}"
  21        end
  22      end
  23 
  24      def l_or_humanize(s, options={})
  25        k = "#{options[:prefix]}#{s}".to_sym
  26        ::I18n.t(k, :default => s.to_s.humanize)
  27      end
  28 
  29      def l_hours(hours)
  30        hours = hours.to_f
  31        l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
  32      end
  33 
  34      def ll(lang, str, value=nil)
  35        ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
  36      end
  37 
  38      def format_date(date)
  39        return nil unless date
  40        Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
  41      end
  42 
  43      def format_time(time, include_date = true)
  44        return nil unless time
  45        time = time.to_time if time.is_a?(String)
  46        zone = User.current.time_zone
  47        local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
  48        (include_date ? "#{format_date(local)} " : "") +
  49          (Setting.time_format.blank? ? ::I18n.l(local, :format => :time) : local.strftime(Setting.time_format))
  50      end
  51 
  52      def day_name(day)
  53        ::I18n.t('date.day_names')[day % 7]
  54      end
  55 
  56      def month_name(month)
  57        ::I18n.t('date.month_names')[month]
  58      end
  59 
  60      def valid_languages
  61        @@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym)
  62      end
  63 
  64      def find_language(lang)
  65        @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
  66        @@languages_lookup[lang.to_s.downcase]
  67      end
  68 
  69      def set_language_if_valid(lang)
  70        if l = find_language(lang)
  71          ::I18n.locale = l
  72        end
  73      end
  74 
  75      def current_language
  76        ::I18n.locale
  77      end
  78    end
  79  end