Commit 85bd2779a3aafa92a8141f911fdc2dfadc12d26d

Authored by Victor Costa
Committed by Antonio Terceiro
1 parent 39d29854

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>
@@ -17,6 +17,7 @@ gem &#39;nokogiri&#39; @@ -17,6 +17,7 @@ gem &#39;nokogiri&#39;
17 gem 'rake', :require => false 17 gem 'rake', :require => false
18 gem 'rest-client' 18 gem 'rest-client'
19 gem 'exception_notification' 19 gem 'exception_notification'
  20 +gem 'gettext', :require => false, :group => :development
20 21
21 # FIXME list here all actual dependencies (i.e. the ones in debian/control), 22 # FIXME list here all actual dependencies (i.e. the ones in debian/control),
22 # with their GEM names (not the Debian package names) 23 # with their GEM names (not the Debian package names)
@@ -67,6 +67,8 @@ GEM @@ -67,6 +67,8 @@ GEM
67 activesupport (>= 3.0.4) 67 activesupport (>= 3.0.4)
68 fast_gettext (0.6.8) 68 fast_gettext (0.6.8)
69 ffi (1.0.11) 69 ffi (1.0.11)
  70 + gettext (2.2.1)
  71 + locale
70 gherkin (2.4.21) 72 gherkin (2.4.21)
71 json (>= 1.4.6) 73 json (>= 1.4.6)
72 hike (1.2.1) 74 hike (1.2.1)
@@ -74,6 +76,7 @@ GEM @@ -74,6 +76,7 @@ GEM
74 i18n (0.6.0) 76 i18n (0.6.0)
75 journey (1.0.3) 77 journey (1.0.3)
76 json (1.7.3) 78 json (1.7.3)
  79 + locale (2.0.5)
77 mail (2.4.4) 80 mail (2.4.4)
78 i18n (>= 0.4.0) 81 i18n (>= 0.4.0)
79 mime-types (~> 1.16) 82 mime-types (~> 1.16)
@@ -172,6 +175,7 @@ DEPENDENCIES @@ -172,6 +175,7 @@ DEPENDENCIES
172 database_cleaner 175 database_cleaner
173 exception_notification 176 exception_notification
174 fast_gettext 177 fast_gettext
  178 + gettext
175 hpricot 179 hpricot
176 mocha 180 mocha
177 nokogiri 181 nokogiri
app/controllers/my_profile/cms_controller.rb
@@ -227,7 +227,7 @@ class CmsController &lt; MyProfileController @@ -227,7 +227,7 @@ class CmsController &lt; MyProfileController
227 @article = profile.articles.find(params[:id]) 227 @article = profile.articles.find(params[:id])
228 if request.post? 228 if request.post?
229 @article.destroy 229 @article.destroy
230 - session[:notice] = _("\"#{@article.name}\" was removed.") 230 + session[:notice] = _("\"%s\" was removed." % @article.name)
231 referer = Rails.application.routes.recognize_path URI.parse(request.referer).path rescue nil 231 referer = Rails.application.routes.recognize_path URI.parse(request.referer).path rescue nil
232 if referer and referer[:controller] == 'cms' and referer[:action] != 'edit' 232 if referer and referer[:controller] == 'cms' and referer[:action] != 'edit'
233 redirect_to referer 233 redirect_to referer
app/helpers/application_helper.rb
@@ -1281,9 +1281,9 @@ module ApplicationHelper @@ -1281,9 +1281,9 @@ module ApplicationHelper
1281 1281
1282 def delete_article_message(article) 1282 def delete_article_message(article)
1283 if article.folder? 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 else 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 end 1287 end
1288 end 1288 end
1289 1289
app/models/article.rb
@@ -157,14 +157,17 @@ class Article &lt; ActiveRecord::Base @@ -157,14 +157,17 @@ class Article &lt; ActiveRecord::Base
157 self.profile 157 self.profile
158 end 158 end
159 159
160 - def self.human_attribute_name(attrib, options = {}) 160 + def self.human_attribute_name_with_customization(attrib, options={})
161 case attrib.to_sym 161 case attrib.to_sym
162 when :name 162 when :name
163 _('Title') 163 _('Title')
164 else 164 else
165 - _(self.superclass.human_attribute_name(attrib)) 165 + _(self.human_attribute_name_without_customization(attrib))
166 end 166 end
167 end 167 end
  168 + class << self
  169 + alias_method_chain :human_attribute_name, :customization
  170 + end
168 171
169 def css_class_list 172 def css_class_list
170 [self.class.name.to_css_class] 173 [self.class.name.to_css_class]
app/models/change_password.rb
@@ -2,16 +2,19 @@ class ChangePassword &lt; Task @@ -2,16 +2,19 @@ class ChangePassword &lt; Task
2 2
3 attr_accessor :password, :password_confirmation 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 case attrib.to_sym 6 case attrib.to_sym
7 when :password 7 when :password
8 _('Password') 8 _('Password')
9 when :password_confirmation 9 when :password_confirmation
10 _('Password Confirmation') 10 _('Password Confirmation')
11 else 11 else
12 - _(self.superclass.human_attribute_name(attrib)) 12 + _(self.human_attribute_name_without_customization(attrib))
13 end 13 end
14 end 14 end
  15 + class << self
  16 + alias_method_chain :human_attribute_name, :customization
  17 + end
15 18
16 validates_presence_of :requestor 19 validates_presence_of :requestor
17 20
app/models/user.rb
@@ -16,15 +16,18 @@ class User &lt; ActiveRecord::Base @@ -16,15 +16,18 @@ class User &lt; ActiveRecord::Base
16 end 16 end
17 17
18 # FIXME ugly workaround 18 # FIXME ugly workaround
19 - def self.human_attribute_name(attrib, options={}) 19 + def self.human_attribute_name_with_customization(attrib, options={})
20 case attrib.to_sym 20 case attrib.to_sym
21 when :login 21 when :login
22 return [_('Username'), _('Email')].join(' / ') 22 return [_('Username'), _('Email')].join(' / ')
23 when :email 23 when :email
24 return _('e-Mail') 24 return _('e-Mail')
25 - else _(self.superclass.human_attribute_name(attrib)) 25 + else _(self.human_attribute_name_without_customization(attrib))
26 end 26 end
27 end 27 end
  28 + class << self
  29 + alias_method_chain :human_attribute_name, :customization
  30 + end
28 31
29 before_create do |user| 32 before_create do |user|
30 if user.environment.nil? 33 if user.environment.nil?
lib/noosfero/i18n.rb
@@ -20,5 +20,5 @@ if File.exists?(locale_dir) @@ -20,5 +20,5 @@ if File.exists?(locale_dir)
20 repos << FastGettext::TranslationRepository.build('iso_3166', :type => 'mo', :path => locale_dir) 20 repos << FastGettext::TranslationRepository.build('iso_3166', :type => 'mo', :path => locale_dir)
21 end 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 = &#39;tmp/makemo.stamp&#39; @@ -6,8 +6,16 @@ makemo_stamp = &#39;tmp/makemo.stamp&#39;
6 desc "Create mo-files for L10n" 6 desc "Create mo-files for L10n"
7 task :makemo => makemo_stamp 7 task :makemo => makemo_stamp
8 file makemo_stamp => Dir.glob('po/*/noosfero.po') do 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 Rake::Task['symlinkmo'].invoke 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 FileUtils.mkdir_p 'tmp' 19 FileUtils.mkdir_p 'tmp'
12 FileUtils.touch makemo_stamp 20 FileUtils.touch makemo_stamp
13 end 21 end
@@ -22,7 +30,7 @@ task :symlinkmo do @@ -22,7 +30,7 @@ task :symlinkmo do
22 'pt' => 'pt_BR', 30 'pt' => 'pt_BR',
23 } 31 }
24 mkdir_p(Rails.root.join('locale')) 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 lang = File.basename(dir) 34 lang = File.basename(dir)
27 orig_lang = langmap[lang] || lang 35 orig_lang = langmap[lang] || lang
28 mkdir_p(Rails.root.join('locale', "#{lang}", 'LC_MESSAGES')) 36 mkdir_p(Rails.root.join('locale', "#{lang}", 'LC_MESSAGES'))
@@ -38,21 +46,28 @@ end @@ -38,21 +46,28 @@ end
38 46
39 desc "Update pot/po files to match new version." 47 desc "Update pot/po files to match new version."
40 task :updatepo do 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 puts 'Extracting strings from source. This may take a while ...' 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 end 71 end
57 72
58 task :checkpo do 73 task :checkpo do