i18n_lazy_scope 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -60
- data/lib/i18n_lazy_scope.rb +13 -0
- data/lib/i18n_lazy_scope/action_controller/helper.rb +1 -1
- data/lib/i18n_lazy_scope/action_mailer/helper.rb +1 -1
- data/lib/i18n_lazy_scope/action_view/helper.rb +1 -1
- data/lib/i18n_lazy_scope/configuration.rb +11 -0
- data/lib/i18n_lazy_scope/version.rb +1 -1
- data/spec/i18n_lazy_scope/action_controller/helper_spec.rb +0 -12
- data/spec/i18n_lazy_scope/action_mailer/helper_spec.rb +0 -12
- data/spec/i18n_lazy_scope/action_view/helper_spec.rb +0 -16
- data/spec/i18n_lazy_scope/configuration_spec.rb +53 -0
- data/spec/i18n_lazy_scope/i18n_lazy_scope_spec.rb +30 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/fake_controller.rb +16 -0
- data/spec/support/fake_mailer.rb +16 -0
- data/spec/support/fake_view.rb +20 -0
- metadata +13 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b293b5d8ce8b2d9b36e0e6aa0f0bc5089782123
|
4
|
+
data.tar.gz: cd63978ec85c128b6386140a40f09d37d2cbdccb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 016a2424f4540b1fdbc24d5822bdb46075b0f5dcf5e217b1a350d79c0024ba09acfc00e2c42f9f3ae3c7337b14f6c59f2d29cb41281c447b6202224241ef55b3
|
7
|
+
data.tar.gz: a9ebaad147eead7f833fbc4b2d3bdb1b4646b2e966481b492301d7952d0b84224cb01d9ba3709adc019c64d71e62887c0e0bbe0c010a8c5fb0f2c6eed32c8153
|
data/README.md
CHANGED
@@ -1,81 +1,57 @@
|
|
1
1
|
# I18nLazyScope
|
2
2
|
|
3
|
-
I18nLazyScope is a tiny library that lets you use lazy lookup and
|
3
|
+
I18nLazyScope is a tiny library that lets you use lazy lookup and custom scoping in your locale files when localising strings in your Ruby applications. This means quicker translations without sacrificing the structure of your locale files.
|
4
4
|
|
5
|
-
|
6
|
-
# users_controller#create
|
7
|
-
redirect_to @user, notice: scoped_t('welcome_message')
|
8
|
-
```
|
5
|
+
The library inserts a customisable namespace in the scope for you, just below the locale. The following table illustrates the difference between I18nLazyLookup and Rails when resolving lazy lookup keys.
|
9
6
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
welcome_message: "You are such a star!"
|
16
|
-
```
|
7
|
+
| | I18nLazyLookup | Rails/I18n Lazy Lookup
|
8
|
+
| ------------|------------------------------------------------------|------------------------------------------|
|
9
|
+
| Controllers | `locale.controllers.controller_name.action_name.key` | `locale.controller_name.action_name.key` |
|
10
|
+
| Mailers | `locale.mailers.mailer_name.action_name.key` | `locale.mailer_name.action_name.key` |
|
11
|
+
| Views | `locale.views.template_or_partial_path.key` | `locale.template_or_partial_path.key` |
|
17
12
|
|
18
|
-
I18nLazyScope provides a wrapper method around the `translate` and `t` methods in Rails and the Ruby [I18n][3] gem.
|
19
13
|
|
20
|
-
## What
|
14
|
+
## What's Lazy Lookup?
|
21
15
|
|
22
|
-
|
16
|
+
[Lazy lookup][1] is a feature that ships with Rails and the [I18n][3] gem. It allows you to translate strings without explicitly qualifying their entire scope. For example, consider the following code that lives in `show.html.erb` inside `app/views/users`, and that your application's locale is English.
|
23
17
|
|
24
|
-
```
|
25
|
-
|
26
|
-
if @user.save
|
27
|
-
redirect_to @user, notice: t('welcome_message')
|
28
|
-
end
|
29
|
-
end
|
18
|
+
```erb
|
19
|
+
<%= t('.welcome_message') %>
|
30
20
|
```
|
31
21
|
|
32
|
-
|
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.
|
22
|
+
Rails automatically converts `.welcome_message` to `en.users.show.welcome_message`. This saves you from having to type `users.show`, which is handy if you have lots of translations. But it forces you structure your locale files the way Rails prefers.
|
35
23
|
|
36
24
|
```yaml
|
37
25
|
en:
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
welcome_message: "You are such a star!"
|
26
|
+
users:
|
27
|
+
show:
|
28
|
+
welcome_message: "You are such a star!"
|
42
29
|
```
|
43
30
|
|
44
|
-
|
31
|
+
The above structure can get messy because `en.users` can refer to a view or a controller, and it feels wrong for `users` to be floating under the top level namespace. Most developers prefer to keep their translations under a scope.
|
45
32
|
|
46
|
-
```
|
47
|
-
|
33
|
+
```yaml
|
34
|
+
en:
|
35
|
+
controllers:
|
36
|
+
users:
|
37
|
+
show:
|
38
|
+
success: "Yay!"
|
39
|
+
views:
|
40
|
+
users:
|
41
|
+
show:
|
42
|
+
welcome_message: "Yay!"
|
48
43
|
```
|
49
44
|
|
50
|
-
|
45
|
+
So applications with lots of translations will find it hard to take advantage of lazy lookup. Wouldn't it be great if you could use lazy lookup and a custom namespace? Well, now you can.
|
51
46
|
|
52
|
-
```
|
53
|
-
|
47
|
+
```erb
|
48
|
+
<%= t_scoped('welcome_message') %>
|
54
49
|
```
|
55
50
|
|
56
|
-
|
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.
|
51
|
+
The `scoped_t` method will automatically convert `welcome_message` to `en.views.show.welcome_message`. And you can customise the `views` part of the scope to anything you like. Your locale file would contain the `views` namespace to keep view translations organised as demonstrated above.
|
62
52
|
|
63
|
-
|
53
|
+
I18nLazyScope provides a wrapper method around the `translate` and `t` methods in Rails and the Ruby I18n gem.
|
64
54
|
|
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
55
|
|
80
56
|
## Installation
|
81
57
|
|
@@ -95,7 +71,7 @@ Or install it yourself as:
|
|
95
71
|
|
96
72
|
## Usage
|
97
73
|
|
98
|
-
Call the `t_scoped` method instead of `t`, or `translate`, and make sure you have the corresponding keys in your locale
|
74
|
+
Call the `t_scoped` method instead of `t`, or `translate`, and make sure you have the corresponding keys in your locale files. Say you are in `app/views/users/show.html.erb`.
|
99
75
|
|
100
76
|
```erb
|
101
77
|
<%= t_scoped 'greeting' %>
|
@@ -109,12 +85,27 @@ en:
|
|
109
85
|
greeting: "Hello!"
|
110
86
|
```
|
111
87
|
|
112
|
-
The library inserts a top level name in the scope for you.
|
88
|
+
The library inserts a top level name in the scope for you. Here are the defaults:
|
113
89
|
|
114
90
|
1. Conrollers: `locale.controllers.controller_name.action_name.key`
|
115
91
|
2. Mailers: `locale.mailers.mailer_name.action_name.key`
|
116
92
|
3. Views: `locale.views.template_or_partial_path.key`
|
117
93
|
|
94
|
+
### Customising the Namespace
|
95
|
+
|
96
|
+
I18nLazyScope accepts a configuration block. This is an example that you might put into a Rails initializer at `config/initializers/i18n_lazy_scope.rb`:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
I18nLazyScope.configure do |config|
|
100
|
+
# Resolves lazy lookup to `locale.my.custom.scope.controller_name.action_name.key`
|
101
|
+
config.action_controller_scope = [:my, :custom, :scope]
|
102
|
+
# Resolves lazy lookup to `locale.my.custom.scope.mailer_name.action_name.key`
|
103
|
+
config.action_mailer_scope = [:my, :custom, :scope]
|
104
|
+
# Resolves lazy lookup to `locale.my.custom.scope.template_or_partial_path.key`
|
105
|
+
config.action_view_scope = [:my, :custom, :scope]
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
118
109
|
### Interpolation
|
119
110
|
|
120
111
|
It works exactly as it would if you call `t` or `translate`.
|
@@ -131,12 +122,10 @@ en:
|
|
131
122
|
greeting: "Hello, %{name}!"
|
132
123
|
```
|
133
124
|
|
134
|
-
### Scoping
|
125
|
+
### A Note on Scoping
|
135
126
|
|
136
127
|
If you have to customise the scope on individual basis, then you should use `t` and `translate` that ship with Rails or 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.
|
137
128
|
|
138
|
-
In future releases you will be able to customise top level namespaces, such as `views`, `controllers`, `mailers`, etc.
|
139
|
-
|
140
129
|
## API
|
141
130
|
|
142
131
|
```ruby
|
@@ -147,6 +136,10 @@ t_scoped(key, **args)
|
|
147
136
|
|
148
137
|
I18nLazyScope requires Ruby 2.0 because it uses the [double splat `**` operator][2] to capture all keyword arguments.
|
149
138
|
|
139
|
+
## Version
|
140
|
+
|
141
|
+
I18nLazyScope uses semantic versioning.
|
142
|
+
|
150
143
|
## Contributing
|
151
144
|
|
152
145
|
1. Fork it ( https://github.com/abitdodgy/i18n_lazy_scope/fork )
|
data/lib/i18n_lazy_scope.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require "i18n_lazy_scope/version"
|
2
|
+
require "i18n_lazy_scope/configuration"
|
3
|
+
|
2
4
|
require "i18n_lazy_scope/railtie" if defined?(Rails)
|
3
5
|
|
4
6
|
begin
|
@@ -7,4 +9,15 @@ rescue LoadError
|
|
7
9
|
end
|
8
10
|
|
9
11
|
module I18nLazyScope
|
12
|
+
class << self
|
13
|
+
attr_writer :configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configuration
|
17
|
+
@configuration ||= Configuration.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.configure
|
21
|
+
yield(configuration)
|
22
|
+
end
|
10
23
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module I18nLazyScope
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :action_controller_scope, :action_mailer_scope, :action_view_scope
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@action_controller_scope = [:controllers]
|
7
|
+
@action_mailer_scope = [:mailers]
|
8
|
+
@action_view_scope = [:views]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -1,17 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
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
3
|
describe FakeController do
|
16
4
|
before :each do
|
17
5
|
@controller = FakeController.new
|
@@ -1,17 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
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
3
|
describe FakeMailer do
|
16
4
|
before :each do
|
17
5
|
@mailer = FakeMailer.new
|
@@ -1,21 +1,5 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
class FakeView
|
4
|
-
include I18nLazyScope::ActionView::Helper
|
5
|
-
include I18nLazyScope::Helper
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@virtual_path = 'users.show.user_profile'
|
9
|
-
end
|
10
|
-
|
11
|
-
def t(*args)
|
12
|
-
I18n.t(*args)
|
13
|
-
end
|
14
|
-
|
15
|
-
def controller_name; :users; end
|
16
|
-
def action_name; :show; end
|
17
|
-
end
|
18
|
-
|
19
3
|
describe FakeView do
|
20
4
|
before :each do
|
21
5
|
@view = FakeView.new
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module I18nLazyScope
|
4
|
+
describe Configuration do
|
5
|
+
describe "#action_controller_scope" do
|
6
|
+
it "defaults to [:controllers]" do
|
7
|
+
scope = Configuration.new.action_controller_scope
|
8
|
+
expect(scope).to eq([:controllers])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#action_controller_scope=" do
|
13
|
+
it "sets a value" do
|
14
|
+
config = Configuration.new
|
15
|
+
config.action_controller_scope = [:my, :controller, :scope]
|
16
|
+
scope = config.action_controller_scope
|
17
|
+
expect(scope).to eq([:my, :controller, :scope])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#action_mailer_scope" do
|
22
|
+
it "defaults to [:mailers]" do
|
23
|
+
scope = Configuration.new.action_mailer_scope
|
24
|
+
expect(scope).to eq([:mailers])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#action_mailer_scope=" do
|
29
|
+
it "sets a value" do
|
30
|
+
config = Configuration.new
|
31
|
+
config.action_mailer_scope = [:my, :mailer, :scope]
|
32
|
+
scope = config.action_mailer_scope
|
33
|
+
expect(scope).to eq([:my, :mailer, :scope])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#action_view_scope" do
|
38
|
+
it "defaults to [:views]" do
|
39
|
+
scope = Configuration.new.action_view_scope
|
40
|
+
expect(scope).to eq([:views])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#action_view_scope=" do
|
45
|
+
it "sets a value" do
|
46
|
+
config = Configuration.new
|
47
|
+
config.action_view_scope = [:my, :view, :scope]
|
48
|
+
scope = config.action_view_scope
|
49
|
+
expect(scope).to eq([:my, :view, :scope])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module I18nLazyScope
|
4
|
+
describe I18nLazyScope do
|
5
|
+
describe "#configure" do
|
6
|
+
before do
|
7
|
+
I18nLazyScope.configure do |config|
|
8
|
+
config.action_controller_scope = %i(my scope)
|
9
|
+
config.action_mailer_scope = %i(my scope)
|
10
|
+
config.action_view_scope = %i(my scope)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets custom controller scope" do
|
15
|
+
controller = FakeController.new
|
16
|
+
expect(controller.lazy_scope).to eq [:my, :scope, :users, :show]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "sets custom mailer scope" do
|
20
|
+
mailer = FakeMailer.new
|
21
|
+
expect(mailer.lazy_scope).to eq [:my, :scope, :user_mailer, :welcome_email]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sets custom view scope" do
|
25
|
+
view = FakeView.new
|
26
|
+
expect(view.lazy_scope).to eq [:my, :scope, "users.show.user_profile"]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
|
+
require "i18n_lazy_scope"
|
2
|
+
|
1
3
|
require "i18n_lazy_scope/action_controller/helper"
|
2
4
|
require "i18n_lazy_scope/action_mailer/helper"
|
3
5
|
require "i18n_lazy_scope/action_view/helper"
|
4
6
|
|
5
7
|
require "i18n_lazy_scope/helper"
|
8
|
+
require "i18n_lazy_scope/configuration"
|
9
|
+
|
10
|
+
require "support/fake_controller"
|
11
|
+
require "support/fake_mailer"
|
12
|
+
require "support/fake_view"
|
6
13
|
|
7
14
|
require "i18n"
|
8
15
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class FakeView
|
2
|
+
include I18nLazyScope::ActionView::Helper
|
3
|
+
include I18nLazyScope::Helper
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@virtual_path = 'users.show.user_profile'
|
7
|
+
end
|
8
|
+
|
9
|
+
def t(*args)
|
10
|
+
I18n.t(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def controller_name
|
14
|
+
:users
|
15
|
+
end
|
16
|
+
|
17
|
+
def action_name
|
18
|
+
:show
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n_lazy_scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mohamad El-Husseini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,14 +100,20 @@ files:
|
|
100
100
|
- lib/i18n_lazy_scope/action_controller/helper.rb
|
101
101
|
- lib/i18n_lazy_scope/action_mailer/helper.rb
|
102
102
|
- lib/i18n_lazy_scope/action_view/helper.rb
|
103
|
+
- lib/i18n_lazy_scope/configuration.rb
|
103
104
|
- lib/i18n_lazy_scope/helper.rb
|
104
105
|
- lib/i18n_lazy_scope/railtie.rb
|
105
106
|
- lib/i18n_lazy_scope/version.rb
|
106
107
|
- spec/i18n_lazy_scope/action_controller/helper_spec.rb
|
107
108
|
- spec/i18n_lazy_scope/action_mailer/helper_spec.rb
|
108
109
|
- spec/i18n_lazy_scope/action_view/helper_spec.rb
|
110
|
+
- spec/i18n_lazy_scope/configuration_spec.rb
|
111
|
+
- spec/i18n_lazy_scope/i18n_lazy_scope_spec.rb
|
109
112
|
- spec/spec_helper.rb
|
110
113
|
- spec/support/en.yml
|
114
|
+
- spec/support/fake_controller.rb
|
115
|
+
- spec/support/fake_mailer.rb
|
116
|
+
- spec/support/fake_view.rb
|
111
117
|
homepage: ''
|
112
118
|
licenses:
|
113
119
|
- MIT
|
@@ -136,5 +142,10 @@ test_files:
|
|
136
142
|
- spec/i18n_lazy_scope/action_controller/helper_spec.rb
|
137
143
|
- spec/i18n_lazy_scope/action_mailer/helper_spec.rb
|
138
144
|
- spec/i18n_lazy_scope/action_view/helper_spec.rb
|
145
|
+
- spec/i18n_lazy_scope/configuration_spec.rb
|
146
|
+
- spec/i18n_lazy_scope/i18n_lazy_scope_spec.rb
|
139
147
|
- spec/spec_helper.rb
|
140
148
|
- spec/support/en.yml
|
149
|
+
- spec/support/fake_controller.rb
|
150
|
+
- spec/support/fake_mailer.rb
|
151
|
+
- spec/support/fake_view.rb
|