i18n_lazy_scope 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8dc1ed2523804d760805abfb82ed7479ffaea98a
4
+ data.tar.gz: 3e1c861232b2cf3e7cc130eac814bf7c2e013071
5
+ SHA512:
6
+ metadata.gz: 4ba9f541345de796626518e20843e8865a8a9e2ee303e3cd22d88bc2663a45acf82d3571dcaa182f6419182ae3c17daff7cffdc55c532b3fd753cb9d52787b72
7
+ data.tar.gz: 0817f901549cc0913798adc9919b9bbe4bc80d4ed44043d1ca13aef1e861ff714d3d274b54657b0068a42b5adbfbd5e6737b36435329a2e651296fb359a41a82
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in i18n_lazy_scope.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Mohamad El-Husseini
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # I18nLazyScope
2
+
3
+ I18nLazyScope is a tiny library that lets you use lazy lookup and keep a better structure for your locale files when localising strings in your Ruby applications. This means quicker translations without sacrificing the structure of your locale files.
4
+
5
+ ```ruby
6
+ # users_controller#create
7
+ redirect_to @user, notice: scoped_t('welcome_message')
8
+ ```
9
+
10
+ ```yaml
11
+ en:
12
+ controllers: # <- Oh, look, it's a controller scope with lazy lookup! Yay!
13
+ users:
14
+ create:
15
+ welcome_message: "You are such a star!"
16
+ ```
17
+
18
+ I18nLazyScope provides a wrapper method around the `translate` and `t` methods in Rails and the Ruby [I18n][3] gem.
19
+
20
+ ## What Problem Does This Solve
21
+
22
+ Imagine you are in the `users_controller#show` action, and you want to flash a localised welcome message.
23
+
24
+ ```ruby
25
+ def create
26
+ if @user.save
27
+ redirect_to @user, notice: t('welcome_message')
28
+ end
29
+ end
30
+ ```
31
+
32
+ This scopes the translation to `locale.welcome_message`. If your current--or default--locale is English, the corresponding key in your locale file would be `en.welcome_message`.
33
+
34
+ Oh dear! This is an awful way to structure translations. You want to scope them instead. You create a key in your locale file.
35
+
36
+ ```yaml
37
+ en:
38
+ controllers:
39
+ users:
40
+ create:
41
+ welcome_message: "You are such a star!"
42
+ ```
43
+
44
+ And you add the key to your code.
45
+
46
+ ```ruby
47
+ redirect_to @user, notice: t('controllers.users.create.welcome_message')
48
+ ```
49
+
50
+ Holy Semantics, Batman! `controllers.users.create` is a lot to type before each key name. And after some time you tire of typing it repeatedly. You read the documentation and discover [lazy lookup][1]--brilliant. You decide to use it, and diligently place a dot before your key name.
51
+
52
+ ```ruby
53
+ redirect_to @user, notice: t('.welcome_message')
54
+ ```
55
+
56
+ Soon after, when testing, you discover that you have a `translation missing` error in your template. This is because lazy lookup will scope the translation to `en.users.create.welcome_message`. The neat `controller` namespace we created is missing.
57
+
58
+ You have two choices:
59
+
60
+ 1. Change the structure of your locale file to match how lazy lookup works.
61
+ 2. Use verbose, fully qualified scopes.
62
+
63
+ Option one might cause your locale file to become a mess, or worse, cause namespace collisions because views and controllers share part of the namespace: `en.users`. And option two is annoying to type all the time.
64
+
65
+ **I18nLazyScope** lets you use lazy lookup and keep a better structure for your locale files.
66
+
67
+ ```erb
68
+ redirect_to @user, notice: scoped_t('welcome_message')
69
+
70
+ ```
71
+
72
+ ```yaml
73
+ en:
74
+ controllers: # or mailers, views, or, whatever you want
75
+ users:
76
+ create:
77
+ welcome_message: "You are such a star!"
78
+ ```
79
+
80
+ ## Installation
81
+
82
+ Add this line to your application's Gemfile:
83
+
84
+ ```ruby
85
+ gem 'i18n_lazy_scope'
86
+ ```
87
+
88
+ And then execute:
89
+
90
+ $ bundle
91
+
92
+ Or install it yourself as:
93
+
94
+ $ gem install i18n_lazy_scope
95
+
96
+ ## Usage
97
+
98
+ Call the `t_scoped` method instead of `t`, or `translate`, and make sure you have the corresponding keys in your locale file. Say you are in `app/views/users/show.html.erb`.
99
+
100
+ ```erb
101
+ <%= t_scoped 'weclome' %>
102
+ ```
103
+
104
+ ```yaml
105
+ en:
106
+ views:
107
+ users:
108
+ show:
109
+ greeting: "Hello!"
110
+ ```
111
+
112
+ ### Interpolation
113
+
114
+ It works exactly as it would if you call `t` or `translate`.
115
+
116
+ ```erb
117
+ <%= t_scoped 'weclome', name: @user.name %>
118
+ ```
119
+
120
+ ```yaml
121
+ en:
122
+ views:
123
+ users:
124
+ show:
125
+ greeting: "Hello, %{name}!"
126
+ ```
127
+
128
+ ### Scoping
129
+
130
+ If you have to customise the scope on individual basis, then you should use `t` and `translate` that ship with the [I18n][3] gem. Scoping on individual basis defeates the point of this gem. This gem isn't meant to replace the I18n; it's a tiny wrapper that depends on it. But in future releases you will be able to customise top level namespaces, such as `views`, `controllers`, `mailers`, etc.
131
+
132
+ ## API
133
+
134
+ ```ruby
135
+ t_scoped(key, **args)
136
+ ```
137
+
138
+ ## A Note on Ruby Versions
139
+
140
+ I18nLazyScope requires Ruby 2.0 because it uses the [double splat `**` operator][2] to capture all keyword arguments.
141
+
142
+ ## Contributing
143
+
144
+ 1. Fork it ( https://github.com/abitdodgy/i18n_lazy_scope/fork )
145
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
146
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
147
+ 4. Push to the branch (`git push origin my-new-feature`)
148
+ 5. Create a new Pull Request
149
+
150
+ [1]: http://guides.rubyonrails.org/i18n.html#lazy-lookup
151
+ [2]: http://stackoverflow.com/questions/18289152/what-does-a-double-splat-operator-do
152
+ [3]: https://github.com/svenfuchs/rails-i18n
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ task :console do
9
+ exec "irb -r i18n_lazy_scope -I ./lib"
10
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'i18n_lazy_scope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i18n_lazy_scope"
8
+ spec.version = I18nLazyScope::VERSION
9
+ spec.authors = ["Mohamad El-Husseini"]
10
+ spec.email = ["husseini.mel@gmail.com"]
11
+ spec.summary = %q{Use lazy lookup with custom scopes in your locale files.}
12
+ spec.description = %q{I18nLazyScope lets you use lazy lookup and keep a better structure for your locale files when localising strings in your Ruby on Rails application. This means quicker translations without sacrificing the structure of your locale files.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "i18n"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,10 @@
1
+ require "i18n_lazy_scope/version"
2
+ require "i18n_lazy_scope/railtie" if defined?(Rails)
3
+
4
+ begin
5
+ require "pry"
6
+ rescue LoadError
7
+ end
8
+
9
+ module I18nLazyScope
10
+ end
@@ -0,0 +1,7 @@
1
+ module I18nLazyScope::ActionController
2
+ module Helper
3
+ def lazy_scope
4
+ [:controllers, controller_name, action_name]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module I18nLazyScope::ActionMailer
2
+ module Helper
3
+ def lazy_scope
4
+ [:mailers, mailer_name, action_name]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module I18nLazyScope::ActionView
2
+ module Helper
3
+ def t_scoped(key, **args)
4
+ t(key, scope: lazy_scope(key), **args)
5
+ end
6
+
7
+ def lazy_scope(key)
8
+ [:views, view_path]
9
+ end
10
+
11
+ private
12
+
13
+ def view_path
14
+ if @virtual_path
15
+ @virtual_path.gsub(%r{/_?}, ".")
16
+ else
17
+ raise "Cannot use t(#{key.inspect}) lazy lookup because path is not available"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module I18nLazyScope
2
+ module Helper
3
+ def t_scoped(key, **args)
4
+ t(key, scope: lazy_scope, **args)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ require "rails"
2
+
3
+ require "i18n_lazy_scope/action_controller/helper"
4
+ require "i18n_lazy_scope/action_mailer/helper"
5
+ require "i18n_lazy_scope/action_view/helper"
6
+
7
+ require "i18n_lazy_scope/helper"
8
+
9
+ module I18nLazyScope
10
+ class Railtie < Rails::Railtie
11
+ initializer "i18n_lazy_scope.helpers" do
12
+ ActiveSupport.on_load(:action_controller) do
13
+ include I18nLazyScope::Helper
14
+ include I18nLazyScope::ActionController::Helper
15
+ end
16
+
17
+ ActiveSupport.on_load(:action_mailer) do
18
+ include I18nLazyScope::Helper
19
+ include I18nLazyScope::ActionMailer::Helper
20
+ end
21
+
22
+ ActiveSupport.on_load(:action_view) do
23
+ include I18nLazyScope::ActionView::Helper
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module I18nLazyScope
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ class FakeController
4
+ include I18nLazyScope::ActionController::Helper
5
+ include I18nLazyScope::Helper
6
+
7
+ def t(*args)
8
+ I18n.t(*args)
9
+ end
10
+
11
+ def controller_name; :users; end
12
+ def action_name; :show; end
13
+ end
14
+
15
+ describe FakeController do
16
+ before :each do
17
+ @controller = FakeController.new
18
+ end
19
+
20
+ describe "#t_scoped" do
21
+ it "delegates to t" do
22
+ expect(@controller).to receive(:t)
23
+ @controller.t_scoped(:key)
24
+ end
25
+
26
+ it "returns correct key with scope" do
27
+ string = @controller.t_scoped(:hello)
28
+ expect(string).to eq('World!')
29
+ end
30
+
31
+ it "interpolates passed variables" do
32
+ string = @controller.t_scoped(:greet, name: 'Mohamad', greeting: 'Hello')
33
+ expect(string).to eq('Hello, Mohamad!')
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ class FakeMailer
4
+ include I18nLazyScope::ActionMailer::Helper
5
+ include I18nLazyScope::Helper
6
+
7
+ def t(*args)
8
+ I18n.t(*args)
9
+ end
10
+
11
+ def mailer_name; :user_mailer; end
12
+ def action_name; :welcome_email; end
13
+ end
14
+
15
+ describe FakeMailer do
16
+ before :each do
17
+ @mailer = FakeMailer.new
18
+ end
19
+
20
+ describe "#t_scoped" do
21
+ it "delegates to t" do
22
+ expect(@mailer).to receive(:t)
23
+ @mailer.t_scoped(:key)
24
+ end
25
+
26
+ it "returns correct key with scope" do
27
+ string = @mailer.t_scoped(:hello)
28
+ expect(string).to eq('World!')
29
+ end
30
+
31
+ it "interpolates passed variables" do
32
+ string = @mailer.t_scoped(:subject, name: 'Mohamad', greeting: 'Hello')
33
+ expect(string).to eq('Hello, Mohamad!')
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ class FakeView
4
+ include I18nLazyScope::ActionView::Helper
5
+
6
+ def initialize
7
+ @virtual_path = 'users.show.user_profile'
8
+ end
9
+
10
+ def t(*args)
11
+ I18n.t(*args)
12
+ end
13
+
14
+ def controller_name; :users; end
15
+ def action_name; :show; end
16
+ end
17
+
18
+ describe FakeView do
19
+ before :each do
20
+ @view = FakeView.new
21
+ end
22
+
23
+ describe "#t_scoped" do
24
+ it "delegates to t" do
25
+ expect(@view).to receive(:t)
26
+ @view.t_scoped(:key)
27
+ end
28
+
29
+ it "returns correct key with scope" do
30
+ string = @view.t_scoped(:hello)
31
+ expect(string).to eq('World!')
32
+ end
33
+
34
+ it "interpolates passed variables" do
35
+ string = @view.t_scoped(:greet, name: 'Mohamad', greeting: 'Hello')
36
+ expect(string).to eq('Hello, Mohamad!')
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ require "i18n_lazy_scope/helper"
2
+ require "i18n_lazy_scope/action_controller/helper"
3
+ require "i18n_lazy_scope/action_mailer/helper"
4
+ require "i18n_lazy_scope/action_view/helper"
5
+
6
+ require "i18n"
7
+
8
+ RSpec.configure do |config|
9
+ config.before :all do
10
+ I18n.load_path = [[File.dirname(__FILE__) + '/support/en.yml']]
11
+ end
12
+
13
+ config.expect_with :rspec do |expectations|
14
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
15
+ end
16
+
17
+ config.mock_with :rspec do |mocks|
18
+ mocks.verify_partial_doubles = true
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ en:
2
+ controllers:
3
+ users:
4
+ show:
5
+ hello: "World!"
6
+ greet: "%{greeting}, %{name}!"
7
+ mailers:
8
+ user_mailer:
9
+ welcome_email:
10
+ hello: "World!"
11
+ subject: "%{greeting}, %{name}!"
12
+ views:
13
+ users:
14
+ show:
15
+ user_profile:
16
+ hello: "World!"
17
+ greet: "%{greeting}, %{name}!"
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_lazy_scope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mohamad El-Husseini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: I18nLazyScope lets you use lazy lookup and keep a better structure for
84
+ your locale files when localising strings in your Ruby on Rails application. This
85
+ means quicker translations without sacrificing the structure of your locale files.
86
+ email:
87
+ - husseini.mel@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - i18n_lazy_scope.gemspec
99
+ - lib/i18n_lazy_scope.rb
100
+ - lib/i18n_lazy_scope/action_controller/helper.rb
101
+ - lib/i18n_lazy_scope/action_mailer/helper.rb
102
+ - lib/i18n_lazy_scope/action_view/helper.rb
103
+ - lib/i18n_lazy_scope/helper.rb
104
+ - lib/i18n_lazy_scope/railtie.rb
105
+ - lib/i18n_lazy_scope/version.rb
106
+ - spec/i18n_lazy_scope/action_controller/helper_spec.rb
107
+ - spec/i18n_lazy_scope/action_mailer/helper_spec.rb
108
+ - spec/i18n_lazy_scope/action_view/helper_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/en.yml
111
+ homepage: ''
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Use lazy lookup with custom scopes in your locale files.
135
+ test_files:
136
+ - spec/i18n_lazy_scope/action_controller/helper_spec.rb
137
+ - spec/i18n_lazy_scope/action_mailer/helper_spec.rb
138
+ - spec/i18n_lazy_scope/action_view/helper_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/support/en.yml