i18n 0.4.2 → 0.5.0beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of i18n might be problematic. Click here for more details.
- data/CHANGELOG.textile +12 -3
- data/lib/i18n.rb +18 -21
- data/lib/i18n/backend.rb +0 -1
- data/lib/i18n/backend/base.rb +0 -13
- data/lib/i18n/backend/cache.rb +0 -2
- data/lib/i18n/backend/cascade.rb +6 -10
- data/lib/i18n/backend/chain.rb +0 -2
- data/lib/i18n/backend/cldr.rb +1 -2
- data/lib/i18n/backend/fallbacks.rb +0 -2
- data/lib/i18n/backend/gettext.rb +0 -2
- data/lib/i18n/backend/interpolation_compiler.rb +1 -3
- data/lib/i18n/backend/key_value.rb +1 -3
- data/lib/i18n/backend/memoize.rb +1 -3
- data/lib/i18n/backend/pluralization.rb +0 -2
- data/lib/i18n/backend/simple.rb +0 -2
- data/lib/i18n/config.rb +2 -2
- data/lib/i18n/core_ext/string/interpolate.rb +1 -3
- data/lib/i18n/exceptions.rb +29 -6
- data/lib/i18n/gettext.rb +0 -2
- data/lib/i18n/gettext/helpers.rb +0 -1
- data/lib/i18n/locale/fallbacks.rb +0 -2
- data/lib/i18n/locale/tag/parents.rb +0 -2
- data/lib/i18n/locale/tag/rfc4646.rb +0 -2
- data/lib/i18n/locale/tag/simple.rb +0 -2
- data/lib/i18n/tests.rb +12 -0
- data/lib/i18n/tests/basics.rb +54 -0
- data/lib/i18n/tests/defaults.rb +40 -0
- data/lib/i18n/tests/interpolation.rb +133 -0
- data/lib/i18n/tests/link.rb +56 -0
- data/lib/i18n/tests/localization.rb +19 -0
- data/lib/i18n/tests/localization/date.rb +84 -0
- data/lib/i18n/tests/localization/date_time.rb +77 -0
- data/lib/i18n/tests/localization/procs.rb +103 -0
- data/lib/i18n/tests/localization/time.rb +76 -0
- data/lib/i18n/tests/lookup.rb +74 -0
- data/lib/i18n/tests/pluralization.rb +35 -0
- data/lib/i18n/tests/procs.rb +55 -0
- data/lib/i18n/version.rb +1 -1
- metadata +19 -10
- data/lib/i18n/backend/active_record.rb +0 -61
- data/lib/i18n/backend/active_record/missing.rb +0 -65
- data/lib/i18n/backend/active_record/store_procs.rb +0 -38
- data/lib/i18n/backend/active_record/translation.rb +0 -110
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
|
3
|
-
module I18n
|
4
|
-
module Backend
|
5
|
-
# ActiveRecord model used to store actual translations to the database.
|
6
|
-
#
|
7
|
-
# This model expects a table like the following to be already set up in
|
8
|
-
# your the database:
|
9
|
-
#
|
10
|
-
# create_table :translations do |t|
|
11
|
-
# t.string :locale
|
12
|
-
# t.string :key
|
13
|
-
# t.text :value
|
14
|
-
# t.text :interpolations
|
15
|
-
# t.boolean :is_proc, :default => false
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# This model supports to named scopes :locale and :lookup. The :locale
|
19
|
-
# scope simply adds a condition for a given locale:
|
20
|
-
#
|
21
|
-
# I18n::Backend::ActiveRecord::Translation.locale(:en).all
|
22
|
-
# # => all translation records that belong to the :en locale
|
23
|
-
#
|
24
|
-
# The :lookup scope adds a condition for looking up all translations
|
25
|
-
# that either start with the given keys (joined by an optionally given
|
26
|
-
# separator or I18n.default_separator) or that exactly have this key.
|
27
|
-
#
|
28
|
-
# # with translations present for :"foo.bar" and :"foo.baz"
|
29
|
-
# I18n::Backend::ActiveRecord::Translation.lookup(:foo)
|
30
|
-
# # => an array with both translation records :"foo.bar" and :"foo.baz"
|
31
|
-
#
|
32
|
-
# I18n::Backend::ActiveRecord::Translation.lookup([:foo, :bar])
|
33
|
-
# I18n::Backend::ActiveRecord::Translation.lookup(:"foo.bar")
|
34
|
-
# # => an array with the translation record :"foo.bar"
|
35
|
-
#
|
36
|
-
# When the StoreProcs module was mixed into this model then Procs will
|
37
|
-
# be stored to the database as Ruby code and evaluated when :value is
|
38
|
-
# called.
|
39
|
-
#
|
40
|
-
# Translation = I18n::Backend::ActiveRecord::Translation
|
41
|
-
# Translation.create \
|
42
|
-
# :locale => 'en'
|
43
|
-
# :key => 'foo'
|
44
|
-
# :value => lambda { |key, options| 'FOO' }
|
45
|
-
# Translation.find_by_locale_and_key('en', 'foo').value
|
46
|
-
# # => 'FOO'
|
47
|
-
class ActiveRecord
|
48
|
-
class Translation < ::ActiveRecord::Base
|
49
|
-
TRUTHY_CHAR = "\001"
|
50
|
-
FALSY_CHAR = "\002"
|
51
|
-
|
52
|
-
set_table_name 'translations'
|
53
|
-
attr_protected :is_proc, :interpolations
|
54
|
-
|
55
|
-
serialize :value
|
56
|
-
serialize :interpolations, Array
|
57
|
-
|
58
|
-
class << self
|
59
|
-
def locale(locale)
|
60
|
-
scoped(:conditions => { :locale => locale.to_s })
|
61
|
-
end
|
62
|
-
|
63
|
-
def lookup(keys, *separator)
|
64
|
-
column_name = connection.quote_column_name('key')
|
65
|
-
keys = Array(keys).map! { |key| key.to_s }
|
66
|
-
|
67
|
-
unless separator.empty?
|
68
|
-
warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
|
69
|
-
"You can change the internal separator by overwriting FLATTEN_SEPARATOR."
|
70
|
-
end
|
71
|
-
|
72
|
-
namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
|
73
|
-
scoped(:conditions => ["#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace])
|
74
|
-
end
|
75
|
-
|
76
|
-
def available_locales
|
77
|
-
Translation.find(:all, :select => 'DISTINCT locale').map { |t| t.locale.to_sym }
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def interpolates?(key)
|
82
|
-
self.interpolations.include?(key) if self.interpolations
|
83
|
-
end
|
84
|
-
|
85
|
-
def value
|
86
|
-
value = read_attribute(:value)
|
87
|
-
if is_proc
|
88
|
-
Kernel.eval(value)
|
89
|
-
elsif value == FALSY_CHAR
|
90
|
-
false
|
91
|
-
elsif value == TRUTHY_CHAR
|
92
|
-
true
|
93
|
-
else
|
94
|
-
value
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def value=(value)
|
99
|
-
if value === false
|
100
|
-
value = FALSY_CHAR
|
101
|
-
elsif value === true
|
102
|
-
value = TRUTHY_CHAR
|
103
|
-
end
|
104
|
-
|
105
|
-
write_attribute(:value, value)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|