diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-02-21 11:04:50 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-02-21 11:04:50 +0000 |
commit | fe28193e4eb9af2dc5262535a29ffde5249568fc (patch) | |
tree | bd4cf3a9fbada98e58e510ca0e25c42bf00676a7 /lib | |
parent | 9a986ac0a51fe844eee816325e6a6d4122136d9a (diff) | |
download | redmine-fe28193e4eb9af2dc5262535a29ffde5249568fc.tar.gz redmine-fe28193e4eb9af2dc5262535a29ffde5249568fc.zip |
Merged Rails 2.2 branch. Redmine now requires Rails 2.2.2.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2493 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r-- | lib/redmine/default_data/loader.rb | 2 | ||||
-rw-r--r-- | lib/redmine/export/pdf.rb | 3 | ||||
-rw-r--r-- | lib/redmine/helpers/calendar.rb | 4 | ||||
-rw-r--r-- | lib/redmine/hook.rb | 2 | ||||
-rw-r--r-- | lib/redmine/i18n.rb | 79 | ||||
-rw-r--r-- | lib/redmine/menu_manager.rb | 11 | ||||
-rw-r--r-- | lib/redmine/plugin.rb | 3 | ||||
-rw-r--r-- | lib/tabular_form_builder.rb | 2 | ||||
-rw-r--r-- | lib/tasks/initializers.rake | 24 | ||||
-rw-r--r-- | lib/tasks/load_default_data.rake | 6 |
10 files changed, 120 insertions, 16 deletions
diff --git a/lib/redmine/default_data/loader.rb b/lib/redmine/default_data/loader.rb index b7cab56ca..a923b5783 100644 --- a/lib/redmine/default_data/loader.rb +++ b/lib/redmine/default_data/loader.rb @@ -20,7 +20,7 @@ module Redmine class DataAlreadyLoaded < Exception; end module Loader - include GLoc + include Redmine::I18n class << self # Returns true if no data is already loaded in the database diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb index 5918b6d37..6629e1c32 100644 --- a/lib/redmine/export/pdf.rb +++ b/lib/redmine/export/pdf.rb @@ -22,10 +22,11 @@ require 'rfpdf/chinese' module Redmine module Export module PDF + include ActionView::Helpers::TextHelper include ActionView::Helpers::NumberHelper class IFPDF < FPDF - include GLoc + include Redmine::I18n attr_accessor :footer_date def initialize(lang) diff --git a/lib/redmine/helpers/calendar.rb b/lib/redmine/helpers/calendar.rb index 347f1c5b5..3e703ea6d 100644 --- a/lib/redmine/helpers/calendar.rb +++ b/lib/redmine/helpers/calendar.rb @@ -20,7 +20,7 @@ module Redmine # Simple class to compute the start and end dates of a calendar class Calendar - include GLoc + include Redmine::I18n attr_reader :startdt, :enddt def initialize(date, lang = current_language, period = :month) @@ -28,7 +28,7 @@ module Redmine @events = [] @ending_events_by_days = {} @starting_events_by_days = {} - set_language lang + set_language_if_valid lang case period when :month @startdt = Date.civil(date.year, date.month, 1) diff --git a/lib/redmine/hook.rb b/lib/redmine/hook.rb index 63d7a5961..2dedb78e7 100644 --- a/lib/redmine/hook.rb +++ b/lib/redmine/hook.rb @@ -70,7 +70,7 @@ module Redmine # Base class for hook listeners. class Listener include Singleton - include GLoc + include Redmine::I18n # Registers the listener def self.inherited(child) diff --git a/lib/redmine/i18n.rb b/lib/redmine/i18n.rb new file mode 100644 index 000000000..1bfca81e4 --- /dev/null +++ b/lib/redmine/i18n.rb @@ -0,0 +1,79 @@ +module Redmine + module I18n + def self.included(base) + base.extend Redmine::I18n + end + + def l(*args) + case args.size + when 1 + ::I18n.t(*args) + when 2 + if args.last.is_a?(Hash) + ::I18n.t(*args) + elsif args.last.is_a?(String) + ::I18n.t(args.first, :value => args.last) + else + ::I18n.t(args.first, :count => args.last) + end + else + raise "Translation string with multiple values: #{args.first}" + end + end + + def l_or_humanize(s, options={}) + k = "#{options[:prefix]}#{s}".to_sym + ::I18n.t(k, :default => s.to_s.humanize) + end + + def l_hours(hours) + hours = hours.to_f + l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f)) + end + + def ll(lang, str, value=nil) + ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }) + end + + def format_date(date) + return nil unless date + Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format) + end + + def format_time(time, include_date = true) + return nil unless time + time = time.to_time if time.is_a?(String) + zone = User.current.time_zone + local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) + Setting.time_format.blank? ? ::I18n.l(local, :format => (include_date ? :default : :time)) : + ((include_date ? "#{format_date(time)} " : "") + "#{local.strftime(Setting.time_format)}") + end + + def day_name(day) + ::I18n.t('date.day_names')[day % 7] + end + + def month_name(month) + ::I18n.t('date.month_names')[month] + end + + def valid_languages + @@valid_languages ||= Dir.glob(File.join(RAILS_ROOT, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym) + end + + def find_language(lang) + @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } + @@languages_lookup[lang.to_s.downcase] + end + + def set_language_if_valid(lang) + if l = find_language(lang) + ::I18n.locale = l + end + end + + def current_language + ::I18n.locale + end + end +end diff --git a/lib/redmine/menu_manager.rb b/lib/redmine/menu_manager.rb index 7a89a32b9..353a1f3a8 100644 --- a/lib/redmine/menu_manager.rb +++ b/lib/redmine/menu_manager.rb @@ -15,8 +15,6 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -require 'gloc' - module Redmine module MenuManager module MenuController @@ -101,7 +99,7 @@ module Redmine item.url end caption = item.caption(project) - caption = l(caption) if caption.is_a?(Symbol) + caption = l_or_humanize(caption, :prefix => 'label_') if caption.is_a?(Symbol) if block_given? yield item, caption, url, (current_menu_item == item.name) else @@ -178,7 +176,7 @@ module Redmine end class MenuItem - include GLoc + include Redmine::I18n attr_reader :name, :url, :param, :condition def initialize(name, url, options) @@ -188,7 +186,7 @@ module Redmine @url = url @condition = options[:if] @param = options[:param] || :id - @caption = options[:caption] + @caption = options[:caption] || @name @html_options = options[:html] || {} # Adds a unique class to each menu item based on its name @html_options[:class] = [@html_options[:class], @name.to_s.dasherize].compact.join(' ') @@ -200,8 +198,7 @@ module Redmine c = @name.to_s.humanize if c.blank? c else - # check if localized string exists on first render (after GLoc strings are loaded) - @caption_key ||= (@caption || (l_has_string?("label_#{@name}".to_sym) ? "label_#{@name}".to_sym : @name.to_s.humanize)) + @caption end end diff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rb index 0fc6985f4..c04ba334c 100644 --- a/lib/redmine/plugin.rb +++ b/lib/redmine/plugin.rb @@ -67,6 +67,9 @@ module Redmine #:nodoc: p.instance_eval(&block) # Set a default name if it was not provided during registration p.name(id.to_s.humanize) if p.name.nil? + # Adds plugin locales if any + # YAML translation files should be found under <plugin>/config/locales/ + ::I18n.load_path += Dir.glob(File.join(RAILS_ROOT, 'vendor', 'plugins', id.to_s, 'config', 'locales', '*.yml')) registered_plugins[id] = p end diff --git a/lib/tabular_form_builder.rb b/lib/tabular_form_builder.rb index 3ca2d7aab..2dd2f2903 100644 --- a/lib/tabular_form_builder.rb +++ b/lib/tabular_form_builder.rb @@ -18,7 +18,7 @@ require 'action_view/helpers/form_helper' class TabularFormBuilder < ActionView::Helpers::FormBuilder - include GLoc + include Redmine::I18n def initialize(object_name, object, template, options, proc) set_language_if_valid options.delete(:lang) diff --git a/lib/tasks/initializers.rake b/lib/tasks/initializers.rake new file mode 100644 index 000000000..a81c4681a --- /dev/null +++ b/lib/tasks/initializers.rake @@ -0,0 +1,24 @@ +desc 'Generates a configuration file for cookie store sessions.' + +file 'config/initializers/session_store.rb' do + path = File.join(RAILS_ROOT, 'config', 'initializers', 'session_store.rb') + secret = ActiveSupport::SecureRandom.hex(40) + File.open(path, 'w') do |f| + f.write <<"EOF" +# This file was generated by 'rake config/initializers/session_store.rb', +# and should not be made visible to public. +# If you have a load-balancing Redmine cluster, you will need to use the +# same version of this file on each machine. And be sure to restart your +# server when you modify this file. + +# Your secret key for verifying cookie session data integrity. If you +# change this key, all old sessions will become invalid! Make sure the +# secret is at least 30 characters and all random, no regular words or +# you'll be exposed to dictionary attacks. +ActionController::Base.session = { + :session_key => '_redmine_session', + :secret => '#{secret}' +} +EOF + end +end diff --git a/lib/tasks/load_default_data.rake b/lib/tasks/load_default_data.rake index 6ddd1fb97..4179b9db3 100644 --- a/lib/tasks/load_default_data.rake +++ b/lib/tasks/load_default_data.rake @@ -2,14 +2,14 @@ desc 'Load Redmine default configuration data' namespace :redmine do
task :load_default_data => :environment do
- include GLoc
+ include Redmine::I18n
set_language_if_valid('en')
puts
while true
print "Select language: "
- print GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }.join(", ")
- print " [#{GLoc.current_language}] "
+ print valid_languages.collect(&:to_s).sort.join(", ")
+ print " [#{current_language}] "
lang = STDIN.gets.chomp!
break if lang.empty?
break if set_language_if_valid(lang)
|