chipairon-i18n-active_record 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3776f3bf880d52469fbb3146896c4b59d3231cbb
4
- data.tar.gz: 9a5b7f73d95a6f43473f152dafa84597f13688a2
3
+ metadata.gz: a21451f4f0606b0ece514ef310c4b6b1f173f15d
4
+ data.tar.gz: 0db34961a29760d60c8c440e4792b3eed056d1d9
5
5
  SHA512:
6
- metadata.gz: cc912db913b2b31b6be032c966f1de628eb0fc778825bfe2518a3cf7df48ba836bf9ef6ee0ff9c8a072c91c348d84024b2c4895841158de0ef010b54d81f4942
7
- data.tar.gz: 49dac427adabf790374ff32bf078654145cfaddaeabb735768560d3513dec6ebe10e0244d2b1527f0d0ac9801922ca1683f4ecc786e37ee3bc760cab1c2c9e7e
6
+ metadata.gz: 4b17dbb4cbacaa33ad46af65a66bc0a4ef1b0c1f6e9f67d0044f7549d9a39ddc4b0c402de96b6c2f54b2a482f6b3065cc20cf7d0888b26a663a4bc8b05234f14
7
+ data.tar.gz: aa4e52a831e4f7ccd2a38f18d71e16ffb37fa49f1cfdd4c795b4258c671ee794c933009c68878810e14898f888ab34c0244f71d0b8cacc2de46b7845c7c06ef9
@@ -1,6 +1,6 @@
1
1
  module I18n
2
2
  module ActiveRecord
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
6
6
 
@@ -41,6 +41,11 @@ module I18n
41
41
  key = normalize_flat_keys(locale, key, scope, separator)
42
42
  interpolations = options.keys - I18n::RESERVED_KEYS
43
43
 
44
+ # if already memoized, then a record already exists.
45
+ if @memoized_lookup and @memoized_lookup[locale.to_sym].keys.include? key.to_sym
46
+ return
47
+ end
48
+
44
49
  if I18n::available_locales.any?
45
50
  I18n::available_locales.each do |a_locale|
46
51
  store_if_not_exists(a_locale, key, interpolations, count)
@@ -50,13 +55,6 @@ module I18n
50
55
  end
51
56
  end
52
57
 
53
- def store_if_not_exists(locale, key, interpolations, count)
54
- unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
55
- keys = count ? I18n.t('i18n.plural.keys', :locale => locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
56
- keys.each { |key| store_default_translation(locale, key, interpolations) }
57
- end
58
- end
59
-
60
58
  def store_default_translation(locale, key, interpolations)
61
59
  translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
62
60
  translation.interpolations = interpolations
@@ -68,6 +66,25 @@ module I18n
68
66
  ensure
69
67
  self.store_default_translations(locale, key, options) unless translation
70
68
  end
69
+
70
+ private
71
+
72
+ def store_if_not_exists(locale, key, interpolations, count)
73
+ unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
74
+ keys = count ? get_plurals(locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
75
+ keys.each { |key| store_default_translation(locale, key, interpolations) }
76
+ end
77
+ end
78
+
79
+ # get plural rules or use English rules as default:
80
+ def get_plurals(locale)
81
+ plurals = I18n.t('i18n.plural.keys', :locale => locale)
82
+ # if no rules found, plurals is a string "translation missing...."
83
+ if plurals.is_a?(String)
84
+ plurals = [:zero, :one, :other]
85
+ end
86
+ plurals
87
+ end
71
88
  end
72
89
  end
73
90
  end
@@ -0,0 +1,96 @@
1
+ # This extension stores translation stub records for missing translations to
2
+ # the database.
3
+ #
4
+ # This is useful if you have a web based translation tool. It will populate
5
+ # the database with untranslated keys as the application is being used. A
6
+ # translator can then go through these and add missing translations.
7
+ #
8
+ # Example usage:
9
+ #
10
+ # I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
11
+ # I18n.backend = I18n::Backend::Chain.new(I18n::Backend::ActiveRecord.new, I18n::Backend::Simple.new)
12
+ #
13
+ # Stub records for pluralizations will also be created for each key defined
14
+ # in i18n.plural.keys.
15
+ #
16
+ # For example:
17
+ #
18
+ # # en.yml
19
+ # en:
20
+ # i18n:
21
+ # plural:
22
+ # keys: [:zero, :one, :other]
23
+ #
24
+ # # pl.yml
25
+ # pl:
26
+ # i18n:
27
+ # plural:
28
+ # keys: [:zero, :one, :few, :other]
29
+ #
30
+ # It will also persist interpolation keys in Translation#interpolations so
31
+ # translators will be able to review and use them.
32
+ module I18n
33
+ module Backend
34
+ class ActiveRecord
35
+ module Missing
36
+ include Flatten
37
+
38
+ def store_default_translations(locale, key, options = {})
39
+ count, scope, default, separator = options.values_at(:count, :scope, :default, :separator)
40
+ separator ||= I18n.default_separator
41
+ key = normalize_flat_keys(locale, key, scope, separator)
42
+ interpolations = options.keys - I18n::RESERVED_KEYS
43
+
44
+ # if already memoized, then a record already exists.
45
+ if @memoized_lookup and @memoized_lookup[locale.to_sym].keys.include? key.to_sym
46
+ return
47
+ end
48
+
49
+ if I18n::available_locales.any?
50
+ I18n::available_locales.each do |a_locale|
51
+ store_if_not_exists(a_locale, key, interpolations, count)
52
+ end
53
+ else
54
+ store_if_not_exists(locale, key, interpolations, count)
55
+ end
56
+ end
57
+
58
+ def store_if_not_exists(locale, key, interpolations, count)
59
+ unless ActiveRecord::Translation.locale(locale).lookup(key).exists?
60
+ <<<<<<< HEAD
61
+ keys = count ? I18n.t('i18n.plural.keys', :locale => locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
62
+ =======
63
+ interpolations = options.keys - I18n::RESERVED_KEYS
64
+ keys = count ? get_plurals(locale).map { |k| [key, k].join(FLATTEN_SEPARATOR) } : [key]
65
+ >>>>>>> e4d6427a8d4550e773199bef8410dcbe5f175f62
66
+ keys.each { |key| store_default_translation(locale, key, interpolations) }
67
+ end
68
+ end
69
+
70
+ def store_default_translation(locale, key, interpolations)
71
+ translation = ActiveRecord::Translation.new :locale => locale.to_s, :key => key
72
+ translation.interpolations = interpolations
73
+ translation.save
74
+ end
75
+
76
+ def translate(locale, key, options = {})
77
+ translation = super
78
+ ensure
79
+ self.store_default_translations(locale, key, options) unless translation
80
+ end
81
+
82
+ private
83
+ # get plural rules or use English rules as default:
84
+ def get_plurals(locale)
85
+ plurals = I18n.t('i18n.plural.keys', :locale => locale)
86
+ # if no rules found, plurals is a string "translation missing...."
87
+ if plurals.is_a?(String)
88
+ plurals = [:zero, :one, :other]
89
+ end
90
+ plurals
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
data/test/mi_test.rb ADDED
@@ -0,0 +1,97 @@
1
+ # test setup:
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'optparse'
5
+
6
+ module I18n
7
+ module Tests
8
+ class << self
9
+ def options
10
+ @options ||= { :with => [], :adapter => 'sqlite3' }
11
+ end
12
+
13
+ def parse_options!
14
+ OptionParser.new do |o|
15
+ o.on('-w', '--with DEPENDENCIES', 'Define dependencies') do |dep|
16
+ options[:with] = dep.split(',').map { |group| group.to_sym }
17
+ end
18
+ end.parse!
19
+
20
+ options[:with].each do |dep|
21
+ case dep
22
+ when :sqlite3, :mysql, :postgres
23
+ @options[:adapter] = dep
24
+ when :r23, :'rails-2.3.x'
25
+ ENV['BUNDLE_GEMFILE'] = 'ci/Gemfile.rails-2.3.x'
26
+ when :r3, :'rails-3.0.x'
27
+ ENV['BUNDLE_GEMFILE'] = 'ci/Gemfile.rails-3.x'
28
+ when :'no-rails'
29
+ ENV['BUNDLE_GEMFILE'] = 'ci/Gemfile.no-rails'
30
+ end
31
+ end
32
+
33
+ ENV['BUNDLE_GEMFILE'] ||= 'Gemfile'
34
+ end
35
+
36
+ def setup_active_record
37
+ begin
38
+ require 'active_record'
39
+ ActiveRecord::Base.connection
40
+ true
41
+ rescue LoadError => e
42
+ puts "can't use ActiveRecord backend because: #{e.message}"
43
+ rescue ActiveRecord::ConnectionNotEstablished
44
+ require 'i18n/backend/active_record'
45
+ require 'i18n/backend/active_record/store_procs'
46
+ connect_active_record
47
+ true
48
+ end
49
+ end
50
+
51
+ def connect_active_record
52
+ connect_adapter
53
+ ActiveRecord::Migration.verbose = false
54
+ ActiveRecord::Schema.define(:version => 1) do
55
+ create_table :translations, :force => true do |t|
56
+ t.string :locale
57
+ t.string :key
58
+ t.text :value
59
+ t.text :interpolations
60
+ t.boolean :is_proc, :default => false
61
+ end
62
+ add_index :translations, [:locale, :key], :unique => true
63
+ end
64
+ end
65
+
66
+ def connect_adapter
67
+ case options[:adapter].to_sym
68
+ when :sqlite3
69
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
70
+ when :mysql
71
+ # CREATE DATABASE i18n_unittest;
72
+ # CREATE USER 'i18n'@'localhost' IDENTIFIED BY '';
73
+ # GRANT ALL PRIVILEGES ON i18n_unittest.* to 'i18n'@'localhost';
74
+ ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "i18n_unittest", :username => "i18n", :password => "", :host => "localhost")
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ # test_helper:
82
+ require 'bundler/setup'
83
+ $:.unshift File.expand_path("../lib", File.dirname(__FILE__))
84
+ require 'i18n/active_record'
85
+
86
+ require 'i18n/tests'
87
+ I18n::Tests.setup_active_record
88
+
89
+ class MiTest < Test::Unit::TestCase
90
+ class Backend < I18n::Backend::ActiveRecord
91
+ include I18n::Backend::ActiveRecord::Missing
92
+ end
93
+
94
+ def test_add
95
+ assert_equal 2, 5
96
+ end
97
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chipairon-i18n-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -37,6 +37,7 @@ files:
37
37
  - lib/i18n/backend/active_record/missing.rb
38
38
  - lib/i18n/backend/active_record/store_procs.rb
39
39
  - lib/i18n/backend/active_record/translation.rb
40
+ - lib/i18n/backend/active_record/missing.rb.orig
40
41
  - lib/i18n/active_record.rb
41
42
  - lib/i18n/active_record/version.rb
42
43
  - test/test_setup.rb
@@ -44,11 +45,12 @@ files:
44
45
  - test/missing_test.rb
45
46
  - test/active_record_test.rb
46
47
  - test/all.rb
48
+ - test/mi_test.rb
47
49
  - test/api_test.rb
48
50
  - MIT-LICENSE
49
51
  - README.textile
50
52
  - Rakefile
51
- homepage: http://github.com/Chipairon/i18n-active_record
53
+ homepage: http://github.com/svenfuchs/i18n-active_record
52
54
  licenses:
53
55
  - MIT
54
56
  metadata: {}