From 85bd2779a3aafa92a8141f911fdc2dfadc12d26d Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 26 Aug 2014 11:34:21 -0300 Subject: [PATCH] gettext: fix string extraction and runtime usage --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/controllers/my_profile/cms_controller.rb | 2 +- app/helpers/application_helper.rb | 4 ++-- app/models/article.rb | 7 +++++-- app/models/change_password.rb | 7 +++++-- app/models/user.rb | 7 +++++-- lib/noosfero/i18n.rb | 4 ++-- lib/tasks/gettext.rake | 45 ++++++++++++++++++++++++++++++--------------- 9 files changed, 55 insertions(+), 26 deletions(-) diff --git a/Gemfile b/Gemfile index dbf3824..9265979 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ gem 'nokogiri' gem 'rake', :require => false gem 'rest-client' gem 'exception_notification' +gem 'gettext', :require => false, :group => :development # FIXME list here all actual dependencies (i.e. the ones in debian/control), # with their GEM names (not the Debian package names) diff --git a/Gemfile.lock b/Gemfile.lock index a585c84..14dd52f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,8 @@ GEM activesupport (>= 3.0.4) fast_gettext (0.6.8) ffi (1.0.11) + gettext (2.2.1) + locale gherkin (2.4.21) json (>= 1.4.6) hike (1.2.1) @@ -74,6 +76,7 @@ GEM i18n (0.6.0) journey (1.0.3) json (1.7.3) + locale (2.0.5) mail (2.4.4) i18n (>= 0.4.0) mime-types (~> 1.16) @@ -172,6 +175,7 @@ DEPENDENCIES database_cleaner exception_notification fast_gettext + gettext hpricot mocha nokogiri diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 9748164..b641e84 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -227,7 +227,7 @@ class CmsController < MyProfileController @article = profile.articles.find(params[:id]) if request.post? @article.destroy - session[:notice] = _("\"#{@article.name}\" was removed.") + session[:notice] = _("\"%s\" was removed." % @article.name) referer = Rails.application.routes.recognize_path URI.parse(request.referer).path rescue nil if referer and referer[:controller] == 'cms' and referer[:action] != 'edit' redirect_to referer diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7b0c233..533bae0 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1281,9 +1281,9 @@ module ApplicationHelper def delete_article_message(article) if article.folder? - _("Are you sure that you want to remove the folder \"#{article.name}\"? Note that all the items inside it will also be removed!") + _("Are you sure that you want to remove the folder \"%s\"? Note that all the items inside it will also be removed!") % article.name else - _("Are you sure that you want to remove the item \"#{article.name}\"?") + _("Are you sure that you want to remove the item \"%s\"?") % article.name end end diff --git a/app/models/article.rb b/app/models/article.rb index 49ce5f4..5584325 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -157,14 +157,17 @@ class Article < ActiveRecord::Base self.profile end - def self.human_attribute_name(attrib, options = {}) + def self.human_attribute_name_with_customization(attrib, options={}) case attrib.to_sym when :name _('Title') else - _(self.superclass.human_attribute_name(attrib)) + _(self.human_attribute_name_without_customization(attrib)) end end + class << self + alias_method_chain :human_attribute_name, :customization + end def css_class_list [self.class.name.to_css_class] diff --git a/app/models/change_password.rb b/app/models/change_password.rb index 6209372..f3c7a9a 100644 --- a/app/models/change_password.rb +++ b/app/models/change_password.rb @@ -2,16 +2,19 @@ class ChangePassword < Task attr_accessor :password, :password_confirmation - def self.human_attribute_name(attrib, options = {}) + def self.human_attribute_name_with_customization(attrib, options={}) case attrib.to_sym when :password _('Password') when :password_confirmation _('Password Confirmation') else - _(self.superclass.human_attribute_name(attrib)) + _(self.human_attribute_name_without_customization(attrib)) end end + class << self + alias_method_chain :human_attribute_name, :customization + end validates_presence_of :requestor diff --git a/app/models/user.rb b/app/models/user.rb index 8c7a690..4f58793 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,15 +16,18 @@ class User < ActiveRecord::Base end # FIXME ugly workaround - def self.human_attribute_name(attrib, options={}) + def self.human_attribute_name_with_customization(attrib, options={}) case attrib.to_sym when :login return [_('Username'), _('Email')].join(' / ') when :email return _('e-Mail') - else _(self.superclass.human_attribute_name(attrib)) + else _(self.human_attribute_name_without_customization(attrib)) end end + class << self + alias_method_chain :human_attribute_name, :customization + end before_create do |user| if user.environment.nil? diff --git a/lib/noosfero/i18n.rb b/lib/noosfero/i18n.rb index 50227bc..46ac9c2 100644 --- a/lib/noosfero/i18n.rb +++ b/lib/noosfero/i18n.rb @@ -20,5 +20,5 @@ if File.exists?(locale_dir) repos << FastGettext::TranslationRepository.build('iso_3166', :type => 'mo', :path => locale_dir) end -FastGettext.add_text_domain 'noosferofull', :type => :chain, :chain => repos -FastGettext.default_text_domain = 'noosferofull' +FastGettext.add_text_domain 'noosfero', :type => :chain, :chain => repos +FastGettext.default_text_domain = 'noosfero' diff --git a/lib/tasks/gettext.rake b/lib/tasks/gettext.rake index 21621ff..7006ccf 100644 --- a/lib/tasks/gettext.rake +++ b/lib/tasks/gettext.rake @@ -6,8 +6,16 @@ makemo_stamp = 'tmp/makemo.stamp' desc "Create mo-files for L10n" task :makemo => makemo_stamp file makemo_stamp => Dir.glob('po/*/noosfero.po') do - ruby '-I. -rconfig/boot -e \'require "gettext"; require "gettext/utils"; GetText.create_mofiles(true, "po", "locale")\'' Rake::Task['symlinkmo'].invoke + + require 'gettext' + require 'gettext/tools' + GetText.create_mofiles( + verbose: true, + po_root: 'po', + mo_root: 'locale', + ) + FileUtils.mkdir_p 'tmp' FileUtils.touch makemo_stamp end @@ -22,7 +30,7 @@ task :symlinkmo do 'pt' => 'pt_BR', } mkdir_p(Rails.root.join('locale')) - Dir.glob(Rails.root.join('locale/*')).each do |dir| + Dir.glob(Rails.root.join('po/*/')).each do |dir| lang = File.basename(dir) orig_lang = langmap[lang] || lang mkdir_p(Rails.root.join('locale', "#{lang}", 'LC_MESSAGES')) @@ -38,21 +46,28 @@ end desc "Update pot/po files to match new version." task :updatepo do - require 'gettext_rails/tools' - require_dependency 'noosfero' - - GetText::RubyParser::ID << '__' - GetText::RubyParser::PLURAL_ID << 'n__' - GetText::ActiveRecordParser.init(:use_classname => false) puts 'Extracting strings from source. This may take a while ...' - sources = - Dir.glob("{app,lib}/**/*.{rb,rhtml,erb}") + - Dir.glob('config/initializers/*.rb') + - Dir.glob('public/*.html.erb') + - Dir.glob('public/designs/themes/{base,noosfero,profile-base}/*.{rhtml,html.erb}') + - Dir.glob('plugins/**/{controllers,models,lib,views}/**/*.{rhtml,html.erb,rb}') - GetText.update_pofiles(Noosfero::PROJECT, sources, "#{Noosfero::PROJECT} #{Noosfero::VERSION}") + + files_to_translate = [ + "{app,lib}/**/*.{rb,rhtml,erb}", + 'config/initializers/*.rb', + 'public/*.html.erb', + 'public/designs/themes/{base,noosfero,profile-base}/*.{rhtml,html.erb}', + 'plugins/**/{controllers,models,lib,views}/**/*.{rhtml,html.erb,rb}', + ].map { |pattern| Dir.glob(pattern) }.flatten + + require 'gettext' + require 'gettext/tools' + GetText.update_pofiles( + 'noosfero', + files_to_translate, + Noosfero::VERSION, + { + po_root: 'po', + } + ) + end task :checkpo do -- libgit2 0.21.2