GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Dead simple Rails localization.
Homepage: http://errtheblog.com/posts/55-ya-talkin-gibberish
Clone URL: git://github.com/defunkt/gibberish.git
some warning about how were modifying ruby core yada yada
defunkt (author)
Thu Feb 15 04:40:07 -0800 2007
commit  58756a6f296c36569bdb7d54b656c9d8756fd8a6
tree    273dd250ed8d3ff33229d541dd373831265a28c9
parent  0a6f8e53c2584050c8fc8f8befb4189b16e49241
0
...
1
2
3
 
4
5
6
...
59
60
61
62
 
63
64
65
66
 
67
68
 
69
70
71
 
 
 
 
72
73
74
...
100
101
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
104
...
1
2
 
3
4
5
6
...
59
60
61
 
62
63
64
65
66
67
68
69
70
71
72
 
73
74
75
76
77
78
79
...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
0
@@ -1,6 +1,6 @@
0
 = Gibberish
0
 
0
-Yet another localization library. Maybe with the most agreeable API.
0
+Yet another localization library. Maybe with the most agreeable API?
0
 
0
 = Usage
0
 
0
@@ -59,16 +59,21 @@ This of course works with your translations:
0
 
0
 Neat. What other methods do we get?
0
 
0
-The classic around_filter use case:
0
+The classic around_filter:
0
 
0
   >> Gibberish.default_language?
0
   => true
0
   >> Gibberish.use_language(:es) do
0
+ ?> puts "English? #{Gibberish.default_language?}"
0
   ?> puts "hey"[:hey]
0
   >> end
0
+ English? false
0
   ˇHey allí!
0
 
0
-So, in your ApplicationController (or somewhere):
0
+For the duration of the block, :es is set as the language of choice. After the block is run everything
0
+returns to normal.
0
+
0
+Implement it:
0
 
0
   class ApplicationController < ActionController::Base
0
     around_filter :use_language
0
@@ -100,5 +105,20 @@ modified. No need to reboot the server.
0
 
0
 More as it's needed.
0
 
0
+= Warning
0
+
0
+By default, Ruby returns nil when a symbol is passed to String's [] method. Some of Rails, it seems, depends
0
+on this behavior. Yes, I am changing !!core Ruby behavior!! The humanity!
0
+
0
+To deal with this assumption, Gibberish has a reserved_keys array. It, by default, contains :limit (so Rails
0
+migrations don't explode on you.) To add to this array, just pass it more keys:
0
+
0
+ >> Gibberish.add_reserved_key :another_key
0
+ => [:limit, :another_key]
0
+ >> Gibberish.add_reserved_keys :more, :keys
0
+ => [:limit, :another_key, :more, :keys]
0
+
0
+You've been warned. It really shouldn't affect you, though.
0
+
0
 >> Chris Wanstrath
0
 => chris[at]ozmm[dot]org
...
6
7
8
 
 
 
 
 
9
10
11
...
6
7
8
9
10
11
12
13
14
15
16
0
@@ -6,6 +6,11 @@ module Gibberish
0
     @@reserved_keys = [ :limit ]
0
     mattr_reader :reserved_keys
0
 
0
+ def add_reserved_key(*key)
0
+ (@@reserved_keys += key.flatten).uniq!
0
+ end
0
+ alias :add_reserved_keys :add_reserved_key
0
+
0
     @@languages = {}
0
     def languages
0
       @@languages.keys
...
78
79
80
 
 
 
 
 
 
 
 
 
 
81
82
83
...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
0
@@ -78,6 +78,16 @@ context "After loading languages, Gibberish" do
0
     languages.should.include :es
0
     languages.should.include :fr
0
   end
0
+
0
+ specify "should be able to accept new, unique reserved keys" do
0
+ key = :something_evil
0
+ Gibberish.add_reserved_key key
0
+ Gibberish.reserved_keys.should.include key
0
+ Gibberish.reserved_keys.size.should.equal 2
0
+ Gibberish.add_reserved_key key
0
+ Gibberish.add_reserved_key key
0
+ Gibberish.reserved_keys.size.should.equal 2
0
+ end
0
 end
0
 
0
 context "When no language is set" do

Comments

    No one has commented yet.