voltron-translate 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/Gemfile +4 -0
- data/README.md +20 -13
- data/lib/voltron/translatable.rb +23 -21
- data/lib/voltron/translate/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 979979611ec2a7d4eac3353ae47a0caa509bbef5
|
4
|
+
data.tar.gz: 65497650f47fa5723f1725b7562ff38e99e13db2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18db3717e361b952ada8f4a11fa03b9fb54432810e9adb613959c50028b69e802a1dd57b7eabee78b1125fdc3edce2dfc335a4f079906e9ac31efa1d547f9652
|
7
|
+
data.tar.gz: 46759ab9a35119a2957b965d38984711d15f233d9fa37181bcf7532f750132a7fd1696df2ee12ae579d2a1d4cec7031f520c2ca23d66b644ce7de001451b6375
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,36 +1,42 @@
|
|
1
|
+
[![Coverage Status](https://coveralls.io/repos/github/ehainer/voltron-translate/badge.svg?branch=master)](https://coveralls.io/github/ehainer/voltron-translate?branch=master)
|
1
2
|
[![Build Status](https://travis-ci.org/ehainer/voltron-translate.svg?branch=master)](https://travis-ci.org/ehainer/voltron-translate)
|
3
|
+
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](http://www.gnu.org/licenses/gpl-3.0)
|
2
4
|
|
3
5
|
# Voltron::Translate
|
4
6
|
|
5
|
-
Voltron Translate is a different, in my mind more logical way of dealing with internationalization in rails, largely inspired by the Magento framework's __() method.
|
7
|
+
Voltron Translate is a different, in my mind more logical way of dealing with internationalization in rails, largely inspired by the Magento framework's `__()` helper method.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
9
11
|
Add this line to your application's Gemfile:
|
10
12
|
|
11
13
|
```ruby
|
12
|
-
gem 'voltron-translate'
|
14
|
+
gem 'voltron-translate', '~> 0.2.1'
|
13
15
|
```
|
14
16
|
|
15
17
|
And then execute:
|
16
18
|
|
17
|
-
$ bundle
|
19
|
+
$ bundle install
|
18
20
|
|
19
21
|
Or install it yourself as:
|
20
22
|
|
21
23
|
$ gem install voltron-translate
|
22
24
|
|
23
|
-
Then run the following to create the voltron.rb initializer (if not exists already)
|
25
|
+
Then run the following to create the voltron.rb initializer (if not exists already), add the translate config, and generate the database migration:
|
24
26
|
|
25
27
|
$ rails g voltron:translate:install
|
26
28
|
|
29
|
+
Then run the migrations to add the table to support backend translations:
|
30
|
+
|
31
|
+
$ bundle exec rake db:migrate
|
32
|
+
|
27
33
|
## Usage
|
28
34
|
|
29
35
|
### The Double Underscore Method
|
30
36
|
|
31
37
|
Voltron Translate extends ActiveRecord::Base, ActionController::Base, ActionMailer::Base, and ActionView::Base with a __ (double underscore) method that makes internationalization and/or translating static phrases easier.
|
32
38
|
|
33
|
-
Once installed, from any class that extends from any of the
|
39
|
+
Once installed, from any class that extends from any of the four rails classes listed you can use the double underscore method to allow for text translation. For example:
|
34
40
|
|
35
41
|
```ruby
|
36
42
|
@user = User.new(user_params)
|
@@ -117,10 +123,12 @@ class Company < ActiveRecord::Base
|
|
117
123
|
end
|
118
124
|
```
|
119
125
|
|
120
|
-
Options to the `translates` method are optional,
|
126
|
+
Options to the `translates` method are optional, details of which are:
|
121
127
|
|
122
|
-
|
123
|
-
|
128
|
+
| Option | Default | Comment |
|
129
|
+
|---------|-------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
130
|
+
| locales | `Voltron.config.translates.locales`, whose default is actually the value of `Rails.application.config.i18n.available_locales` | Should be an array of locale names, as strings or symbols. Each locale defined will extend the original attribute to make locale specific methods. i.e. - Locales `[:en, :es]` on attribute `name` would generate methods for `name_en`, `name_es`, `name_en=`, `name_es=`, etc... |
|
131
|
+
| default | nil | Should be a string or symbol of the default locale you'd like to use. This default overrides the value of `I18n.locale`, so should really only be used if the attribute in question needs to default to a different language. |
|
124
132
|
|
125
133
|
The `translates` method adds locale specific version of the attribute(s) to the model with the following methods:
|
126
134
|
|
@@ -193,11 +201,11 @@ Should go without saying, but to set the translation text on the frontend, you'd
|
|
193
201
|
|
194
202
|
Add the appropriate attributes to your strong params, so on, so on...
|
195
203
|
|
196
|
-
##
|
204
|
+
## Note
|
197
205
|
|
198
|
-
Setting `Voltron.config.translate.enabled` to `false` will never break any __() call, it simply causes it to ignore the locale argument (if specified) and return the interpolated string using the latter arguments (again, if any)
|
206
|
+
Setting `Voltron.config.translate.enabled` to `false` will never break any `__()` call, it simply causes it to ignore the locale argument (if specified) and return the interpolated string using the latter arguments (again, if any)
|
199
207
|
|
200
|
-
Disabling translations simply disables any IO related actions that would occur normally, like building or looking up translations when __() methods are called.
|
208
|
+
Disabling translations simply disables any IO related actions that would occur normally, like building or looking up translations when `__()` methods are called.
|
201
209
|
|
202
210
|
It also disables the locale specific text translation on any method call that was targeted with `translates`, meaning `@company.name(:es)` would be the equivalent of calling `@company.name`. Note that `@company.name_es` would still work as it normally would.
|
203
211
|
|
@@ -211,5 +219,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/ehaine
|
|
211
219
|
|
212
220
|
## License
|
213
221
|
|
214
|
-
The gem is available as open source under the terms of the [
|
215
|
-
|
222
|
+
The gem is available as open source under the terms of the [GNU General Public License](https://www.gnu.org/licenses/gpl-3.0.en.html).
|
data/lib/voltron/translatable.rb
CHANGED
@@ -7,60 +7,62 @@ module Voltron
|
|
7
7
|
options = (attributes.extract_options!).with_indifferent_access
|
8
8
|
locales = Array.wrap(options[:locales] || Voltron.config.translate.locales).map(&:to_s).map(&:underscore)
|
9
9
|
|
10
|
-
attributes.each do |
|
10
|
+
attributes.each do |attrib|
|
11
11
|
|
12
|
-
column = self.columns_hash[
|
12
|
+
column = self.columns_hash[attrib.to_s]
|
13
13
|
|
14
|
-
raise ::ActiveRecord::UnknownAttributeError.new(self.new,
|
14
|
+
raise ::ActiveRecord::UnknownAttributeError.new(self.new, attrib) if column.nil?
|
15
15
|
|
16
|
-
raise ::Voltron::Translate::InvalidColumnTypeError.new("Invalid type '#{column.type}' for attribute: #{
|
16
|
+
raise ::Voltron::Translate::InvalidColumnTypeError.new("Invalid type '#{column.type}' for attribute: #{attrib}. Translations only work on string and text attribute types.") unless [:string, :text].include?(column.type)
|
17
17
|
|
18
18
|
# Override the attribute with a method that accepts a specific locale as an argument
|
19
19
|
# If specified, will attempt to fetch that locale's translation, otherwise the default
|
20
20
|
# locale specified for the attribute, and ultimately the current locale translation
|
21
21
|
# If still nil, returns the value from super
|
22
|
-
define_method :"#{
|
22
|
+
define_method :"#{attrib}" do |locale=nil|
|
23
23
|
# +action_view/helpers/targs+ exist when this method is called from within
|
24
24
|
# ActionView::Helpers. In other words, form helper tags. In that
|
25
25
|
# case we want the actual value of the attribute, not whatever the locale is
|
26
26
|
return super() if caller.any? { |l| /action_view\/helpers\/tags/.match(l) } || !Voltron.config.translate.enabled?
|
27
|
-
try(:"#{
|
27
|
+
try(:"#{attrib}_#{locale.to_s.underscore}") || try(:"#{attrib}_#{options[:default].to_s.underscore}") || try(:"#{attrib}_#{I18n.locale.to_s.underscore}") || super()
|
28
28
|
end
|
29
29
|
|
30
30
|
locales.each do |locale|
|
31
31
|
|
32
|
+
attribute :"#{attrib}_#{locale}"
|
33
|
+
|
32
34
|
# Define setter, i.e. - +attribute_es=+
|
33
|
-
define_method :"#{
|
34
|
-
attribute_will_change! "#{
|
35
|
-
instance_variable_set("@#{
|
35
|
+
define_method :"#{attrib}_#{locale}=" do |val|
|
36
|
+
attribute_will_change! "#{attrib}_#{locale}"
|
37
|
+
instance_variable_set("@#{attrib}_#{locale}", val)
|
36
38
|
end
|
37
39
|
|
38
40
|
# Define getter, i.e - +attribute_es+
|
39
41
|
# If nil, calling this method will attempt to fetch the value
|
40
42
|
# We do this to avoid preloading the translations association records
|
41
|
-
define_method :"#{
|
42
|
-
if instance_variable_get("@#{
|
43
|
-
instance_variable_set("@#{
|
43
|
+
define_method :"#{attrib}_#{locale}" do
|
44
|
+
if instance_variable_get("@#{attrib}_#{locale}").nil?
|
45
|
+
instance_variable_set("@#{attrib}_#{locale}", send(:"#{attrib}_#{locale}_was"))
|
44
46
|
end
|
45
|
-
instance_variable_get("@#{
|
47
|
+
instance_variable_get("@#{attrib}_#{locale}")
|
46
48
|
end
|
47
49
|
|
48
50
|
# Define the changed? method, i.e. - +attribute_es_changed?+
|
49
|
-
define_method :"#{
|
50
|
-
changed.include?("#{
|
51
|
+
define_method :"#{attrib}_#{locale}_changed?" do
|
52
|
+
changed.include?("#{attrib}_#{locale}")
|
51
53
|
end
|
52
54
|
|
53
55
|
# Define the was method, i.e. - +attribute_es_was+
|
54
|
-
define_method :"#{
|
55
|
-
translations.find_by(attribute_name:
|
56
|
+
define_method :"#{attrib}_#{locale}_was" do
|
57
|
+
translations.find_by(attribute_name: attrib, locale: locale).try(:translation)
|
56
58
|
end
|
57
59
|
|
58
|
-
define_method :"#{
|
59
|
-
attribute_will_change! "#{
|
60
|
+
define_method :"#{attrib}_#{locale}_will_change!" do
|
61
|
+
attribute_will_change! "#{attrib}_#{locale}"
|
60
62
|
end
|
61
63
|
|
62
|
-
define_method :"#{
|
63
|
-
instance_variable_get("@#{
|
64
|
+
define_method :"#{attrib}_#{locale}?" do
|
65
|
+
instance_variable_get("@#{attrib}_#{locale}").present?
|
64
66
|
end
|
65
67
|
|
66
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voltron-translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Hainer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -210,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
210
210
|
version: '0'
|
211
211
|
requirements: []
|
212
212
|
rubyforge_project:
|
213
|
-
rubygems_version: 2.6.
|
213
|
+
rubygems_version: 2.6.11
|
214
214
|
signing_key:
|
215
215
|
specification_version: 4
|
216
216
|
summary: Implements double underscore method for translating and interpolating phrases
|