snusnu-dm-is-localizable 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,42 @@
1
+ [59dd665 | Mon May 18 17:23:50 UTC 2009] snusnu <gamsnjaga@gmail.com>
2
+
3
+ * Version bump to 0.0.7
4
+
5
+ [845b6f0 | Mon May 18 16:44:16 UTC 2009] snusnu <gamsnjaga@gmail.com>
6
+
7
+ * more specs for the generated property translation readers
8
+
9
+ [30990da | Mon May 18 16:31:07 UTC 2009] snusnu <gamsnjaga@gmail.com>
10
+
11
+ * fixed README
12
+
13
+ [e8c9981 | Mon May 18 16:25:46 UTC 2009] snusnu <gamsnjaga@gmail.com>
14
+
15
+ * added readers that return the property translated to the target language.
16
+
17
+ If the property :name is declared to be localizable for class Item,
18
+ and there is a ItemTranslation for 'en-US', you're able to call
19
+
20
+ * i.name(:en_US)
21
+
22
+ on an instance of Item named i
23
+
24
+ [0981aa8 | Mon May 18 16:10:25 UTC 2009] snusnu <gamsnjaga@gmail.com>
25
+
26
+ * README cosmetics
27
+
28
+ [f8993ae | Mon May 18 16:07:00 UTC 2009] snusnu <gamsnjaga@gmail.com>
29
+
30
+ * added localizable_properties class level API
31
+
32
+ [9bace64 | Mon May 18 15:52:23 UTC 2009] snusnu <gamsnjaga@gmail.com>
33
+
34
+ * updated README to reflect latest API addition
35
+
36
+ [4a43df6 | Mon May 18 15:43:52 UTC 2009] snusnu <gamsnjaga@gmail.com>
37
+
38
+ * stripped whitespace, updated changelog and regenerated gemspec
39
+
1
40
  [82db7fd | Mon May 18 15:42:50 UTC 2009] snusnu <gamsnjaga@gmail.com>
2
41
 
3
42
  * Version bump to 0.0.6
data/README.textile CHANGED
@@ -68,7 +68,13 @@ end
68
68
 
69
69
  The above @Item@ model will define and thus be able to @DataMapper.auto_migrate!@ the @ItemTranslation@ model. The _naming convention_ used here is @"#{ClassToBeLocalized.name}Translation"@.
70
70
 
71
- Preliminary support for changing this is available by using the @:class_name@ option like so: @DataMapper::Model.is :localized, :class_name => 'ItemLocalization'@. However, this currently not yet specced.
71
+ Preliminary support for changing this is available by using the @:class_name@ option like so (note that this isn't specced yet).
72
+
73
+ <pre>
74
+ <code>
75
+ @DataMapper::Model.is :localized, :class_name => 'ItemLocalization'@
76
+ </code>
77
+ </pre>
72
78
 
73
79
  <pre>
74
80
  <code>
@@ -138,6 +144,16 @@ class Item
138
144
  nr_of_available_languages * all.size == translation_class.all.size
139
145
  end
140
146
 
147
+ # returns a list of symbols reflecting all localizable property names of this resource
148
+ def localizable_properties
149
+ translation_class.properties.map do |p|
150
+ p.name
151
+ end.select do |p|
152
+ # exclude properties that are'nt localizable
153
+ p != :id && p != :language_id && p != Extlib::Inflection.foreign_key(self.name).to_sym
154
+ end
155
+ end
156
+
141
157
  # ----------------------
142
158
  # instance level API
143
159
  # ----------------------
@@ -157,6 +173,26 @@ class Item
157
173
  self.class.nr_of_available_languages == translations.size
158
174
  end
159
175
 
176
+ # translates the given attribute to the language identified by the given language_code
177
+ def translate(attribute, language_code)
178
+ if language = Language[language_code]
179
+ t = translations.first(:language_id => language.id)
180
+ t.respond_to?(attribute) ? t.send(attribute) : nil
181
+ else
182
+ nil
183
+ end
184
+ end
185
+
186
+ # translates the :name property to the given language
187
+ def name(language_code)
188
+ translate(:name, language_code)
189
+ end
190
+
191
+ # translates the :desc property to the given language
192
+ def desc(language_code)
193
+ translate(:desc, language_code)
194
+ end
195
+
160
196
 
161
197
  # TODO
162
198
  # more API to support common usecases (and i18n/l10n solutions)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{dm-is-localizable}
5
- s.version = "0.0.6"
5
+ s.version = "0.0.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Martin Gamsjaeger (snusnu)"]
@@ -39,9 +39,19 @@ module DataMapper
39
39
 
40
40
  has n, :languages, :through => remixee
41
41
 
42
- self.class_eval(<<-EOS, __FILE__, __LINE__ + 1)
42
+ self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
43
43
  alias :translations #{remixee}
44
- EOS
44
+ RUBY
45
+
46
+ localizable_properties.each do |property_name|
47
+ self.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
48
+
49
+ def #{property_name}(language_code)
50
+ translate(:#{property_name.to_sym}, language_code)
51
+ end
52
+
53
+ RUBY
54
+ end
45
55
 
46
56
  end
47
57
 
@@ -62,6 +72,16 @@ module DataMapper
62
72
  available_languages.size * all.size == translation_class.all.size
63
73
  end
64
74
 
75
+ # returns a list of symbols reflecting all localizable property names of this resource
76
+ def localizable_properties
77
+ translation_class.properties.map do |p|
78
+ p.name
79
+ end.select do |p|
80
+ # exclude properties that are'nt localizable
81
+ p != :id && p != :language_id && p != Extlib::Inflection.foreign_key(self.name).to_sym
82
+ end
83
+ end
84
+
65
85
  end
66
86
 
67
87
  module InstanceMethods
@@ -81,6 +101,7 @@ module DataMapper
81
101
  self.class.nr_of_available_languages == translations.size
82
102
  end
83
103
 
104
+ # translates the given attribute to the language identified by the given language_code
84
105
  def translate(attribute, language_code)
85
106
  if language = Language[language_code]
86
107
  t = translations.first(:language_id => language.id)
@@ -156,4 +156,14 @@ describe "class level API:" do
156
156
 
157
157
  end
158
158
 
159
+ describe "localizable_properties" do
160
+
161
+ it "should return a list of symbols reflecting the localizable properties" do
162
+ Item.localizable_properties.size.should == 2
163
+ Item.localizable_properties.should include(:name)
164
+ Item.localizable_properties.should include(:desc)
165
+ end
166
+
167
+ end
168
+
159
169
  end
@@ -208,4 +208,43 @@ describe "instance level API:" do
208
208
 
209
209
  end
210
210
 
211
+ describe "property_name(language_code)" do
212
+
213
+ before :each do
214
+ @l1 = Language.create :code => 'en-US', :name => 'English'
215
+ @l2 = Language.create :code => 'de-AT', :name => 'Deutsch'
216
+ @i1 = Item.create
217
+ @t1 = ItemTranslation.create :item => @i1, :language => @l1, :name => 'Book', :desc => 'Literature'
218
+ @t2 = ItemTranslation.create :item => @i1, :language => @l2, :name => 'Buch', :desc => 'Literatur'
219
+ end
220
+
221
+ describe "with a nil language_code" do
222
+
223
+ it "should return nil" do
224
+ @i1.name(nil).should be_nil
225
+ end
226
+
227
+ end
228
+
229
+ describe "with a non existent language_code" do
230
+
231
+ it "should return nil" do
232
+ @i1.name(:it).should be_nil
233
+ end
234
+
235
+ end
236
+
237
+ describe "with an existing language_code" do
238
+
239
+ it "should return the translated property" do
240
+ @i1.name(:en_US).should == 'Book'
241
+ @i1.desc(:en_US).should == 'Literature'
242
+ @i1.name(:de_AT).should == 'Buch'
243
+ @i1.desc(:de_AT).should == 'Literatur'
244
+ end
245
+
246
+ end
247
+
248
+ end
249
+
211
250
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snusnu-dm-is-localizable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Gamsjaeger (snusnu)