localite 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
@@ -0,0 +1,88 @@
1
+ module Localite::Filter
2
+ module ControllerFilter
3
+ def self.locale(controller)
4
+ #
5
+ # get the current locale
6
+ locale = begin
7
+ controller.send(:current_locale)
8
+ rescue NoMethodError
9
+ end
10
+ end
11
+
12
+ def self.filter(controller, &block)
13
+ #
14
+ # set up localite for this action.
15
+ Localite.in(locale(controller)) do
16
+ Localite.html(&block)
17
+
18
+ filter_response controller
19
+ end
20
+ rescue
21
+ controller.response.body = "Caught exception: " + CGI::escapeHTML($!.inspect) if Rails.env.development?
22
+ raise
23
+ end
24
+
25
+ def self.filter_response(controller)
26
+ next unless controller.response.headers["Content-Type"] =~ /text\/html/
27
+ controller.response.body = filter_lang_nodes(controller.response.body)
28
+ end
29
+
30
+ def self.filter_lang_nodes(body)
31
+ doc = Nokogiri.XML "<filter-outer-span>#{body}</filter-outer-span>"
32
+
33
+ doc.css("[lang]").each do |node|
34
+ if Localite.locale.to_s != node["lang"]
35
+ node.remove
36
+ next
37
+ end
38
+
39
+ #
40
+ # we have a node with content specific for the current locale,
41
+ # i.e. a node to keep. If we find a base locale sibling (i.e.
42
+ # with identical attributes but no "lang" attribute) prior
43
+ # this node we have to remove that.
44
+ next unless base = base_node(node)
45
+ base.remove
46
+ end
47
+
48
+ doc.css("filter-outer-span").inner_html
49
+ end
50
+
51
+ def self.base_node(node)
52
+ previous = node
53
+ while (previous = previous.previous) && previous.name == "text" do
54
+ :void
55
+ end
56
+
57
+ return previous if base_node?(node, previous)
58
+ end
59
+
60
+ def self.base_node?(me, other_node)
61
+ return false if !other_node
62
+ return false if me.name != other_node.name
63
+ return false if other_node.attributes.key?("lang")
64
+ return false if me.attributes.length != other_node.attributes.length + 1
65
+
66
+ # do we have a mismatching attribute?
67
+ other_node.attributes.each { |k,v|
68
+ return false if me.attributes[k] != v
69
+ }
70
+
71
+ true
72
+ end
73
+ end
74
+
75
+ module ControllerMethods
76
+ #
77
+ # override this method to adjust localite parameters
78
+ def current_locale
79
+ return params[:locale] if params[:locale] && params[:locale] =~ /^[a-z][a-z]$/
80
+ return params[:lang] if params[:lang] && params[:lang] =~ /^[a-z][a-z]$/
81
+ end
82
+ end
83
+
84
+ def self.included(klass)
85
+ klass.send :include, ControllerMethods
86
+ klass.send :around_filter, ControllerFilter
87
+ end
88
+ end
@@ -36,6 +36,10 @@ class Localite::Template < String
36
36
  end
37
37
 
38
38
  def method_missing(sym, *args, &block)
39
+ unless @host.is_a?(Hash)
40
+ return @host.send(sym, *args, &block)
41
+ end
42
+
39
43
  begin
40
44
  return @host.fetch(sym.to_sym)
41
45
  rescue IndexError
@@ -93,6 +97,16 @@ module Localite::Template::Etest
93
97
  assert_equal "3 Fixnums and 1 Float", Template.run(:text, "{*pl xyz*} and {*pl fl*}", :xyz => [1, 2, 3], :fl => [1.0])
94
98
  end
95
99
 
100
+ class Name < String
101
+ def name
102
+ self
103
+ end
104
+ end
105
+
106
+ def test_nohash
107
+ assert_equal "abc", Template.run(:text, "{*name*}", Name.new("abc"))
108
+ end
109
+
96
110
  def test_pl
97
111
  h = Object.new.extend(Localite::Template::Env::Helpers)
98
112
  assert_equal "1 apple", h.pl("apple", 1)
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{localite}
5
- s.version = "0.1.3"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["pboy"]
9
9
  s.date = %q{2010-05-11}
10
10
  s.description = %q{An easy to use localization gem.}
11
11
  s.email = %q{eno-pboy@open-lab.org}
12
- s.extra_rdoc_files = ["lib/localite.rb", "lib/localite/rails_filter.rb", "lib/localite/scope.rb", "lib/localite/settings.rb", "lib/localite/template.rb", "lib/localite/translate.rb", "tasks/echoe.rake"]
13
- s.files = ["Manifest", "Rakefile", "VERSION", "config/dependencies.rb", "config/gem.yml", "lib/localite.rb", "lib/localite/rails_filter.rb", "lib/localite/scope.rb", "lib/localite/settings.rb", "lib/localite/template.rb", "lib/localite/translate.rb", "localite.tmproj", "script/console", "script/rebuild", "tasks/echoe.rake", "test/i18n/de.yml", "test/initializers/fake_rails.rb", "test/test.rb", "localite.gemspec"]
12
+ s.extra_rdoc_files = ["lib/localite.rb", "lib/localite/filter.rb", "lib/localite/scope.rb", "lib/localite/settings.rb", "lib/localite/template.rb", "lib/localite/translate.rb", "tasks/echoe.rake"]
13
+ s.files = ["Manifest", "Rakefile", "VERSION", "config/dependencies.rb", "config/gem.yml", "lib/localite.rb", "lib/localite/filter.rb", "lib/localite/scope.rb", "lib/localite/settings.rb", "lib/localite/template.rb", "lib/localite/translate.rb", "localite.tmproj", "script/console", "script/rebuild", "tasks/echoe.rake", "test/i18n/de.yml", "test/initializers/fake_rails.rb", "test/test.rb", "localite.gemspec"]
14
14
  s.homepage = %q{http://github.com/pboy/localite}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Localite"]
16
16
  s.require_paths = ["lib"]
@@ -2,6 +2,8 @@
2
2
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
3
  <plist version="1.0">
4
4
  <dict>
5
+ <key>currentDocument</key>
6
+ <string>script/console</string>
5
7
  <key>documents</key>
6
8
  <array>
7
9
  <dict>
@@ -10,7 +12,7 @@
10
12
  <key>name</key>
11
13
  <string>localite</string>
12
14
  <key>regexFolderFilter</key>
13
- <string>!.*/(\.[^/]*|CVS|localite|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
15
+ <string>!.*/(\.[^/]*|CVS|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
14
16
  <key>sourceDirectory</key>
15
17
  <string></string>
16
18
  </dict>
@@ -18,7 +20,116 @@
18
20
  <key>fileHierarchyDrawerWidth</key>
19
21
  <integer>339</integer>
20
22
  <key>metaData</key>
21
- <dict/>
23
+ <dict>
24
+ <key>config/dependencies.rb</key>
25
+ <dict>
26
+ <key>caret</key>
27
+ <dict>
28
+ <key>column</key>
29
+ <integer>0</integer>
30
+ <key>line</key>
31
+ <integer>4</integer>
32
+ </dict>
33
+ <key>firstVisibleColumn</key>
34
+ <integer>0</integer>
35
+ <key>firstVisibleLine</key>
36
+ <integer>0</integer>
37
+ </dict>
38
+ <key>config/gem.yml</key>
39
+ <dict>
40
+ <key>caret</key>
41
+ <dict>
42
+ <key>column</key>
43
+ <integer>0</integer>
44
+ <key>line</key>
45
+ <integer>7</integer>
46
+ </dict>
47
+ <key>firstVisibleColumn</key>
48
+ <integer>0</integer>
49
+ <key>firstVisibleLine</key>
50
+ <integer>0</integer>
51
+ </dict>
52
+ <key>lib/localite.rb</key>
53
+ <dict>
54
+ <key>caret</key>
55
+ <dict>
56
+ <key>column</key>
57
+ <integer>24</integer>
58
+ <key>line</key>
59
+ <integer>25</integer>
60
+ </dict>
61
+ <key>firstVisibleColumn</key>
62
+ <integer>0</integer>
63
+ <key>firstVisibleLine</key>
64
+ <integer>40</integer>
65
+ </dict>
66
+ <key>lib/localite/scope.rb</key>
67
+ <dict>
68
+ <key>caret</key>
69
+ <dict>
70
+ <key>column</key>
71
+ <integer>0</integer>
72
+ <key>line</key>
73
+ <integer>50</integer>
74
+ </dict>
75
+ <key>firstVisibleColumn</key>
76
+ <integer>0</integer>
77
+ <key>firstVisibleLine</key>
78
+ <integer>0</integer>
79
+ </dict>
80
+ <key>lib/localite/settings.rb</key>
81
+ <dict>
82
+ <key>caret</key>
83
+ <dict>
84
+ <key>column</key>
85
+ <integer>0</integer>
86
+ <key>line</key>
87
+ <integer>15</integer>
88
+ </dict>
89
+ <key>firstVisibleColumn</key>
90
+ <integer>0</integer>
91
+ <key>firstVisibleLine</key>
92
+ <integer>0</integer>
93
+ </dict>
94
+ <key>lib/localite/template.rb</key>
95
+ <dict>
96
+ <key>caret</key>
97
+ <dict>
98
+ <key>column</key>
99
+ <integer>0</integer>
100
+ <key>line</key>
101
+ <integer>54</integer>
102
+ </dict>
103
+ <key>firstVisibleColumn</key>
104
+ <integer>0</integer>
105
+ <key>firstVisibleLine</key>
106
+ <integer>31</integer>
107
+ </dict>
108
+ <key>script/console</key>
109
+ <dict>
110
+ <key>caret</key>
111
+ <dict>
112
+ <key>column</key>
113
+ <integer>47</integer>
114
+ <key>line</key>
115
+ <integer>16</integer>
116
+ </dict>
117
+ <key>firstVisibleColumn</key>
118
+ <integer>0</integer>
119
+ <key>firstVisibleLine</key>
120
+ <integer>0</integer>
121
+ </dict>
122
+ </dict>
123
+ <key>openDocuments</key>
124
+ <array>
125
+ <string>lib/localite.rb</string>
126
+ <string>lib/localite/scope.rb</string>
127
+ <string>lib/localite/settings.rb</string>
128
+ <string>script/console</string>
129
+ <string>lib/localite/template.rb</string>
130
+ <string>config/gem.yml</string>
131
+ <string>config/dependencies.rb</string>
132
+ </array>
22
133
  <key>showFileHierarchyDrawer</key>
23
134
  <true/>
24
135
  <key>windowFrame</key>
@@ -14,7 +14,7 @@ rescue LoadError
14
14
  end
15
15
 
16
16
  $: << "#{File.dirname(__FILE__)}/../lib"
17
- load "#{File.dirname(__FILE__)}/../init.rb"
17
+ load "#{File.dirname(__FILE__)}/../lib/localite.rb"
18
18
 
19
19
  I18n.load_path += Dir["#{File.dirname(__FILE__)}/../test/i18n/*.yml"]
20
20
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 3
9
- version: 0.1.3
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - pboy
@@ -37,7 +37,7 @@ extensions: []
37
37
 
38
38
  extra_rdoc_files:
39
39
  - lib/localite.rb
40
- - lib/localite/rails_filter.rb
40
+ - lib/localite/filter.rb
41
41
  - lib/localite/scope.rb
42
42
  - lib/localite/settings.rb
43
43
  - lib/localite/template.rb
@@ -50,7 +50,7 @@ files:
50
50
  - config/dependencies.rb
51
51
  - config/gem.yml
52
52
  - lib/localite.rb
53
- - lib/localite/rails_filter.rb
53
+ - lib/localite/filter.rb
54
54
  - lib/localite/scope.rb
55
55
  - lib/localite/settings.rb
56
56
  - lib/localite/template.rb
@@ -1,35 +0,0 @@
1
- module Localite::RailsFilter
2
- def self.filter(controller, &block)
3
- #
4
- # get the current locale
5
- locale = begin
6
- controller.send(:current_locale)
7
- rescue NoMethodError
8
- end
9
-
10
- #
11
- # get the current locale
12
- begin
13
- scope = controller.send(:localite_scope)
14
- scope = [ scope ] if scope && !scope.is_a?(Array)
15
- rescue NoMethodError
16
- end
17
-
18
- #
19
- # set up localite for this action.
20
- Localite.html do
21
- Localite.in(locale) do
22
- Localite.scope(*scope, &block)
23
- end
24
- end
25
- end
26
-
27
- private
28
-
29
- #
30
- # override this method to adjust localite parameters
31
- def current_locale
32
- return params[:locale] if params[:locale] && params[:locale] =~ /^[a-z][a-z]$/
33
- return params[:lang] if params[:lang] && params[:lang] =~ /^[a-z][a-z]$/
34
- end
35
- end