Commit 85bd2779a3aafa92a8141f911fdc2dfadc12d26d
Committed by
Antonio Terceiro
1 parent
39d29854
Exists in
master
and in
27 other branches
gettext: fix string extraction and runtime usage
Closes #307 Signed-off-by: Victor Costa <vfcosta@gmail.com> Signed-off-by: Antonio Terceiro <terceiro@colivre.coop.br>
Showing
9 changed files
with
55 additions
and
26 deletions
Show diff stats
Gemfile
| ... | ... | @@ -17,6 +17,7 @@ gem 'nokogiri' |
| 17 | 17 | gem 'rake', :require => false |
| 18 | 18 | gem 'rest-client' |
| 19 | 19 | gem 'exception_notification' |
| 20 | +gem 'gettext', :require => false, :group => :development | |
| 20 | 21 | |
| 21 | 22 | # FIXME list here all actual dependencies (i.e. the ones in debian/control), |
| 22 | 23 | # with their GEM names (not the Debian package names) | ... | ... |
Gemfile.lock
| ... | ... | @@ -67,6 +67,8 @@ GEM |
| 67 | 67 | activesupport (>= 3.0.4) |
| 68 | 68 | fast_gettext (0.6.8) |
| 69 | 69 | ffi (1.0.11) |
| 70 | + gettext (2.2.1) | |
| 71 | + locale | |
| 70 | 72 | gherkin (2.4.21) |
| 71 | 73 | json (>= 1.4.6) |
| 72 | 74 | hike (1.2.1) |
| ... | ... | @@ -74,6 +76,7 @@ GEM |
| 74 | 76 | i18n (0.6.0) |
| 75 | 77 | journey (1.0.3) |
| 76 | 78 | json (1.7.3) |
| 79 | + locale (2.0.5) | |
| 77 | 80 | mail (2.4.4) |
| 78 | 81 | i18n (>= 0.4.0) |
| 79 | 82 | mime-types (~> 1.16) |
| ... | ... | @@ -172,6 +175,7 @@ DEPENDENCIES |
| 172 | 175 | database_cleaner |
| 173 | 176 | exception_notification |
| 174 | 177 | fast_gettext |
| 178 | + gettext | |
| 175 | 179 | hpricot |
| 176 | 180 | mocha |
| 177 | 181 | nokogiri | ... | ... |
app/controllers/my_profile/cms_controller.rb
| ... | ... | @@ -227,7 +227,7 @@ class CmsController < MyProfileController |
| 227 | 227 | @article = profile.articles.find(params[:id]) |
| 228 | 228 | if request.post? |
| 229 | 229 | @article.destroy |
| 230 | - session[:notice] = _("\"#{@article.name}\" was removed.") | |
| 230 | + session[:notice] = _("\"%s\" was removed." % @article.name) | |
| 231 | 231 | referer = Rails.application.routes.recognize_path URI.parse(request.referer).path rescue nil |
| 232 | 232 | if referer and referer[:controller] == 'cms' and referer[:action] != 'edit' |
| 233 | 233 | redirect_to referer | ... | ... |
app/helpers/application_helper.rb
| ... | ... | @@ -1281,9 +1281,9 @@ module ApplicationHelper |
| 1281 | 1281 | |
| 1282 | 1282 | def delete_article_message(article) |
| 1283 | 1283 | if article.folder? |
| 1284 | - _("Are you sure that you want to remove the folder \"#{article.name}\"? Note that all the items inside it will also be removed!") | |
| 1284 | + _("Are you sure that you want to remove the folder \"%s\"? Note that all the items inside it will also be removed!") % article.name | |
| 1285 | 1285 | else |
| 1286 | - _("Are you sure that you want to remove the item \"#{article.name}\"?") | |
| 1286 | + _("Are you sure that you want to remove the item \"%s\"?") % article.name | |
| 1287 | 1287 | end |
| 1288 | 1288 | end |
| 1289 | 1289 | ... | ... |
app/models/article.rb
| ... | ... | @@ -157,14 +157,17 @@ class Article < ActiveRecord::Base |
| 157 | 157 | self.profile |
| 158 | 158 | end |
| 159 | 159 | |
| 160 | - def self.human_attribute_name(attrib, options = {}) | |
| 160 | + def self.human_attribute_name_with_customization(attrib, options={}) | |
| 161 | 161 | case attrib.to_sym |
| 162 | 162 | when :name |
| 163 | 163 | _('Title') |
| 164 | 164 | else |
| 165 | - _(self.superclass.human_attribute_name(attrib)) | |
| 165 | + _(self.human_attribute_name_without_customization(attrib)) | |
| 166 | 166 | end |
| 167 | 167 | end |
| 168 | + class << self | |
| 169 | + alias_method_chain :human_attribute_name, :customization | |
| 170 | + end | |
| 168 | 171 | |
| 169 | 172 | def css_class_list |
| 170 | 173 | [self.class.name.to_css_class] | ... | ... |
app/models/change_password.rb
| ... | ... | @@ -2,16 +2,19 @@ class ChangePassword < Task |
| 2 | 2 | |
| 3 | 3 | attr_accessor :password, :password_confirmation |
| 4 | 4 | |
| 5 | - def self.human_attribute_name(attrib, options = {}) | |
| 5 | + def self.human_attribute_name_with_customization(attrib, options={}) | |
| 6 | 6 | case attrib.to_sym |
| 7 | 7 | when :password |
| 8 | 8 | _('Password') |
| 9 | 9 | when :password_confirmation |
| 10 | 10 | _('Password Confirmation') |
| 11 | 11 | else |
| 12 | - _(self.superclass.human_attribute_name(attrib)) | |
| 12 | + _(self.human_attribute_name_without_customization(attrib)) | |
| 13 | 13 | end |
| 14 | 14 | end |
| 15 | + class << self | |
| 16 | + alias_method_chain :human_attribute_name, :customization | |
| 17 | + end | |
| 15 | 18 | |
| 16 | 19 | validates_presence_of :requestor |
| 17 | 20 | ... | ... |
app/models/user.rb
| ... | ... | @@ -16,15 +16,18 @@ class User < ActiveRecord::Base |
| 16 | 16 | end |
| 17 | 17 | |
| 18 | 18 | # FIXME ugly workaround |
| 19 | - def self.human_attribute_name(attrib, options={}) | |
| 19 | + def self.human_attribute_name_with_customization(attrib, options={}) | |
| 20 | 20 | case attrib.to_sym |
| 21 | 21 | when :login |
| 22 | 22 | return [_('Username'), _('Email')].join(' / ') |
| 23 | 23 | when :email |
| 24 | 24 | return _('e-Mail') |
| 25 | - else _(self.superclass.human_attribute_name(attrib)) | |
| 25 | + else _(self.human_attribute_name_without_customization(attrib)) | |
| 26 | 26 | end |
| 27 | 27 | end |
| 28 | + class << self | |
| 29 | + alias_method_chain :human_attribute_name, :customization | |
| 30 | + end | |
| 28 | 31 | |
| 29 | 32 | before_create do |user| |
| 30 | 33 | if user.environment.nil? | ... | ... |
lib/noosfero/i18n.rb
| ... | ... | @@ -20,5 +20,5 @@ if File.exists?(locale_dir) |
| 20 | 20 | repos << FastGettext::TranslationRepository.build('iso_3166', :type => 'mo', :path => locale_dir) |
| 21 | 21 | end |
| 22 | 22 | |
| 23 | -FastGettext.add_text_domain 'noosferofull', :type => :chain, :chain => repos | |
| 24 | -FastGettext.default_text_domain = 'noosferofull' | |
| 23 | +FastGettext.add_text_domain 'noosfero', :type => :chain, :chain => repos | |
| 24 | +FastGettext.default_text_domain = 'noosfero' | ... | ... |
lib/tasks/gettext.rake
| ... | ... | @@ -6,8 +6,16 @@ makemo_stamp = 'tmp/makemo.stamp' |
| 6 | 6 | desc "Create mo-files for L10n" |
| 7 | 7 | task :makemo => makemo_stamp |
| 8 | 8 | file makemo_stamp => Dir.glob('po/*/noosfero.po') do |
| 9 | - ruby '-I. -rconfig/boot -e \'require "gettext"; require "gettext/utils"; GetText.create_mofiles(true, "po", "locale")\'' | |
| 10 | 9 | Rake::Task['symlinkmo'].invoke |
| 10 | + | |
| 11 | + require 'gettext' | |
| 12 | + require 'gettext/tools' | |
| 13 | + GetText.create_mofiles( | |
| 14 | + verbose: true, | |
| 15 | + po_root: 'po', | |
| 16 | + mo_root: 'locale', | |
| 17 | + ) | |
| 18 | + | |
| 11 | 19 | FileUtils.mkdir_p 'tmp' |
| 12 | 20 | FileUtils.touch makemo_stamp |
| 13 | 21 | end |
| ... | ... | @@ -22,7 +30,7 @@ task :symlinkmo do |
| 22 | 30 | 'pt' => 'pt_BR', |
| 23 | 31 | } |
| 24 | 32 | mkdir_p(Rails.root.join('locale')) |
| 25 | - Dir.glob(Rails.root.join('locale/*')).each do |dir| | |
| 33 | + Dir.glob(Rails.root.join('po/*/')).each do |dir| | |
| 26 | 34 | lang = File.basename(dir) |
| 27 | 35 | orig_lang = langmap[lang] || lang |
| 28 | 36 | mkdir_p(Rails.root.join('locale', "#{lang}", 'LC_MESSAGES')) |
| ... | ... | @@ -38,21 +46,28 @@ end |
| 38 | 46 | |
| 39 | 47 | desc "Update pot/po files to match new version." |
| 40 | 48 | task :updatepo do |
| 41 | - require 'gettext_rails/tools' | |
| 42 | - require_dependency 'noosfero' | |
| 43 | - | |
| 44 | - GetText::RubyParser::ID << '__' | |
| 45 | - GetText::RubyParser::PLURAL_ID << 'n__' | |
| 46 | - GetText::ActiveRecordParser.init(:use_classname => false) | |
| 47 | 49 | |
| 48 | 50 | puts 'Extracting strings from source. This may take a while ...' |
| 49 | - sources = | |
| 50 | - Dir.glob("{app,lib}/**/*.{rb,rhtml,erb}") + | |
| 51 | - Dir.glob('config/initializers/*.rb') + | |
| 52 | - Dir.glob('public/*.html.erb') + | |
| 53 | - Dir.glob('public/designs/themes/{base,noosfero,profile-base}/*.{rhtml,html.erb}') + | |
| 54 | - Dir.glob('plugins/**/{controllers,models,lib,views}/**/*.{rhtml,html.erb,rb}') | |
| 55 | - GetText.update_pofiles(Noosfero::PROJECT, sources, "#{Noosfero::PROJECT} #{Noosfero::VERSION}") | |
| 51 | + | |
| 52 | + files_to_translate = [ | |
| 53 | + "{app,lib}/**/*.{rb,rhtml,erb}", | |
| 54 | + 'config/initializers/*.rb', | |
| 55 | + 'public/*.html.erb', | |
| 56 | + 'public/designs/themes/{base,noosfero,profile-base}/*.{rhtml,html.erb}', | |
| 57 | + 'plugins/**/{controllers,models,lib,views}/**/*.{rhtml,html.erb,rb}', | |
| 58 | + ].map { |pattern| Dir.glob(pattern) }.flatten | |
| 59 | + | |
| 60 | + require 'gettext' | |
| 61 | + require 'gettext/tools' | |
| 62 | + GetText.update_pofiles( | |
| 63 | + 'noosfero', | |
| 64 | + files_to_translate, | |
| 65 | + Noosfero::VERSION, | |
| 66 | + { | |
| 67 | + po_root: 'po', | |
| 68 | + } | |
| 69 | + ) | |
| 70 | + | |
| 56 | 71 | end |
| 57 | 72 | |
| 58 | 73 | task :checkpo do | ... | ... |