i18n_multitenant 0.0.1 → 0.0.2
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 +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +19 -1
- data/lib/i18n_multitenant.rb +24 -6
- data/lib/i18n_multitenant/railtie.rb +1 -1
- data/lib/i18n_multitenant/version.rb +1 -1
- data/spec/lib/i18n_multitenant_spec.rb +21 -0
- data/spec/spec_helper.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df671c97a3bab010dc11c20ae0843a32829be66a
|
4
|
+
data.tar.gz: 4048ac20539337ae5e6f6b3210ac64d924ea3fee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37764caf34088612a0764abb9a9143422712ecfdf58f17a6253ba6024acf183938998d48bb4e2a382bb1e1915b55121bfd592b619250343bf244a9d637fc7e94
|
7
|
+
data.tar.gz: bbd601c72ccb03bfe84cb3687178e0abb3bfd7a510a7f5f7b8edd14c2322f1d1a55f06d51b238fc93b83d546ebb6cf82c250d92f23fc17094c586ada8d1b833f
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
-
|
1
|
+
i18n Multi-Tenant
|
2
|
+
====================
|
3
|
+
|
4
|
+
[](http://badge.fury.io/rb/i18n_multitenant)
|
5
|
+
[](https://travis-ci.org/ElMassimo/i18n_multitenant)
|
6
|
+
[](https://codeclimate.com/github/ElMassimo/i18n_multitenant/coverage)
|
7
|
+
[](https://codeclimate.com/github/ElMassimo/i18n_multitenant)
|
8
|
+
[](https://github.com/ElMassimo/i18n_multitenant/blob/master/LICENSE.txt)
|
2
9
|
|
3
10
|
This gem is a small utility that provides the basic configuration to perform
|
4
11
|
tenant-specific translations in multi-tenant apps.
|
@@ -57,3 +64,14 @@ the configuration yourself in other applications by calling:
|
|
57
64
|
```ruby
|
58
65
|
I18nMultitenant.configure(I18n) # or pass I18n.config
|
59
66
|
```
|
67
|
+
|
68
|
+
## Front-end Translations
|
69
|
+
If you also need to perform translations in the front-end, you can use a library
|
70
|
+
like [`i18n-js`](https://github.com/fnando/i18n-js). You can easily configure
|
71
|
+
`i18n-js` to support multi-tenant translations by leveraging `i18n` itself:
|
72
|
+
|
73
|
+
```erb
|
74
|
+
I18n.locale = <%= I18n.locale.to_json.html_safe %>;
|
75
|
+
I18n.locales[I18n.locale] = <%= I18n.fallbacks[I18n.locale].to_json.html_safe %>
|
76
|
+
```
|
77
|
+
|
data/lib/i18n_multitenant.rb
CHANGED
@@ -20,19 +20,37 @@ module I18nMultitenant
|
|
20
20
|
#
|
21
21
|
# I18nMultitenant.set(locale: :es)
|
22
22
|
# => :es
|
23
|
-
def self.set(
|
24
|
-
|
25
|
-
|
26
|
-
end
|
23
|
+
def self.set(options)
|
24
|
+
I18n.locale = locale_for(options)
|
25
|
+
end
|
27
26
|
|
28
|
-
|
27
|
+
# Public: Executes block using the specified locale configuration, restoring
|
28
|
+
# it after the block is executed.
|
29
|
+
def self.with_locale(options)
|
30
|
+
I18n.with_locale(locale_for(options)) { yield }
|
31
|
+
end
|
32
|
+
|
33
|
+
# Internal: Get the internal locale for a tenant-specific locale.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
# I18nMultitenant.locale_for(locale: :en, tenant: 'Veridian Dynamics')
|
37
|
+
# => "en-VERIDIAN_DYNAMICS"
|
38
|
+
def self.locale_for(locale: I18n.default_locale, tenant: nil)
|
39
|
+
if tenant && !tenant.to_s.empty?
|
40
|
+
"#{ locale }-#{ tenant.to_s.upcase.tr(' .-', '_') }"
|
41
|
+
else
|
42
|
+
locale
|
43
|
+
end
|
29
44
|
end
|
30
45
|
|
31
46
|
# Public: Configures an instance of I18n::Config to ensure fallbacks are setup.
|
32
47
|
def self.configure(config, enforce_available_locales: false)
|
33
48
|
config.enforce_available_locales = enforce_available_locales
|
34
49
|
config.backend.class.send(:include, I18n::Backend::Fallbacks)
|
35
|
-
|
50
|
+
|
51
|
+
if defined?(Rails.root)
|
52
|
+
config.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
|
53
|
+
end
|
36
54
|
end
|
37
55
|
end
|
38
56
|
|
@@ -11,6 +11,11 @@ RSpec.describe I18nMultitenant do
|
|
11
11
|
}
|
12
12
|
|
13
13
|
describe 'set' do
|
14
|
+
context 'passing an incorrect option' do
|
15
|
+
When(:result) { I18nMultitenant.set(locales: [:en, :es]) }
|
16
|
+
Then { expect(result).to have_failed(ArgumentError, /unknown keyword: locales/) }
|
17
|
+
end
|
18
|
+
|
14
19
|
context 'setting only the locale' do
|
15
20
|
When(:result) { I18nMultitenant.set(locale: locale) }
|
16
21
|
|
@@ -78,6 +83,22 @@ RSpec.describe I18nMultitenant do
|
|
78
83
|
end
|
79
84
|
end
|
80
85
|
|
86
|
+
describe 'with_locale' do
|
87
|
+
context 'is restored afterwards' do
|
88
|
+
Given {
|
89
|
+
I18n.locale = :en
|
90
|
+
}
|
91
|
+
When(:result) {
|
92
|
+
I18nMultitenant.with_locale(locale: :es, tenant: 'Free') {
|
93
|
+
t(:app_name)
|
94
|
+
}
|
95
|
+
}
|
96
|
+
Then { result == 'I18n (versión gratuita)' }
|
97
|
+
And { I18n.locale == :en }
|
98
|
+
And { t(:app_name) == 'I18n' }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
81
102
|
describe 'translation' do
|
82
103
|
context 'setting the locale directly' do
|
83
104
|
context 'english' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n_multitenant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|