multi_formal_i18n_tenancy 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ class MultiFormalI18nTenancy::Backend < I18n::Backend::Simple
|
|
6
6
|
TENANT_LOCALE_PATTERN = /tenants$/
|
7
7
|
|
8
8
|
attr_accessor :filenames
|
9
|
-
|
9
|
+
|
10
10
|
# Accepts a list of paths to translation files. Loads translations from
|
11
11
|
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
|
12
12
|
# for details.
|
@@ -15,66 +15,7 @@ class MultiFormalI18nTenancy::Backend < I18n::Backend::Simple
|
|
15
15
|
|
16
16
|
@filenames = filenames.flatten
|
17
17
|
|
18
|
-
|
19
|
-
# locale file groups order (ancestor chain)
|
20
|
-
#
|
21
|
-
# a) de > de_formal > your_enterprise_name_de > your_enterprise_name_de_formal
|
22
|
-
# b) de > your_enterprise_name_de
|
23
|
-
|
24
|
-
# de
|
25
|
-
->(f) { !f.match(FORMAL_FILENAME_PATTERN) && !f.match(TENANT_FILENAME_PATTERN) },
|
26
|
-
# de > your_enterprise_name_de
|
27
|
-
->(f) { !f.match(FORMAL_FILENAME_PATTERN) && f.match(TENANT_FILENAME_PATTERN) },
|
28
|
-
# de > de_formal
|
29
|
-
->(f) { f.match(FORMAL_FILENAME_PATTERN) && !f.match(TENANT_FILENAME_PATTERN) },
|
30
|
-
# de > de_formal > your_enterprise_name_de > your_enterprise_name_de_formal
|
31
|
-
->(f) { f.match(FORMAL_FILENAME_PATTERN) && f.match(TENANT_FILENAME_PATTERN) }
|
32
|
-
].each do |filename_filter|
|
33
|
-
filenames.flatten.select{|f| filename_filter.call(f) }.each { |filename| load_file(filename) }
|
34
|
-
end
|
35
|
-
|
36
|
-
@filenames = [] # free memory
|
37
|
-
end
|
38
|
-
|
39
|
-
# Stores translations for the given locale in memory.
|
40
|
-
# This uses a deep merge for the translations hash, so existing
|
41
|
-
# translations will be overwritten by new ones only at the deepest
|
42
|
-
# level of the hash.
|
43
|
-
def store_translations(locale, data, options = {})
|
44
|
-
locale = locale.to_sym
|
45
|
-
|
46
|
-
# the has_key check assures that the inheritance will be performed for the first locale file
|
47
|
-
if (locale.to_s.match(FORMAL_LOCALE_PATTERN) || tenant_from_locale?(locale)) && !translations.has_key?(locale)
|
48
|
-
# inherit keys from base locale file
|
49
|
-
base_locale = locale.to_s
|
50
|
-
tenant = tenant_from_locale?(locale)
|
51
|
-
|
52
|
-
if tenant && locale.to_s.match(FORMAL_LOCALE_PATTERN)
|
53
|
-
# de > de_formal
|
54
|
-
base_locale = locale.to_s.gsub(/^#{tenant}_/, '')
|
55
|
-
translations[locale] = (translations[base_locale.to_sym] || {}).clone
|
56
|
-
|
57
|
-
# de_formal > your_enterprise_name_de
|
58
|
-
base_locale = locale.to_s.gsub(FORMAL_LOCALE_PATTERN, '')
|
59
|
-
base_translations = (translations[base_locale.to_sym] || {}).clone.deep_symbolize_keys
|
60
|
-
|
61
|
-
translations[locale] = base_translations.deep_merge(translations[locale])
|
62
|
-
elsif tenant
|
63
|
-
base_locale.gsub!(/^#{tenant}_/, '')
|
64
|
-
else
|
65
|
-
base_locale.gsub!(FORMAL_LOCALE_PATTERN, '')
|
66
|
-
end
|
67
|
-
|
68
|
-
unless tenant && locale.to_s.match(FORMAL_LOCALE_PATTERN)
|
69
|
-
translations[locale] = (translations[base_locale.to_sym] || {}).clone
|
70
|
-
end
|
71
|
-
else
|
72
|
-
translations[locale] ||= {}
|
73
|
-
end
|
74
|
-
|
75
|
-
data = data.deep_symbolize_keys
|
76
|
-
|
77
|
-
translations[locale].deep_merge!(data)
|
18
|
+
@filenames.each { |filename| load_file(filename) }
|
78
19
|
end
|
79
20
|
|
80
21
|
def available_locale(options = {})
|
@@ -117,7 +58,64 @@ class MultiFormalI18nTenancy::Backend < I18n::Backend::Simple
|
|
117
58
|
deepest_available_locale || I18n.default_locale
|
118
59
|
end
|
119
60
|
|
120
|
-
|
61
|
+
protected
|
62
|
+
|
63
|
+
# Looks up a translation from the translations hash. Returns nil if
|
64
|
+
# eiher key is nil, or locale, scope or key do not exist as a key in the
|
65
|
+
# nested translations hash. Splits keys or scopes containing dots
|
66
|
+
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
|
67
|
+
# <tt>%w(currency format)</tt>.
|
68
|
+
def lookup(locale, key, scope = [], options = {})
|
69
|
+
init_translations unless initialized?
|
70
|
+
|
71
|
+
# only formal address: [:de, :de_formal]
|
72
|
+
# only multi tenancy: [:de, :tenant_name_de]
|
73
|
+
# formal address & multi tenancy: [:de, :tenant_name_de, :de_formal, :tenant_name_de_formal]
|
74
|
+
|
75
|
+
locales = []
|
76
|
+
|
77
|
+
base_locale = locale.to_s.gsub(FORMAL_LOCALE_PATTERN, '')
|
78
|
+
|
79
|
+
tenant = tenant_from_locale?(locale)
|
80
|
+
base_locale.gsub!(/^#{tenant}_/, '') if tenant
|
81
|
+
|
82
|
+
locales << base_locale.to_sym
|
83
|
+
|
84
|
+
if locale.to_s.match(FORMAL_LOCALE_PATTERN) && tenant && locale.to_s.match(/^#{tenant}_/)
|
85
|
+
locales << locale.to_s.gsub(FORMAL_LOCALE_PATTERN, '').to_sym
|
86
|
+
locales << locale.to_s.gsub(/^#{tenant}_/, '').to_sym
|
87
|
+
end
|
88
|
+
|
89
|
+
locales << locale unless locales.include?(locale)
|
90
|
+
|
91
|
+
entry, last_entry = nil, nil
|
92
|
+
|
93
|
+
locales.each do |locale|
|
94
|
+
keys = I18n.normalize_keys(locale, key, scope, options[:separator])
|
95
|
+
|
96
|
+
entry = keys.inject(translations) do |result, _key|
|
97
|
+
_key = _key.to_sym
|
98
|
+
|
99
|
+
unless result.is_a?(Hash) && result.has_key?(_key)
|
100
|
+
nil
|
101
|
+
else
|
102
|
+
result = result[_key]
|
103
|
+
result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
|
104
|
+
result
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
if entry.nil?
|
109
|
+
entry = last_entry
|
110
|
+
else
|
111
|
+
last_entry = entry
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
entry
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
121
119
|
|
122
120
|
def tenant_from_locale?(locale)
|
123
121
|
tenant = locale.to_s.gsub(FORMAL_LOCALE_PATTERN, '').split('_')
|
@@ -131,16 +129,4 @@ class MultiFormalI18nTenancy::Backend < I18n::Backend::Simple
|
|
131
129
|
|
132
130
|
tenant && @filenames.select{|f| f.match("/tenants/#{tenant}/")}.any? ? tenant : nil
|
133
131
|
end
|
134
|
-
|
135
|
-
def delete_right_key_if_left_has_key(left_hash, right_hash)
|
136
|
-
right_hash.each do |key,value|
|
137
|
-
next unless left_hash.has_key?(key)
|
138
|
-
|
139
|
-
if value.is_a?(Hash)
|
140
|
-
delete_right_key_if_left_has_key(left_hash[key], value)
|
141
|
-
else
|
142
|
-
right_hash.delete(key) if left_hash.has_key?(key)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
132
|
end
|
@@ -11,7 +11,7 @@ describe MultiFormalI18nTenancy::Backend do
|
|
11
11
|
I18n.locale = I18n.default_locale
|
12
12
|
end
|
13
13
|
|
14
|
-
describe '#
|
14
|
+
describe '#lookup' do
|
15
15
|
include_context :all_locale_file_constellations
|
16
16
|
|
17
17
|
before :all do
|
@@ -193,25 +193,4 @@ describe MultiFormalI18nTenancy::Backend do
|
|
193
193
|
end
|
194
194
|
end
|
195
195
|
end
|
196
|
-
=begin
|
197
|
-
describe '#delete_right_key_if_left_key' do
|
198
|
-
include_context :all_locale_file_constellations
|
199
|
-
|
200
|
-
it 'principally works' do
|
201
|
-
I18n.backend = MultiFormalI18nTenancy::Backend.new
|
202
|
-
|
203
|
-
left_hash = { key_1: 'value 1', key_2: { key_2_1: 'value 2.1' }}
|
204
|
-
right_hash = { key_1: 'value 2', key_2: { key_2_1: 'value 2.1 a', key_2_2: 'value 2.2' } }
|
205
|
-
|
206
|
-
#I18n.backend.delete_right_key_if_left_key(left_hash, right_hash)
|
207
|
-
|
208
|
-
#left_hash.deep_merge!(right_hash)
|
209
|
-
left_hash = right_hash.deep_merge(left_hash)
|
210
|
-
|
211
|
-
left_hash[:key_1].should == 'value 1'
|
212
|
-
left_hash[:key_2][:key_2_1].should == 'value 2.1'
|
213
|
-
left_hash[:key_2][:key_2_2].should == 'value 2.2'
|
214
|
-
end
|
215
|
-
end
|
216
|
-
=end
|
217
196
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_formal_i18n_tenancy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70201387898160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70201387898160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70201387896560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,9 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.11.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
-
description: Your
|
37
|
-
and locales stored in
|
35
|
+
version_requirements: *70201387896560
|
36
|
+
description: ! 'Your locales about polite form of address will inherit translations
|
37
|
+
from their base locale and locales stored in a tenant directory can override base
|
38
|
+
+ formal translations '
|
38
39
|
email:
|
39
40
|
- gawlista@googlemail.com
|
40
41
|
executables: []
|
@@ -62,7 +63,7 @@ files:
|
|
62
63
|
- spec/spec_helper.rb
|
63
64
|
- spec/support/deferred_garbage_collection.rb
|
64
65
|
- spec/support/shared_contexts/all_locale_file_constellations.rb
|
65
|
-
homepage: http://
|
66
|
+
homepage: http://github.com/Applicat/multi_formal_i18n_tenancy
|
66
67
|
licenses: []
|
67
68
|
post_install_message:
|
68
69
|
rdoc_options: []
|
@@ -76,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
77
|
version: '0'
|
77
78
|
segments:
|
78
79
|
- 0
|
79
|
-
hash:
|
80
|
+
hash: 3959917024077130504
|
80
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
82
|
none: false
|
82
83
|
requirements:
|
@@ -85,14 +86,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
86
|
version: '0'
|
86
87
|
segments:
|
87
88
|
- 0
|
88
|
-
hash:
|
89
|
+
hash: 3959917024077130504
|
89
90
|
requirements: []
|
90
91
|
rubyforge_project:
|
91
92
|
rubygems_version: 1.8.17
|
92
93
|
signing_key:
|
93
94
|
specification_version: 3
|
94
|
-
summary: Your
|
95
|
-
locales stored in
|
95
|
+
summary: Your locales about polite form of address will inherit translations from
|
96
|
+
their base locale and locales stored in a tenant directory can override base + formal
|
97
|
+
translations
|
96
98
|
test_files:
|
97
99
|
- spec/fixtures/de.yml
|
98
100
|
- spec/fixtures/de_formal.yml
|