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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 496b01a5fc6aef8382150266d811b2b0feddbd4e
4
- data.tar.gz: 6db2b1e2291a17e376c74cee8744a674632c17d7
3
+ metadata.gz: 979979611ec2a7d4eac3353ae47a0caa509bbef5
4
+ data.tar.gz: 65497650f47fa5723f1725b7562ff38e99e13db2
5
5
  SHA512:
6
- metadata.gz: eef4569dd7db4562ceed56dce7154fe081708b4f975b0b5b600a973bf68b30caf347cd8799a8a1253b87c22542ed69668da41e1501027ed9c04200b52313bd99
7
- data.tar.gz: e27b55db4f9163ec69576d22a89845dff355f01a52d55dffa16705311cb5ed41d296924b8e878f7984cb8310e64901aedfd291d3542d769b25b187eec08130f4
6
+ metadata.gz: 18db3717e361b952ada8f4a11fa03b9fb54432810e9adb613959c50028b69e802a1dd57b7eabee78b1125fdc3edce2dfc335a4f079906e9ac31efa1d547f9652
7
+ data.tar.gz: 46759ab9a35119a2957b965d38984711d15f233d9fa37181bcf7532f750132a7fd1696df2ee12ae579d2a1d4cec7031f520c2ca23d66b644ce7de001451b6375
data/.travis.yml CHANGED
@@ -3,4 +3,6 @@ language: ruby
3
3
  rvm:
4
4
  - 2.2.3
5
5
  - 2.3.1
6
+ - 2.3.3
7
+ - 2.4.1
6
8
  before_install: gem install bundler -v 1.12.5
data/Gemfile CHANGED
@@ -3,3 +3,7 @@ source 'http://gem.minow.io'
3
3
 
4
4
  # Specify your gem's dependencies in voltron-translate.gemspec
5
5
  gemspec
6
+
7
+ group :test do
8
+ gem 'coveralls', require: false
9
+ end
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) and add the translate config:
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 three rails classes you can use the double underscore method to allow for real time text translation. For example:
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, and, if any/all or omitted, the defaults are as follows:
126
+ Options to the `translates` method are optional, details of which are:
121
127
 
122
- locales -> Defaults to Voltron.config.translates.locales, which itself defaults to Rails.application.config.i18n.available_locales
123
- default -> nil, will just return the value of the original attribute, i.e. - "name" or "greeting"
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
- ## Things to Note
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 [MIT License](http://opensource.org/licenses/MIT).
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).
@@ -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 |attribute|
10
+ attributes.each do |attrib|
11
11
 
12
- column = self.columns_hash[attribute.to_s]
12
+ column = self.columns_hash[attrib.to_s]
13
13
 
14
- raise ::ActiveRecord::UnknownAttributeError.new(self.new, attribute) if column.nil?
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: #{attribute}. Translations only work on string and text attribute types.") unless [:string, :text].include?(column.type)
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 :"#{attribute}" do |locale=nil|
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(:"#{attribute}_#{locale.to_s.underscore}") || try(:"#{attribute}_#{options[:default].to_s.underscore}") || try(:"#{attribute}_#{I18n.locale.to_s.underscore}") || super()
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 :"#{attribute}_#{locale}=" do |val|
34
- attribute_will_change! "#{attribute}_#{locale}"
35
- instance_variable_set("@#{attribute}_#{locale}", val)
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 :"#{attribute}_#{locale}" do
42
- if instance_variable_get("@#{attribute}_#{locale}").nil?
43
- instance_variable_set("@#{attribute}_#{locale}", send(:"#{attribute}_#{locale}_was"))
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("@#{attribute}_#{locale}")
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 :"#{attribute}_#{locale}_changed?" do
50
- changed.include?("#{attribute}_#{locale}")
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 :"#{attribute}_#{locale}_was" do
55
- translations.find_by(attribute_name: attribute, locale: locale).try(:translation)
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 :"#{attribute}_#{locale}_will_change!" do
59
- attribute_will_change! "#{attribute}_#{locale}"
60
+ define_method :"#{attrib}_#{locale}_will_change!" do
61
+ attribute_will_change! "#{attrib}_#{locale}"
60
62
  end
61
63
 
62
- define_method :"#{attribute}_#{locale}?" do
63
- instance_variable_get("@#{attribute}_#{locale}").present?
64
+ define_method :"#{attrib}_#{locale}?" do
65
+ instance_variable_get("@#{attrib}_#{locale}").present?
64
66
  end
65
67
 
66
68
  end
@@ -1,5 +1,5 @@
1
1
  module Voltron
2
2
  module Translate
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.2.1'.freeze
4
4
  end
5
5
  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.0
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-04-17 00:00:00.000000000 Z
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.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