i18n_auto_scoping 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ 0.2.0 (Apr 12, 2010)
2
+
3
+ * Releasing Gem
4
+
5
+ 0.1.0 (Apr 08, 2010)
6
+
7
+ * Initial release
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,16 @@
1
+ = I18nAutoScoping
2
+
3
+ I18nAutoScoping is a plugin for Ruby on Rails that lets you easily add auto-scope in your I18n translation.
4
+ It is very useful if you do not want to add every time the scope in your views ! Especially if your are working on big projects !
5
+
6
+ All necessary informations are available on the wiki : http://wiki.github.com/kwi/i18n_autoscoping
7
+
8
+ * I18nAutoScoping is thread safe.
9
+ * I18nAutoScoping works with Rails 2.x and Rails 3
10
+
11
+ == TODO
12
+
13
+ * Another bug fix ?
14
+
15
+
16
+ Copyright (c) 2010 Guillaume Luccisano - g-mai|: guillaume.luccisano, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'spec/rake/spectask'
4
+
5
+ spec_files = Rake::FileList["spec/**/*_spec.rb"]
6
+
7
+
8
+ desc "Run specs for current Rails version"
9
+ Spec::Rake::SpecTask.new do |t|
10
+ t.spec_files = spec_files
11
+ t.spec_opts = ["-c"]
12
+ end
13
+
14
+ task :default => :spec
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'i18n_auto_scoping'
@@ -0,0 +1,19 @@
1
+ # Auto scoping for ActionView
2
+ module ActionView
3
+ class Base
4
+ alias_method :i18n_auto_scoping_aliased_render, :render
5
+
6
+ # To set a default scope for all render
7
+ def render(opts = {}, *args)
8
+ old_auto_scope_in_render = Thread.current[:last_i18n_auto_scope_render_in_render]
9
+ old_auto_scope = Thread.current[:last_i18n_auto_scope_render]
10
+ Thread.current[:last_i18n_auto_scope_render_in_render] = true
11
+ Thread.current[:last_i18n_auto_scope_render] = nil
12
+
13
+ i18n_auto_scoping_aliased_render(opts, *args)
14
+ ensure
15
+ Thread.current[:last_i18n_auto_scope_render] = old_auto_scope
16
+ Thread.current[:last_i18n_auto_scope_render_in_render] = old_auto_scope_in_render
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,122 @@
1
+ module I18nAutoScoping
2
+ module ActionControllerDefaultScope
3
+
4
+ def set_default_i18n_scope
5
+ I18n::Scope.default = "controllers/#{params[:controller]}"
6
+ end
7
+
8
+ end
9
+
10
+ module I18nExtension
11
+
12
+ def self.included(mod)
13
+ mod.class_eval do
14
+ class << self
15
+ alias_method :i18n_auto_scoping_backend=, :backend=
16
+
17
+ def backend=(backend)
18
+ r = send(:i18n_auto_scoping_backend=, backend)
19
+ # When assigning a new backend, we try to extend it :
20
+ extend_backend_for_i18n_auto_scoping
21
+ r
22
+ end
23
+
24
+ def extend_backend_for_i18n_auto_scoping
25
+ # Only extend the backend for auto scoping if it had not been already done
26
+ if !backend.methods.include?('i18n_auto_scoping_translate')
27
+ backend.extend ::I18nAutoScoping::BackendExtension
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ module BackendExtension
39
+
40
+ def self.extended(k)
41
+ k.class_eval do
42
+ alias_method :i18n_auto_scoping_translate, :translate
43
+
44
+ # Override translate method in order to set autoscoping
45
+ def translate(locale, key, options = {})
46
+ # Set the default scope if needed
47
+ if !options.has_key?(:scope) or options[:scope] == :autoscoping
48
+ options[:scope] = I18n::Scope.default
49
+ end
50
+ i18n_auto_scoping_translate(locale, key, options)
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+ #i18n extension for having default namespacing
59
+ module I18n
60
+ module Scope
61
+ class << self
62
+ # Set a default scope
63
+ def default=(scope)
64
+ Thread.current[:i18n_default_scope] = (scope.is_a?(String)) ? scope.gsub('/', '.') : scope
65
+ end
66
+
67
+ def default
68
+ # Temporarly default scope go first
69
+ return Thread.current[:i18n_temporarly_default_scope] if Thread.current[:i18n_temporarly_default_scope]
70
+
71
+ # Then, if we are in a render, we try to get the current scope by scanning the caller stack
72
+ if Thread.current[:last_i18n_auto_scope_render_in_render]
73
+ # Cache the analyse of the caller stack result for performance
74
+ return (Thread.current[:last_i18n_auto_scope_render] ||= get_scope_views_context)
75
+ end
76
+
77
+ # Finally, if there is no scope before, return the default scope
78
+ Thread.current[:i18n_default_scope]
79
+ end
80
+
81
+ # Inspired from : http://github.com/yar/simple_loc_compat
82
+ # Return the current view file, only work in app/views folder
83
+ def get_scope_views_context
84
+ stack_to_analyse = caller
85
+ latest_app_file = caller.detect { |level| level =~ /.*\/app\/views\// }
86
+ return nil unless latest_app_file
87
+
88
+ scope = latest_app_file.match(/([^:]+):\d+.*/)[1]
89
+ path = scope.split('/app/views/').last
90
+ scope = File.basename(path, File.extname(path))
91
+ # If there is a second extension we keep it, in order to work with .erb.html or .erb.iphone, etc...
92
+ scope = "#{File.dirname(path)}/#{scope}" if File.dirname(path) != '.'
93
+
94
+ scope.gsub!('/', '.')
95
+
96
+ scope.to_sym
97
+ end
98
+
99
+ def temporarly_change(scope)
100
+ old = Thread.current[:i18n_temporarly_default_scope]
101
+ Thread.current[:i18n_temporarly_default_scope] = scope
102
+ r = yield
103
+ Thread.current[:i18n_temporarly_default_scope] = old
104
+ return r
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+
111
+ if defined? ActionView
112
+ require 'action_view_extension'
113
+ end
114
+
115
+ I18n.send :include, I18nAutoScoping::I18nExtension
116
+ I18n.extend_backend_for_i18n_auto_scoping
117
+
118
+ # Auto namespacing default in controller : controllers/controller_name
119
+ if defined? ActionController
120
+ ActionController::Base.send :include, I18nAutoScoping::ActionControllerDefaultScope
121
+ ActionController::Base.send :before_filter, :set_default_i18n_scope
122
+ end
@@ -0,0 +1 @@
1
+ <%= I18n.t(:hi) %>
@@ -0,0 +1 @@
1
+ <%= I18n.t(:hi) %>
@@ -0,0 +1 @@
1
+ <%= render :partial => 'partial' %>
@@ -0,0 +1 @@
1
+ <%= render 'folder/partial' %>
@@ -0,0 +1 @@
1
+ <%= I18n.t(:hi) %>
@@ -0,0 +1,3 @@
1
+ <%= I18n::Scope.temporarly_change(:test_scope) do
2
+ I18n.t(:hi)
3
+ end %>
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe :i18n_auto_scoping do
4
+ before(:all) do
5
+ @v = ActionView::Base.new([File.join(File.dirname(__FILE__), '../app/views/')], {}, FakeController.new)
6
+ end
7
+
8
+ it "should still keep working without scope" do
9
+ I18n.t(:hi).should == 'Hello'
10
+ end
11
+
12
+ it "should still keep working with scope" do
13
+ I18n.t(:hi, :scope => :bam).should == 'Hello in Bam'
14
+ end
15
+
16
+ it "Should works with scope temporarly changed" do
17
+ I18n::Scope.temporarly_change(:test_scope) do
18
+ I18n.t(:hi).should == 'Hello in Test Scope'
19
+ end
20
+ end
21
+
22
+ it "Should works with action view template" do
23
+ @v.render(:file => 'view').should == 'Hello in view'
24
+ end
25
+
26
+ it "Should work with action view partial without the underscore" do
27
+ @v.render('partial').should == 'Hello in partial'
28
+ end
29
+
30
+ it "Should works with action view nested template" do
31
+ @v.render(:file => 'nested').should == 'Hello in partial'
32
+ end
33
+
34
+ it "Should works with action view nested nested template" do
35
+ @v.render(:file => 'nested_nested').should == 'Hello in nested partial'
36
+ end
37
+
38
+ it "Should works with scope temporarly changed in view" do
39
+ @v.render(:file => 'view_with_temporarly_change').should == 'Hello in Test Scope'
40
+ end
41
+
42
+ end
@@ -0,0 +1,13 @@
1
+ :en:
2
+ :hi: 'Hello'
3
+ :test_scope:
4
+ :hi: 'Hello in Test Scope'
5
+ :bam:
6
+ :hi: 'Hello in Bam'
7
+ :view:
8
+ :hi: 'Hello in view'
9
+ :_partial:
10
+ :hi: 'Hello in partial'
11
+ :folder:
12
+ :_partial:
13
+ :hi: 'Hello in nested partial'
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require 'i18n'
5
+ require 'action_controller'
6
+ require 'action_view'
7
+
8
+ # Add I18n load_path
9
+ I18n.load_path = (I18n.load_path << Dir[File.join(File.dirname(__FILE__), 'locales', '*.yml')]).uniq
10
+
11
+ require File.dirname(__FILE__) + '/../init.rb'
12
+
13
+ class FakeController
14
+ def self.method_missing(meth)
15
+ return nil
16
+ end
17
+
18
+ def params
19
+ {:controller => :fake, :action => :show}
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_auto_scoping
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Guillaume Luccisano
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-12 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: I18nAutoScoping is a plugin for Ruby on Rails that lets you easily add auto-scope in your I18n translation. It is very useful if you do not want to add every time the scope in your views ! Especially if your are working on big projects !
22
+ email: guillaume.luccisano@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/action_view_extension.rb
31
+ - lib/i18n_auto_scoping.rb
32
+ - spec/app/views/_partial.erb
33
+ - spec/app/views/folder/_partial.erb
34
+ - spec/app/views/nested.erb
35
+ - spec/app/views/nested_nested.erb
36
+ - spec/app/views/view.erb
37
+ - spec/app/views/view_with_temporarly_change.erb
38
+ - spec/i18n_auto_scoping/i18n_auto_scoping_spec.rb
39
+ - spec/locales/en.yml
40
+ - spec/spec_helper.rb
41
+ - CHANGELOG.rdoc
42
+ - MIT-LICENSE
43
+ - Rakefile
44
+ - README.rdoc
45
+ - init.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/kwi/i18n_auto_scoping
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 1
68
+ - 3
69
+ - 4
70
+ version: 1.3.4
71
+ requirements: []
72
+
73
+ rubyforge_project: i18n_auto_scoping
74
+ rubygems_version: 1.3.6
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: I18nAutoScoping lets you easily add auto-scope in your I18n translations for your Rails' views.
78
+ test_files: []
79
+