Commit 00f42579723e8fd5e3debe3a9fe9eb1def8d7da7
1 parent
26db3c33
Exists in
master
and in
22 other branches
Remove I18N local hacks
Internationalization of the ActiveRecord error messages do not work yet.
Showing
3 changed files
with
3 additions
and
161 deletions
Show diff stats
app/controllers/application_controller.rb
| @@ -46,7 +46,7 @@ class ApplicationController < ActionController::Base | @@ -46,7 +46,7 @@ class ApplicationController < ActionController::Base | ||
| 46 | def set_locale | 46 | def set_locale |
| 47 | FastGettext.available_locales = Noosfero.available_locales | 47 | FastGettext.available_locales = Noosfero.available_locales |
| 48 | FastGettext.default_locale = Noosfero.default_locale | 48 | FastGettext.default_locale = Noosfero.default_locale |
| 49 | - FastGettext.set_locale(params[:lang] || session[:lang] || Noosfero.default_locale || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en') | 49 | + I18n.locale = FastGettext.locale = (params[:lang] || session[:lang] || Noosfero.default_locale || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en') |
| 50 | if params[:lang] | 50 | if params[:lang] |
| 51 | session[:lang] = params[:lang] | 51 | session[:lang] = params[:lang] |
| 52 | end | 52 | end |
lib/noosfero/i18n.rb
| @@ -6,153 +6,6 @@ class Object | @@ -6,153 +6,6 @@ class Object | ||
| 6 | alias :ngettext :n_ | 6 | alias :ngettext :n_ |
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | -class ActiveRecord::Errors | ||
| 10 | - default_error_messages.update( | ||
| 11 | - :inclusion => "%{fn} is not included in the list", | ||
| 12 | - :exclusion => "%{fn} is reserved", | ||
| 13 | - :invalid => "%{fn} is invalid", | ||
| 14 | - :confirmation => "%{fn} doesn't match confirmation", | ||
| 15 | - :accepted => "%{fn} must be accepted", | ||
| 16 | - :empty => "%{fn} can't be empty", | ||
| 17 | - :blank => "%{fn} can't be blank", | ||
| 18 | - :too_long => "%{fn} is too long (maximum is %d characters)", | ||
| 19 | - :too_short => "%{fn} is too short (minimum is %d characters)", | ||
| 20 | - :wrong_length => "%{fn} is the wrong length (should be %d characters)", | ||
| 21 | - :taken => "%{fn} has already been taken", | ||
| 22 | - :not_a_number => "%{fn} is not a number" | ||
| 23 | - ) | ||
| 24 | - | ||
| 25 | - def localize_error_messages | ||
| 26 | - errors = {} | ||
| 27 | - each do |attr,msg| | ||
| 28 | - next if msg.nil? | ||
| 29 | - errors[attr] ||= [] | ||
| 30 | - errors[attr] << _(msg).sub('%{fn}', @base.class.human_attribute_name(attr)) | ||
| 31 | - end | ||
| 32 | - errors | ||
| 33 | - end | ||
| 34 | - def on_with_gettext(attribute) | ||
| 35 | - errors = localize_error_messages[attribute.to_s] | ||
| 36 | - return nil if errors.nil? | ||
| 37 | - errors.size == 1 ? errors.first : errors | ||
| 38 | - end | ||
| 39 | - alias_method_chain :on, :gettext | ||
| 40 | - | ||
| 41 | - def full_messages_with_gettext | ||
| 42 | - full_messages = [] | ||
| 43 | - errors = localize_error_messages | ||
| 44 | - errors.each_key do |attr| | ||
| 45 | - errors[attr].each do |msg| | ||
| 46 | - next if msg.nil? | ||
| 47 | - full_messages << msg | ||
| 48 | - end | ||
| 49 | - end | ||
| 50 | - full_messages | ||
| 51 | - end | ||
| 52 | - alias_method_chain :full_messages, :gettext | ||
| 53 | -end | ||
| 54 | - | ||
| 55 | - | ||
| 56 | -module ActionView::Helpers::ActiveRecordHelper | ||
| 57 | - module L10n | ||
| 58 | - @error_message_title = ["%{num} error prohibited this %{record} from being saved", "%{num} errors prohibited this %{record} from being saved"] | ||
| 59 | - @error_message_explanation = ["There was a problem with the following field:", "There were problems with the following fields:"] | ||
| 60 | - module_function | ||
| 61 | - def error_messages_for(instance, objects, object_names, count, options) | ||
| 62 | - record = _(options[:object_name] || object_names[0].to_s) | ||
| 63 | - | ||
| 64 | - html = {} | ||
| 65 | - [:id, :class].each do |key| | ||
| 66 | - if options.include?(key) | ||
| 67 | - value = options[key] | ||
| 68 | - html[key] = value unless value.blank? | ||
| 69 | - else | ||
| 70 | - html[key] = 'errorExplanation' | ||
| 71 | - end | ||
| 72 | - end | ||
| 73 | - | ||
| 74 | - if options[:message_title] | ||
| 75 | - header_message = instance.error_message(options[:message_title], count) % {:num => count, :record => record} | ||
| 76 | - else | ||
| 77 | - header_message = ((count == 1) ? _(@error_message_title[0]) : _(@error_message_title[1])) % {:num => count, :record => record} | ||
| 78 | - end | ||
| 79 | - if options[:message_explanation] | ||
| 80 | - message_explanation = instance.error_message(options[:message_explanation], count) % {:num => count} | ||
| 81 | - else | ||
| 82 | - message_explanation = (count == 1 ? _(@error_message_explanation[0]) : _(@error_message_explanation[1])) % {:num => count} | ||
| 83 | - end | ||
| 84 | - | ||
| 85 | - error_messages = objects.map {|object| object.errors.full_messages.map {|msg| instance.content_tag(:li, msg) } } | ||
| 86 | - | ||
| 87 | - instance.content_tag( | ||
| 88 | - :div, | ||
| 89 | - instance.content_tag(options[:header_tag] || :h2, header_message) << | ||
| 90 | - instance.content_tag(:p, message_explanation) << | ||
| 91 | - instance.content_tag(:ul, error_messages), | ||
| 92 | - html | ||
| 93 | - ) | ||
| 94 | - end | ||
| 95 | - end | ||
| 96 | - | ||
| 97 | - alias error_messages_for_without_localize error_messages_for #:nodoc: | ||
| 98 | - | ||
| 99 | - # error_messages_for overrides original method with localization. | ||
| 100 | - # And also it extends to be able to replace the title/explanation of the header of the error dialog. (Since 1.90) | ||
| 101 | - # If you want to override these messages in the whole application, | ||
| 102 | - # use ActionView::Helpers::ActiveRecordHelper::L10n.set_error_message_(title|explanation) instead. | ||
| 103 | - # * :message_title - the title of message. Use Nn_() to path the strings for singular/plural. | ||
| 104 | - # e.g. Nn_("%{num} error prohibited this %{record} from being saved", | ||
| 105 | - # "%{num} errors prohibited this %{record} from being saved") | ||
| 106 | - # * :message_explanation - the explanation of message | ||
| 107 | - # e.g. Nn_("There was a problem with the following field:", | ||
| 108 | - # "There were %{num} problems with the following fields:") | ||
| 109 | - def error_messages_for(*params) | ||
| 110 | - options = params.last.is_a?(Hash) ? params.pop.symbolize_keys : {} | ||
| 111 | - objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact | ||
| 112 | - object_names = params.dup | ||
| 113 | - count = objects.inject(0) {|sum, object| sum + object.errors.count } | ||
| 114 | - if count.zero? | ||
| 115 | - '' | ||
| 116 | - else | ||
| 117 | - L10n.error_messages_for(self, objects, object_names, count, options) | ||
| 118 | - end | ||
| 119 | - end | ||
| 120 | - | ||
| 121 | -end | ||
| 122 | - | ||
| 123 | -module ActionController::Caching::Fragments | ||
| 124 | - def fragment_cache_key_with_fast_gettext(name) | ||
| 125 | - ret = fragment_cache_key_without_fast_gettext(name) | ||
| 126 | - if ret.is_a? String | ||
| 127 | - ret.gsub(/:/, ".") << "_#{FastGettext.locale}" | ||
| 128 | - else | ||
| 129 | - ret | ||
| 130 | - end | ||
| 131 | - end | ||
| 132 | - alias_method_chain :fragment_cache_key, :fast_gettext | ||
| 133 | - | ||
| 134 | - def expire_fragment_with_fast_gettext(name, options = nil) | ||
| 135 | - return unless perform_caching | ||
| 136 | - | ||
| 137 | - key = fragment_cache_key_without_fast_gettext(name) | ||
| 138 | - if key.is_a?(Regexp) | ||
| 139 | - self.class.benchmark "Expired fragments matching: #{key.source}" do | ||
| 140 | - cache_store.delete_matched(key, options) | ||
| 141 | - end | ||
| 142 | - else | ||
| 143 | - key = key.gsub(/:/, ".") | ||
| 144 | - self.class.benchmark "Expired fragment: #{key}, lang = #{FastGettext.available_locales.inspect}" do | ||
| 145 | - if FastGettext.available_locales | ||
| 146 | - FastGettext.available_locales.each do |lang| | ||
| 147 | - cache_store.delete("#{key}_#{lang}", options) | ||
| 148 | - end | ||
| 149 | - end | ||
| 150 | - end | ||
| 151 | - end | ||
| 152 | - end | ||
| 153 | - alias_method_chain :expire_fragment, :fast_gettext | ||
| 154 | -end | ||
| 155 | - | ||
| 156 | # translations in place? | 9 | # translations in place? |
| 157 | locale_dir = Rails.root.join('locale') | 10 | locale_dir = Rails.root.join('locale') |
| 158 | if File.exists?(locale_dir) | 11 | if File.exists?(locale_dir) |
lib/tasks/gettext.rake
| @@ -39,23 +39,12 @@ end | @@ -39,23 +39,12 @@ end | ||
| 39 | 39 | ||
| 40 | desc "Update pot/po files to match new version." | 40 | desc "Update pot/po files to match new version." |
| 41 | task :updatepo do | 41 | task :updatepo do |
| 42 | - require 'gettext' | ||
| 43 | - require 'gettext/rails' | ||
| 44 | - require 'gettext/utils' | 42 | + require 'gettext_rails/tools' |
| 45 | GetText::RubyParser::ID << '__' | 43 | GetText::RubyParser::ID << '__' |
| 46 | GetText::RubyParser::PLURAL_ID << 'n__' | 44 | GetText::RubyParser::PLURAL_ID << 'n__' |
| 47 | GetText::ActiveRecordParser.init(:use_classname => false) | 45 | GetText::ActiveRecordParser.init(:use_classname => false) |
| 48 | 46 | ||
| 49 | - module GetText | ||
| 50 | - module_function | ||
| 51 | - def update_pofiles(textdomain, files, app_version, po_root = "po", refpot = "tmp.pot") | ||
| 52 | - rgettext(files, refpot) | ||
| 53 | - system("mv tmp.pot tmp2.pot; msguniq -o tmp.pot tmp2.pot; rm -f tmp2.pot") | ||
| 54 | - msgmerge_all(textdomain, app_version, po_root, refpot) | ||
| 55 | - File.delete(refpot) | ||
| 56 | - end | ||
| 57 | - end | ||
| 58 | - | 47 | + puts 'Extracting strings from source. This may take a while ...' |
| 59 | sources = | 48 | sources = |
| 60 | Dir.glob("{app,lib}/**/*.{rb,rhtml,erb}") + | 49 | Dir.glob("{app,lib}/**/*.{rb,rhtml,erb}") + |
| 61 | Dir.glob('config/initializers/*.rb') + | 50 | Dir.glob('config/initializers/*.rb') + |