i18n_accessors 0.1.1 → 0.1.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/Gemfile.lock +1 -1
- data/README.md +22 -4
- data/lib/i18n_accessors.rb +26 -0
- data/lib/i18n_accessors/methods.rb +3 -3
- data/lib/i18n_accessors/missing.rb +1 -1
- data/lib/i18n_accessors/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98b58932586d8b91f258b71fb586f16caad57ecd
|
4
|
+
data.tar.gz: 11a1f04242c82616d1a94fdad70cc174bd77f65e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9aa04edf966a12cfbadc3ffb46c790900904847b66b06b55243a3cf0a4b0c9e4024113d4f9b2af24f067dd0d013421c59b6cafbdda11961b312244ba4598da4
|
7
|
+
data.tar.gz: a2076b293ec651056c1e2a152d90809b870139f34b18df74ddfd85bdbba0986b742b106a337c58ca2f5b419224a52a87e0dbaa919108307082dc8c06a91e81fc
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -6,14 +6,16 @@
|
|
6
6
|
[gem]: https://rubygems.org/gems/i18n_accessors
|
7
7
|
[travis]: https://travis-ci.org/shioyama/i18n_accessors
|
8
8
|
|
9
|
-
Define locale accessors for your translated attributes.
|
9
|
+
Define locale accessors for your translated attributes. Extracted from
|
10
|
+
[Mobility](https://github.com/shioyama/mobility); works with
|
11
|
+
[Globalize](https://github.com/globalize/globalize) and other translation gems.
|
10
12
|
|
11
13
|
## Installation
|
12
14
|
|
13
15
|
Add this line to your application's Gemfile:
|
14
16
|
|
15
17
|
```ruby
|
16
|
-
gem 'i18n_accessors', '~> 0.1.
|
18
|
+
gem 'i18n_accessors', '~> 0.1.2'
|
17
19
|
```
|
18
20
|
|
19
21
|
And then execute:
|
@@ -45,7 +47,7 @@ class Post
|
|
45
47
|
|
46
48
|
def title=(title)
|
47
49
|
# ...
|
48
|
-
end
|
50
|
+
end
|
49
51
|
|
50
52
|
include I18nAccessors::Methods.new(:title)
|
51
53
|
end
|
@@ -87,7 +89,23 @@ class Post
|
|
87
89
|
end
|
88
90
|
```
|
89
91
|
|
90
|
-
|
92
|
+
You can include both a `I18nAccessors::Methods` module and a
|
93
|
+
`I18nAccessors::Missing` module in the same class without conflict (the
|
94
|
+
accessor methods will take precedence).
|
95
|
+
|
96
|
+
### Configuration
|
97
|
+
|
98
|
+
If you are using I18nAccessors with Globalize, you can change the default i18n
|
99
|
+
class to `Globalize`:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
I18nAccessors.configure do |config|
|
103
|
+
config.i18n_class = Globalize
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
With this configuration, `Globalize.with_locale` will be used to set the locale
|
108
|
+
around the accessor methods, rather than `I18n.with_locale`.
|
91
109
|
|
92
110
|
## Development
|
93
111
|
|
data/lib/i18n_accessors.rb
CHANGED
@@ -4,6 +4,24 @@ require "i18n_accessors/missing"
|
|
4
4
|
|
5
5
|
module I18nAccessors
|
6
6
|
class << self
|
7
|
+
|
8
|
+
# Configure I18nAccessors
|
9
|
+
# @yield [I18nAccessors::Configuration] I18nAccessors configuration
|
10
|
+
def configure
|
11
|
+
yield config
|
12
|
+
end
|
13
|
+
|
14
|
+
# @!group Configuration Methods
|
15
|
+
# @return [I18nAccessors::Configuration] I18nAccessors configuration
|
16
|
+
def config
|
17
|
+
@configuration ||= Configuration.new
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Class] I18n Class to use for setting locale
|
21
|
+
def i18n_class
|
22
|
+
config.i18n_class
|
23
|
+
end
|
24
|
+
|
7
25
|
# Return normalized locale
|
8
26
|
# @param [String,Symbol] locale
|
9
27
|
# @return [String] Normalized locale
|
@@ -16,4 +34,12 @@ module I18nAccessors
|
|
16
34
|
"#{locale.to_s.downcase.sub("-", "_")}".freeze
|
17
35
|
end
|
18
36
|
end
|
37
|
+
|
38
|
+
class Configuration
|
39
|
+
attr_accessor :i18n_class
|
40
|
+
|
41
|
+
def initialize
|
42
|
+
@i18n_class ||= I18n
|
43
|
+
end
|
44
|
+
end
|
19
45
|
end
|
@@ -34,13 +34,13 @@ If no locales are passed as an option to the initializer,
|
|
34
34
|
locales.each do |locale|
|
35
35
|
normalized_locale = I18nAccessors.normalize_locale(locale)
|
36
36
|
define_method "#{attribute}_#{normalized_locale}" do |*args|
|
37
|
-
|
37
|
+
I18nAccessors.i18n_class.with_locale(locale) { send(attribute, *args) }
|
38
38
|
end
|
39
39
|
define_method "#{attribute}_#{normalized_locale}?" do |*args|
|
40
|
-
|
40
|
+
I18nAccessors.i18n_class.with_locale(locale) { send("#{attribute}?", *args) }
|
41
41
|
end
|
42
42
|
define_method "#{attribute}_#{normalized_locale}=" do |value, *args|
|
43
|
-
|
43
|
+
I18nAccessors.i18n_class.with_locale(locale) { send("#{attribute}=", value, *args) }
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -35,7 +35,7 @@ the method call.
|
|
35
35
|
attribute = $1.to_sym
|
36
36
|
locale, suffix = $2.split('_'.freeze)
|
37
37
|
locale = "#{locale}-#{suffix.upcase}".freeze if suffix
|
38
|
-
|
38
|
+
I18nAccessors.i18n_class.with_locale(locale.to_sym) { public_send("#{attribute}#{$4}".freeze, *arguments) }
|
39
39
|
else
|
40
40
|
super(method_name, *arguments, &block)
|
41
41
|
end
|