Permalink
Browse files

support gettext 2.3.1+ and a little cleanup

  • Loading branch information...
1 parent 49907d6 commit 984d9680da0a61b139b0b2545f94619546a8ef87 @grosser committed Sep 2, 2012
View
2 Gemfile
@@ -5,7 +5,7 @@ gem 'haml'
gem 'slim'
gem 'hamlet'
gem 'ruby_parser'
-gem 'gettext'
+gem 'gettext', "2.3.1", :github => "ruby-gettext/gettext" # waiting for 2.3.1 to be released
gem 'sqlite3', '~>1.3.6'
gem 'rails', ENV['RAILS'] || '~>3'
gem 'rake'
View
2 gettext_i18n_rails.gemspec
@@ -9,5 +9,5 @@ Gem::Specification.new name, GettextI18nRails::VERSION do |s|
s.homepage = "http://github.com/grosser/#{name}"
s.files = `git ls-files`.split("\n")
s.license = "MIT"
- s.add_runtime_dependency "fast_gettext"
+ s.add_runtime_dependency "fast_gettext", ">=0.4.8"
end
View
14 lib/gettext_i18n_rails.rb
@@ -1,15 +1,11 @@
require 'gettext_i18n_rails/version'
+require 'gettext_i18n_rails/gettext_hooks'
module GettextI18nRails
- extend self
end
+# translate from everywhere
require 'fast_gettext'
-if Gem::Version.new(FastGettext::VERSION) < Gem::Version.new("0.4.8")
- raise "Please upgrade fast_gettext"
-end
-
-# include translations into all the places it needs to go...
Object.send(:include, FastGettext::Translation)
# make translations html_safe if possible and wanted
@@ -18,11 +14,14 @@ module GettextI18nRails
Object.send(:include, GettextI18nRails::HtmlSafeTranslations)
end
+# set up the backend
require 'gettext_i18n_rails/backend'
I18n.backend = GettextI18nRails::Backend.new
+# make I18n play nice with FastGettext
require 'gettext_i18n_rails/i18n_hacks'
+# translate activerecord errors
if defined? Rails::Railtie # Rails 3+
# load active_model extensions at the correct point in time
require 'gettext_i18n_rails/railtie'
@@ -34,4 +33,5 @@ module GettextI18nRails
end
end
-require 'gettext_i18n_rails/action_controller' if defined?(ActionController) # so that bundle console can work in a rails project
+# make bundle console work in a rails project
+require 'gettext_i18n_rails/action_controller' if defined?(ActionController)
View
17 lib/gettext_i18n_rails/base_parser.rb
@@ -1,12 +1,15 @@
-require 'gettext/utils'
-begin
- require 'gettext/tools/rgettext'
-rescue LoadError #version prior to 2.0
- require 'gettext/rgettext'
-end
-
module GettextI18nRails
class BaseParser
+ # copy AS 3 feature so we can play nicely with AS2 and if rails env is not there
+ class << self
+ attr_accessor :parser_descendants
+ end
+ self.parser_descendants = []
+
+ def self.inherited(subclass)
+ parser_descendants << subclass
+ end
+
def self.target?(file)
File.extname(file) == ".#{extension}"
end
View
31 lib/gettext_i18n_rails/gettext_hooks.rb
@@ -0,0 +1,31 @@
+module GettextI18nRails
+ module GettextHooks
+ def self.add_parsers_to_gettext
+ return if @add_parsers_to_gettext
+ @add_parsers_to_gettext = true
+
+ require "gettext_i18n_rails/haml_parser"
+ require "gettext_i18n_rails/hamlet_parser"
+ require "gettext_i18n_rails/slim_parser"
+
+ GettextI18nRails::BaseParser.parser_descendants.each do |parser|
+ xgettext.add_parser(parser)
+ end
+ end
+
+ def self.xgettext
+ @xgettext ||= begin
+ require 'gettext/tools/xgettext' # 2.3+
+ GetText::Tools::XGetText
+ rescue LoadError
+ begin
+ require 'gettext/tools/rgettext' # 2.0 - 2.2
+ GetText::RGetText
+ rescue LoadError # # 1.x
+ require 'gettext/rgettext'
+ GetText::RGetText
+ end
+ end
+ end
+ end
+end
View
2 lib/gettext_i18n_rails/haml_parser.rb
@@ -11,5 +11,3 @@ def self.convert_to_code(text)
end
end
end
-
-GetText::RGetText.add_parser(GettextI18nRails::HamlParser)
View
3 lib/gettext_i18n_rails/hamlet_parser.rb
@@ -11,6 +11,3 @@ def self.convert_to_code(text)
end
end
end
-
-GetText::RGetText.add_parser(GettextI18nRails::HamletParser)
-
View
11 lib/gettext_i18n_rails/ruby_gettext_extractor.rb
@@ -1,17 +1,6 @@
# new ruby parser from retoo, that should help extracting "#{_('xxx')}", which is needed especially when parsing haml files
-#
-#!/usr/bin/ruby
-# parser/ruby.rb - look for gettext msg strings in ruby files
-
-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
View
2 lib/gettext_i18n_rails/slim_parser.rb
@@ -11,5 +11,3 @@ def self.convert_to_code(text)
end
end
end
-
-GetText::RGetText.add_parser(GettextI18nRails::SlimParser)
View
7 lib/gettext_i18n_rails/tasks.rb
@@ -2,6 +2,7 @@
def load_gettext
require 'gettext'
require 'gettext/utils'
+ require 'gettext_i18n_rails/gettext_hooks'
end
desc "Create mo-files for L10n"
@@ -13,11 +14,9 @@ def load_gettext
desc "Update pot/po files."
task :find => :environment do
load_gettext
- $LOAD_PATH << File.join(File.dirname(__FILE__),'..','..','lib')
- require 'gettext_i18n_rails/haml_parser'
- require 'gettext_i18n_rails/slim_parser'
- require 'gettext_i18n_rails/hamlet_parser'
+ $LOAD_PATH << File.join(File.dirname(__FILE__),'..','..','lib') # needed when installed as plugin
+ GettextI18nRails::GettextHooks.add_parsers_to_gettext
if GetText.respond_to? :update_pofiles_org
if defined?(Rails.application)
View
28 spec/gettext_i18n_rails/gettext_hooks_spec.rb
@@ -0,0 +1,28 @@
+require "spec_helper"
+
+describe GettextI18nRails::GettextHooks do
+ describe ".add_parsers_to_gettext" do
+ def parsers
+ GettextI18nRails::GettextHooks.xgettext # load XGetText
+ GettextI18nRails::GettextHooks.instance_variable_set(:@add_parsers_to_gettext, nil) # can add_parsers_to_gettext multiple times
+ GetText::Tools::XGetText.class_variable_get(:@@default_parsers)
+ end
+
+ before do
+ parsers.clear
+ end
+
+ it "should add all parsers to gettext" do
+ expect{
+ GettextI18nRails::GettextHooks.add_parsers_to_gettext
+ }.to change{ parsers.size }.by(+3)
+ end
+
+ it "should not add them twice" do
+ expect{
+ GettextI18nRails::GettextHooks.add_parsers_to_gettext
+ GettextI18nRails::GettextHooks.add_parsers_to_gettext
+ }.to change{ parsers.size }.by(+3)
+ end
+ end
+end
View
6 spec/spec_helper.rb
@@ -79,7 +79,11 @@ class Part < ActiveRecord::Base
end
class NotConventional < ActiveRecord::Base
- set_table_name :not_at_all_conventionals
+ if ActiveRecord::VERSION::MAJOR == 3
+ self.table_name = :not_at_all_conventionals
+ else
+ set_table_name :not_at_all_conventionals
+ end
end
class Idea < ActiveRecord::Base

0 comments on commit 984d968

Please sign in to comment.