variantable 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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54a7be5a58db1fb161f28cd75e82168c744f026f1f067099695311583cc00193
|
4
|
+
data.tar.gz: e9b836cf465a6842745da3e0d60c872a6452ca3ad31ed0ca4b63d1617e8e753d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e9cf6100217f314eb199a882b2ad55e0ca1cc8db0ee32c01abc46ce5ebb91a4c1d1f822379d9c1eea383ffe0f0bca0dfda31d11202f81af95a6a06a38e8522b0
|
7
|
+
data.tar.gz: 5714bf588eb545df3c2020b3e996fc6e437f40e375ae2659ed5735038fd42cea433760a9ed834485ce0e442c5bf18ada080932a4a74529965f91c15887196171
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Variantable
|
2
|
+
module Variantable
|
3
|
+
module InstanceMethods
|
4
|
+
|
5
|
+
protected
|
6
|
+
|
7
|
+
def read_variant(attr_name, locale = I18n.locale, fallback = true, **params)
|
8
|
+
translations = public_send("#{attr_name}#{SUFFIX}") || {}
|
9
|
+
|
10
|
+
translation = translations[selected_locale.to_s]
|
11
|
+
|
12
|
+
translation = I18n.interpolate(translation, params)
|
13
|
+
|
14
|
+
translation
|
15
|
+
end
|
16
|
+
|
17
|
+
def write_variant(attr_name, value, locale = I18n.locale)
|
18
|
+
value = value.presence
|
19
|
+
translation_store = "#{attr_name}#{SUFFIX}"
|
20
|
+
translations = public_send(translation_store) || {}
|
21
|
+
public_send("#{translation_store}_will_change!") unless translations[locale.to_s] == value
|
22
|
+
if value
|
23
|
+
translations[locale.to_s] = value
|
24
|
+
else
|
25
|
+
translations.delete(locale.to_s)
|
26
|
+
end
|
27
|
+
public_send("#{translation_store}=", translations)
|
28
|
+
value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/variantable/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: variantable
|
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
|
- Martin Ziserman
|
@@ -75,8 +75,8 @@ files:
|
|
75
75
|
- MIT-LICENSE
|
76
76
|
- README.md
|
77
77
|
- lib/variantable.rb
|
78
|
-
- lib/variantable/translates/instance_methods.rb
|
79
78
|
- lib/variantable/variantable.rb
|
79
|
+
- lib/variantable/variantable/instance_methods.rb
|
80
80
|
- lib/variantable/version.rb
|
81
81
|
homepage: https://github.com/publidata/variantable
|
82
82
|
licenses:
|
@@ -1,77 +0,0 @@
|
|
1
|
-
module JSONTranslate
|
2
|
-
module Translates
|
3
|
-
module InstanceMethods
|
4
|
-
def disable_fallback
|
5
|
-
toggle_fallback(false)
|
6
|
-
yield if block_given?
|
7
|
-
end
|
8
|
-
|
9
|
-
def enable_fallback
|
10
|
-
toggle_fallback(true)
|
11
|
-
yield if block_given?
|
12
|
-
end
|
13
|
-
|
14
|
-
protected
|
15
|
-
|
16
|
-
attr_reader :enabled_fallback
|
17
|
-
|
18
|
-
def json_translate_fallback_locales(locale)
|
19
|
-
if enabled_fallback != false && I18n.respond_to?(:fallbacks)
|
20
|
-
Array(I18n.fallbacks[locale])
|
21
|
-
else
|
22
|
-
Array(locale)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def read_json_translation(attr_name, locale = I18n.locale, fallback = true, **params)
|
27
|
-
translations = public_send("#{attr_name}#{SUFFIX}") || {}
|
28
|
-
|
29
|
-
selected_locale = locale
|
30
|
-
if fallback
|
31
|
-
selected_locale = json_translate_fallback_locales(locale).detect do |available_locale|
|
32
|
-
translations[available_locale.to_s].present?
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
translation = translations[selected_locale.to_s]
|
37
|
-
|
38
|
-
if translation && params.present?
|
39
|
-
begin
|
40
|
-
translation = I18n.interpolate(translation, params)
|
41
|
-
rescue I18n::MissingInterpolationArgument
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
translation
|
46
|
-
end
|
47
|
-
|
48
|
-
def write_json_translation(attr_name, value, locale = I18n.locale)
|
49
|
-
value = value.presence
|
50
|
-
translation_store = "#{attr_name}#{SUFFIX}"
|
51
|
-
translations = public_send(translation_store) || {}
|
52
|
-
public_send("#{translation_store}_will_change!") unless translations[locale.to_s] == value
|
53
|
-
if value
|
54
|
-
translations[locale.to_s] = value
|
55
|
-
else
|
56
|
-
translations.delete(locale.to_s)
|
57
|
-
end
|
58
|
-
public_send("#{translation_store}=", translations)
|
59
|
-
value
|
60
|
-
end
|
61
|
-
|
62
|
-
def toggle_fallback(enabled)
|
63
|
-
if block_given?
|
64
|
-
old_value = @enabled_fallback
|
65
|
-
begin
|
66
|
-
@enabled_fallback = enabled
|
67
|
-
yield
|
68
|
-
ensure
|
69
|
-
@enabled_fallback = old_value
|
70
|
-
end
|
71
|
-
else
|
72
|
-
@enabled_fallback = enabled
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|