i18n_multitenant 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4554661f81cd76709a2fdd32fd9eefb348941870
4
- data.tar.gz: dbc66eb9a981b1991c391d5d2ff3800abdf3467d
3
+ metadata.gz: df671c97a3bab010dc11c20ae0843a32829be66a
4
+ data.tar.gz: 4048ac20539337ae5e6f6b3210ac64d924ea3fee
5
5
  SHA512:
6
- metadata.gz: fc570d3d1f4f828822721b3cb478408137e5ac6f804903e7da44a0190d29b48bb4d9ce4945f6cfc4c796c6bca7cb7088124241d652389d013ca2c32449af23f1
7
- data.tar.gz: bef4b20e3361d1c6eb176e0f9898042fb159b0577713eddebbe9d755c16dc2c36e6f41624f9a11e044dd6f77116cfe0eea0c0a56d1a30c182fb7e6d5b66796f1
6
+ metadata.gz: 37764caf34088612a0764abb9a9143422712ecfdf58f17a6253ba6024acf183938998d48bb4e2a382bb1e1915b55121bfd592b619250343bf244a9d637fc7e94
7
+ data.tar.gz: bbd601c72ccb03bfe84cb3687178e0abb3bfd7a510a7f5f7b8edd14c2322f1d1a55f06d51b238fc93b83d546ebb6cf82c250d92f23fc17094c586ada8d1b833f
@@ -1,3 +1,7 @@
1
- ## I18nMultitenant 1.0.0 (2017-01-11) ##
1
+ ## I18nMultitenant 0.0.2 (2017-01-26) ##
2
+
3
+ * Added `I18nMultitenant#with_locale`. Fixed non-Rails compatibility issue.
4
+
5
+ ## I18nMultitenant 0.0.1 (2017-01-11) ##
2
6
 
3
7
  * Initial Version.
data/README.md CHANGED
@@ -1,4 +1,11 @@
1
- # i18n Multi-Tenant
1
+ i18n Multi-Tenant
2
+ ====================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/i18n_multitenant.svg)](http://badge.fury.io/rb/i18n_multitenant)
5
+ [![Build Status](https://travis-ci.org/ElMassimo/i18n_multitenant.svg)](https://travis-ci.org/ElMassimo/i18n_multitenant)
6
+ [![Test Coverage](https://codeclimate.com/github/ElMassimo/i18n_multitenant/badges/coverage.svg)](https://codeclimate.com/github/ElMassimo/i18n_multitenant/coverage)
7
+ [![Code Climate](https://codeclimate.com/github/ElMassimo/i18n_multitenant/badges/gpa.svg)](https://codeclimate.com/github/ElMassimo/i18n_multitenant)
8
+ [![License](https://img.shields.io/badge/license-MIT-blue.svg)](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
+
@@ -20,19 +20,37 @@ module I18nMultitenant
20
20
  #
21
21
  # I18nMultitenant.set(locale: :es)
22
22
  # => :es
23
- def self.set(locale: I18n.default_locale, tenant: nil)
24
- unless tenant.nil? || tenant.empty?
25
- locale = "#{ locale }-#{ tenant.to_s.upcase.tr(' .-', '_') }"
26
- end
23
+ def self.set(options)
24
+ I18n.locale = locale_for(options)
25
+ end
27
26
 
28
- I18n.locale = locale
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
- config.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
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
 
@@ -1,5 +1,5 @@
1
1
  class I18nMultitenant::Railtie < ::Rails::Railtie
2
- config.before_initialize do |app|
2
+ config.before_initialize do
3
3
  I18nMultitenant.configure(I18n)
4
4
  end
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module I18nMultitenant
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -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
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
- if ENV['CODECLIMATE_REPO_TOKEN']
3
- require 'codeclimate-test-reporter'
4
- CodeClimate::TestReporter.start
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
5
  end
6
6
 
7
7
  require 'rspec/given'
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.1
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-12 00:00:00.000000000 Z
11
+ date: 2017-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n