public
Description: Globalization made easy with interface in place translations
Homepage: http://lucaguidi.com/projects/click-to-globalize
Clone URL: git://github.com/jodosha/click-to-globalize.git
Extracted formatting logic in #format_translations
jodosha (author)
Thu Jul 17 08:27:38 -0700 2008
commit  5bd9bc33b696264e5a54a42383f42627590ca5df
tree    b249af6c4759d11d63d6399d8eb42f9f1111dc26
parent  049a81063b80561132cbdb8d38e424c4ae8dfb6a
...
107
108
109
110
 
111
112
113
...
205
206
207
208
 
209
210
211
212
213
214
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
217
218
...
107
108
109
 
110
111
112
113
...
205
206
207
 
208
209
 
 
 
 
 
 
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
0
@@ -107,7 +107,7 @@ module Globalize # :nodoc:
0
       #   * textilize_without_paragraph (textile)
0
       #   * markdown (markdown)
0
       def formatting_method
0
-        case @@formatting
0
+        @@formatting_method ||= case @@formatting
0
           when :textile  then :textilize_without_paragraph
0
           when :markdown then :markdown
0
         end
0
@@ -205,14 +205,24 @@ module Globalize # :nodoc:
0
       def observe_locales
0
         return unless globalize?
0
         locale_observer = LocaleObserver.new
0
-        Globalize::Locale.add_observer(locale_observer)
0
+        Locale.add_observer(locale_observer)
0
         yield
0
-        Globalize::Locale.remove_observer(locale_observer)
0
-        session[:__globalize_translations] = if Locale.formatting
0
-                                               locale_observer.translations.each{|key, translation| locale_observer.translations[key] = strip_tags(self.send(Locale.formatting_method, translation)) }
0
-                                             else
0
-                                               locale_observer.translations
0
-                                             end
0
+        Locale.remove_observer(locale_observer)
0
+        session[:__globalize_translations] = format_translations(locale_observer)
0
+      end
0
+      
0
+      # Fetch the translations from the given LocaleObserver.
0
+      # If the formatting feature is turned on it also provide to textilize or markdown.
0
+      def format_translations(locale_observer)
0
+        if Locale.formatting
0
+          formatting_method = Locale.formatting_method
0
+          locale_observer.translations.inject({}) do |result, translation|
0
+            result[translation.first] = self.send(formatting_method, strip_tags(translation.last))
0
+            result
0
+          end
0
+        else
0
+          locale_observer.translations
0
+        end
0
       end
0
     end
0
   end
...
6
7
8
9
10
 
 
11
12
13
...
222
223
224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
226
227
...
252
253
254
255
256
 
 
 
 
 
 
 
 
 
 
257
258
...
6
7
8
 
 
9
10
11
12
13
...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
...
277
278
279
 
 
280
281
282
283
284
285
286
287
288
289
290
291
0
@@ -6,8 +6,8 @@ class ClickToGlobalizeController < ApplicationController
0
   around_filter :observe_locales
0
   def index
0
     Locale.set(params[:locale])
0
-    hello_world = Translation.find_by_tr_key_and_language_id(params[:key], params[:language_id])
0
-    @greet = hello_world.tr_key.t
0
+    translation = Translation.find_by_tr_key_and_language_id(params[:key], params[:language_id])
0
+    @greet = translation.tr_key.t
0
     render :nothing => true, :status => 200
0
   end
0
 end
0
@@ -222,6 +222,31 @@ class ClickToGlobalizeTest < Test::Unit::TestCase
0
     assert_equal expected, @request.session[:__globalize_translations]
0
   end
0
 
0
+  def test_should_return_formatted_translations
0
+    create_translation('hello_mars', '*Hello Mars!*')
0
+    with_formatting :textile do
0
+      get :index, params(:key => 'hello_mars')
0
+      assert_response :success
0
+      
0
+      expected = { 'hello_mars' => %(<strong>Hello Mars!</strong>) }
0
+      assert_equal expected, @request.session[:__globalize_translations]
0
+    end
0
+  end
0
+
0
+  uses_mocha 'ClickToGlobalizeFormattingTest' do
0
+    def test_should_return_plain_translations
0
+      Locale.stubs(:formatting).returns nil
0
+      create_translation('hello_moon', '*Hello Moon!*')
0
+      with_formatting :unexistent do
0
+        get :index, params(:key => 'hello_moon')
0
+        assert_response :success
0
+
0
+        expected = { 'hello_moon' => '*Hello Moon!*' }
0
+        assert_equal expected, @request.session[:__globalize_translations]      
0
+      end
0
+    end
0
+  end
0
+
0
   ### LOCALE_CONTROLLER
0
 
0
   def test_check_globalize
0
@@ -252,7 +277,15 @@ class ClickToGlobalizeTest < Test::Unit::TestCase
0
       @locale_observer ||= LocaleObserver.new
0
     end
0
 
0
-    def params
0
-      { :key => @hello_world.tr_key, :language_id => 1, :locale => @default_locale.code }
0
+    def params(options = {})
0
+      { :key => @hello_world.tr_key, :language_id => 1, :locale => @default_locale.code }.merge!(options)
0
+    end
0
+    
0
+    def create_translation(key, text)
0
+      translation = Translation.new(:tr_key => key, :text => text,
0
+        :language_id => 1, :pluralization_index => 1) do |t|
0
+        t.type = 'ViewTranslation'
0
+      end
0
+      translation.save
0
     end
0
 end
...
52
53
54
 
 
 
 
 
 
 
 
 
55
56
57
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
0
@@ -52,6 +52,15 @@ class Test::Unit::TestCase
0
   end
0
 end
0
 
0
+# Thanks to Rails Core Team
0
+def uses_mocha(description)
0
+  require 'rubygems'
0
+  require 'mocha'
0
+  yield
0
+rescue LoadError
0
+  $stderr.puts "Skipping #{description} tests. `gem install mocha` and try again."
0
+end
0
+
0
 LocalesController.class_eval do #:nodoc:
0
   public :clear_cache, :inline
0
 end

Comments