Skip to content

Commit

Permalink
update plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Dec 10, 2011
1 parent 44c5ea6 commit 4871536
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 40 deletions.
1 change: 1 addition & 0 deletions vendor/plugins/gettext_i18n_rails/Gemfile
@@ -1,6 +1,7 @@
source :rubygems

gem 'fast_gettext'
gem 'ruby_parser'

group :dev do
gem 'sqlite3', '~>1.3.4'
Expand Down
6 changes: 5 additions & 1 deletion vendor/plugins/gettext_i18n_rails/Gemfile.lock
Expand Up @@ -84,10 +84,13 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
ruby_parser (2.3.1)
sexp_processor (~> 3.0)
sexp_processor (3.0.8)
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.4)
thor (0.14.6)
tilt (1.3.3)
Expand All @@ -105,4 +108,5 @@ DEPENDENCIES
rails (~> 3)
rake
rspec (~> 2)
ruby_parser
sqlite3 (~> 1.3.4)
6 changes: 3 additions & 3 deletions vendor/plugins/gettext_i18n_rails/Rakefile
Expand Up @@ -3,9 +3,9 @@ task :spec do
end

task :default do
sh "RAILS=2.3.14 bundle && bundle exec rake spec"
sh "RAILS=3.0.9 bundle && bundle exec rake spec"
sh "RAILS=3.1.0 bundle && bundle exec rake spec"
sh "RAILS=2.3.14 && (bundle || bundle install) && bundle exec rake spec"
sh "RAILS=3.0.9 && (bundle || bundle install) && bundle exec rake spec"
sh "RAILS=3.1.0 && (bundle || bundle install) && bundle exec rake spec"
end

begin
Expand Down
2 changes: 2 additions & 0 deletions vendor/plugins/gettext_i18n_rails/Readme.md
Expand Up @@ -210,6 +210,8 @@ lib/tasks/gettext.rake:
- [ed0h](http://github.com/ed0h)
- [Nikos Dimitrakopoulos](http://blog.nikosd.com)
- [Ben Tucker](http://btucker.net/)
- [Kamil Śliwak](https://github.com/cameel)
- [Paul McMahon](https://github.com/pwim)

[Michael Grosser](http://grosser.it)<br/>
grosser.michael@gmail.com<br/>
Expand Down
2 changes: 1 addition & 1 deletion vendor/plugins/gettext_i18n_rails/VERSION
@@ -1 +1 @@
0.3.0
0.3.6
7 changes: 5 additions & 2 deletions vendor/plugins/gettext_i18n_rails/gettext_i18n_rails.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{gettext_i18n_rails}
s.version = "0.3.0"
s.version = "0.3.6"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Grosser"]
s.date = %q{2011-09-25}
s.date = %q{2011-12-09}
s.email = %q{grosser.michael@gmail.com}
s.files = [
"Gemfile",
Expand Down Expand Up @@ -49,11 +49,14 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<fast_gettext>, [">= 0"])
s.add_runtime_dependency(%q<ruby_parser>, [">= 0"])
else
s.add_dependency(%q<fast_gettext>, [">= 0"])
s.add_dependency(%q<ruby_parser>, [">= 0"])
end
else
s.add_dependency(%q<fast_gettext>, [">= 0"])
s.add_dependency(%q<ruby_parser>, [">= 0"])
end
end

8 changes: 7 additions & 1 deletion vendor/plugins/gettext_i18n_rails/lib/gettext_i18n_rails.rb
Expand Up @@ -22,6 +22,12 @@ module GettextI18nRails
I18n.backend = GettextI18nRails::Backend.new

require 'gettext_i18n_rails/i18n_hacks'
require 'gettext_i18n_rails/active_record' if defined?(ActiveRecord)

require 'gettext_i18n_rails/active_record'
# If configuration via Railties is not available force activerecord extensions
if not defined?(Rails::Railtie) and defined?(ActiveRecord)
ActiveRecord::Base.extend GettextI18nRails::ActiveRecord
end

require 'gettext_i18n_rails/action_controller' if defined?(ActionController) # so that bundle console can work in a rails project
require 'gettext_i18n_rails/railtie'
@@ -1,6 +1,6 @@
class ActionController::Base
def set_gettext_locale
requested_locale = params[:locale] || session[:locale] || cookies[:locale] || request.env['HTTP_ACCEPT_LANGUAGE']
requested_locale = params[:locale] || session[:locale] || cookies[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'] || I18n.default_locale
locale = FastGettext.set_locale(requested_locale)
session[:locale] = locale
I18n.locale = locale # some weird overwriting in action-controller makes this necessary ... see I18nProxy
Expand Down
@@ -1,21 +1,21 @@
class ActiveRecord::Base
module GettextI18nRails::ActiveRecord
# CarDealer.sales_count -> s_('CarDealer|Sales count') -> 'Sales count' if no translation was found
def self.human_attribute_name(attribute, *args)
def human_attribute_name(attribute, *args)
s_(gettext_translation_for_attribute_name(attribute))
end

# CarDealer -> _('car dealer')
def self.human_name(*args)
def human_name(*args)
_(self.human_name_without_translation)
end

def self.human_name_without_translation
def human_name_without_translation
self.to_s.underscore.gsub('_',' ')
end

private

def self.gettext_translation_for_attribute_name(attribute)
def gettext_translation_for_attribute_name(attribute)
"#{self}|#{attribute.to_s.split('.').map! {|a| a.gsub('_',' ').capitalize }.join('|')}"
end
end
Expand Up @@ -2,25 +2,31 @@ module GettextI18nRails
#write all found models/columns to a file where GetTexts ruby parser can find them
def store_model_attributes(options)
file = options[:to] || 'locale/model_attributes.rb'
File.open(file,'w') do |f|
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
ModelAttributesFinder.new.find(options).each do |table_name,column_names|
#model name
begin
model = table_name.singularize.camelcase.constantize
rescue NameError
# Some tables are not models, for example: translation tables created by globalize2.
next
end
f.puts("_('#{model.human_name_without_translation}')")

#all columns namespaced under the model
column_names.each do |attribute|
translation = model.gettext_translation_for_attribute_name(attribute)
f.puts("_('#{translation}')")
begin
File.open(file,'w') do |f|
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
ModelAttributesFinder.new.find(options).each do |table_name,column_names|
#model name
model = table_name_to_namespaced_model(table_name)
if model == nil
# Some tables are not models, for example: translation tables created by globalize2.
puts "[Warning] Model not found for table '#{table_name}'"
next
end
f.puts("_('#{model.human_name_without_translation}')")

#all columns namespaced under the model
column_names.each do |attribute|
translation = model.gettext_translation_for_attribute_name(attribute)
f.puts("_('#{translation}')")
end
end
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
end
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
rescue
puts "[Error] Attribute extraction failed. Removing incomplete file (#{file})"
File.delete(file)
raise
end
end
module_function :store_model_attributes
Expand Down Expand Up @@ -49,4 +55,54 @@ def ignored?(name,patterns)
patterns.detect{|p|p.to_s==name.to_s or (p.is_a?(Regexp) and name=~p)}
end
end

private
# Tries to find the model class corresponding to specified table name.
# Takes into account that the model can be defined in a namespace.
# Searches only up to one level deep - won't find models nested in two
# or more modules.
#
# Note that if we allow namespaces, the conversion can be ambiguous, i.e.
# if the table is named "aa_bb_cc" and AaBbCc, Aa::BbCc and AaBb::Cc are
# all defined there's no absolute rule that tells us which one to use.
# This method prefers the less nested one and, if there are two at
# the same level, the one with shorter module name.
def table_name_to_namespaced_model(table_name)
# First assume that there are no namespaces
model = to_class(table_name.singularize.camelcase)
return model if model != nil

# If you were wrong, assume that the model is in a namespace.
# Iterate over the underscores and try to substitute each of them
# for a slash that camelcase() replaces with the scope operator (::).
underscore_position = table_name.index('_')
while underscore_position != nil
namespaced_table_name = table_name.dup
namespaced_table_name[underscore_position] = '/'
model = to_class(namespaced_table_name.singularize.camelcase)
return model if model != nil

underscore_position = table_name.index('_', underscore_position + 1)
end

# The model either is not defined or is buried more than one level
# deep in a module hierarchy
return nil
end

# Checks if there is a class of specified name and if so, returns
# the class object. Otherwise returns nil.
def to_class(name)
# I wanted to use Module.const_defined?() here to avoid relying
# on exceptions for normal program flow but it's of no use.
# If class autoloading is enabled, the constant may be undefined
# but turn out to be present when we actually try to use it.
begin
constant = name.constantize
rescue NameError
return nil
end

return constant.is_a?(Class) ? constant : nil
end
end
Expand Up @@ -2,9 +2,21 @@
if defined?(Rails::Railtie)
module GettextI18nRails
class Railtie < ::Rails::Railtie
config.gettext_i18n_rails = ActiveSupport::OrderedOptions.new
config.gettext_i18n_rails.msgmerge = %w[--sort-output --no-location --no-wrap]
config.gettext_i18n_rails.use_for_active_record_attributes = true

rake_tasks do
require 'gettext_i18n_rails/tasks'
end

config.after_initialize do |app|
if app.config.gettext_i18n_rails.use_for_active_record_attributes
ActiveSupport.on_load :active_record do
extend GettextI18nRails::ActiveRecord
end
end
end
end
end
end
Expand Up @@ -2,12 +2,16 @@
#
#!/usr/bin/ruby
# parser/ruby.rb - look for gettext msg strings in ruby files
# Copyright (C) 2009 Reto Schüttel <reto (ät) schuettel (dot) ch>
# You may redistribute it and/or modify it under the same license terms as Ruby.

require 'rubygems'
require 'ruby_parser'

begin
require 'gettext/tools/rgettext'
rescue LoadError #version prior to 2.0
require 'gettext/rgettext'
end

module RubyGettextExtractor
extend self

Expand Down Expand Up @@ -137,4 +141,4 @@ def new_call recv, meth, args = nil
super recv, meth, args
end
end
end
end
Expand Up @@ -18,12 +18,17 @@ def load_gettext


if GetText.respond_to? :update_pofiles_org
if defined?(Rails.application)
msgmerge = Rails.application.config.gettext_i18n_rails.msgmerge
end
msgmerge ||= %w[--sort-output --no-location --no-wrap]

GetText.update_pofiles_org(
text_domain,
files_to_translate,
"version 0.0.1",
:po_root => locale_path,
:msgmerge=>['--sort-output']
:msgmerge => msgmerge
)
else #we are on a version < 2.0
puts "install new GetText with gettext:install to gain more features..."
Expand Down
Expand Up @@ -3,19 +3,23 @@
FastGettext.silence_errors

describe ActionController::Base do
before do
#controller
@c = ActionController::Base.new
def reset!
fake_session = {}
@c.stub!(:session).and_return fake_session
fake_cookies = {}
@c.stub!(:cookies).and_return fake_cookies
@c.params = {}
@c.request = stub(:env => {})
end

before do
#controller
@c = ActionController::Base.new
reset!

#locale
FastGettext.available_locales = nil
FastGettext.locale = 'fr'
FastGettext.locale = I18n.default_locale = 'fr'
FastGettext.available_locales = ['fr','en']
end

Expand All @@ -32,9 +36,19 @@
FastGettext.locale.should == 'fr'
end

it "locale isn't cached over request" do
@c.params = {:locale=>'en'}
@c.set_gettext_locale
@c.session[:locale].should == 'en'

reset!
@c.set_gettext_locale
@c.session[:locale].should == 'fr'
end

it "reads the locale from the HTTP_ACCEPT_LANGUAGE" do
@c.request.stub!(:env).and_return 'HTTP_ACCEPT_LANGUAGE'=>'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'
@c.set_gettext_locale
FastGettext.locale.should == 'en'
end
end
end
Expand Up @@ -19,6 +19,8 @@
end
end

ActiveRecord::Base.extend GettextI18nRails::ActiveRecord

class CarSeat < ActiveRecord::Base
validates_presence_of :seat_color, :message=>"translate me"
has_many :parts
Expand Down

0 comments on commit 4871536

Please sign in to comment.