activemodel_translation 0.1.0
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +11 -0
- data/activemodel_translation.gemspec +25 -0
- data/lib/activemodel_translation/helper.rb +27 -0
- data/lib/activemodel_translation/plural.rb +23 -0
- data/lib/activemodel_translation/version.rb +3 -0
- data/lib/activemodel_translation.rb +3 -0
- data/test/lib/activemodel_translation/helper_test.rb +33 -0
- data/test/lib/activemodel_translation/plural_test.rb +60 -0
- data/test/models/person.rb +12 -0
- data/test/test_helper.rb +3 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea2a95ce1ac09f6f0136981773a119e506416a04
|
4
|
+
data.tar.gz: 601425e4b7a7838047fa25051193ea9b0fbf164d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 706cd8c9725fbdd63dc2857c48d430d1283a8d38590cff69a14eedb3b10582c269d96fd9d2536afa857797a36535ce7cc8897bb0d589680ad800574e31f941e0
|
7
|
+
data.tar.gz: 11a994a0315fb0997d6d897a8572754056d828b6b3740063cf318b258c4dfa7d2fde6a1ca6baf47e6cc0bf68558ba5c980d188275be7f12fd1d219c725231cf4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Max Melentiev
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# ActivemodelTranslation
|
2
|
+
|
3
|
+
Lazy translations for models.
|
4
|
+
|
5
|
+
`::tranlate` method performs translation with fallback similar to resolving errors.
|
6
|
+
With one difference - it uses `strings` scope.
|
7
|
+
|
8
|
+
* `activemodel.strings.admin.key`
|
9
|
+
* `activemodel.strings.user.key`
|
10
|
+
* `strings.key`
|
11
|
+
|
12
|
+
`User.model_name.human(plural: true)` returns translated plural model name.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'activemodel_translation'
|
20
|
+
|
21
|
+
# or pick a module:
|
22
|
+
# gem 'activemodel_translation', require: 'activemodel_translation/helper'
|
23
|
+
# gem 'activemodel_translation', require: 'activemodel_translation/plural'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install activemodel_translation
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class User < ActiveRecord::Base
|
38
|
+
# lazy translation
|
39
|
+
def title
|
40
|
+
super || self.class.t(:'.default_title')
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# translated plural model name
|
45
|
+
User.model_name.human(plural: true)
|
46
|
+
|
47
|
+
# ru.yml
|
48
|
+
# activerecord:
|
49
|
+
# models:
|
50
|
+
# user:
|
51
|
+
# one: Пользователь
|
52
|
+
# few: Пользователя
|
53
|
+
# many: Пользователей
|
54
|
+
# other: Пользователя
|
55
|
+
# plural: Пользователи
|
56
|
+
```
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it ( http://github.com/printercu/activemodel_translation/fork )
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new do |t|
|
5
|
+
t.libs << 'lib/activemodel_translation'
|
6
|
+
t.libs << 'test'
|
7
|
+
t.test_files = FileList['test/lib/activemodel_translation/*_test.rb']
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
task default: :test
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activemodel_translation/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "activemodel_translation"
|
8
|
+
spec.version = ActivemodelTranslation::VERSION
|
9
|
+
spec.authors = ["Max Melentiev"]
|
10
|
+
spec.email = ["melentievm@gmail.com"]
|
11
|
+
spec.summary = 'Translation helpers for ActiveModel'
|
12
|
+
spec.description = 'Use lazy translations from models'
|
13
|
+
spec.homepage = 'http://github.com/printercu/activemodel_translation'
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activemodel", ">= 3.2", "< 5.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
+
spec.add_development_dependency "rake", "~> 10"
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
module Translation
|
7
|
+
# Delegates to <tt>I18n#translate</tt> but also performs scoping if key
|
8
|
+
# starts with a period. So if you call <tt>translate(".foo")</tt>
|
9
|
+
# translation will be searched in <tt>I18N_SCOPE.strings.MODEL.foo</tt>
|
10
|
+
# then the same key for all the ancestors and finally in <tt>strings.foo</tt>.
|
11
|
+
def translate(key, options = {})
|
12
|
+
return I18n.translate(key, options) unless key.to_s.first == '.'
|
13
|
+
|
14
|
+
strings_scope = "#{i18n_scope}.strings"
|
15
|
+
|
16
|
+
defaults = lookup_ancestors.map do |klass|
|
17
|
+
:"#{strings_scope}.#{klass.model_name.i18n_key}#{key}"
|
18
|
+
end
|
19
|
+
defaults << :"strings#{key}"
|
20
|
+
defaults << options.delete(:default) if options[:default]
|
21
|
+
|
22
|
+
options[:default] = defaults
|
23
|
+
I18n.translate(defaults.shift, options)
|
24
|
+
end
|
25
|
+
alias :t :translate
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Name
|
5
|
+
def human(options={})
|
6
|
+
return @human unless @klass.respond_to?(:lookup_ancestors) &&
|
7
|
+
@klass.respond_to?(:i18n_scope)
|
8
|
+
|
9
|
+
need_plural = options[:plural]
|
10
|
+
|
11
|
+
defaults = @klass.lookup_ancestors.map do |klass|
|
12
|
+
klass.model_name.i18n_key
|
13
|
+
end
|
14
|
+
defaults = defaults.map { |key| :"#{key}.plural" } if need_plural
|
15
|
+
|
16
|
+
defaults << options[:default] if options[:default]
|
17
|
+
defaults << (need_plural ? @plural : @human)
|
18
|
+
|
19
|
+
options = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults }.merge!(options.except(:default))
|
20
|
+
I18n.translate(defaults.shift, options)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'activemodel_translation/helper'
|
3
|
+
require 'models/person'
|
4
|
+
|
5
|
+
class ActiveModelI18nTests < Minitest::Test
|
6
|
+
def setup
|
7
|
+
I18n.backend = I18n::Backend::Simple.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_lazy_translation
|
11
|
+
I18n.backend.store_translations 'en', activemodel: { strings: { person: { type: 'person type' } } }
|
12
|
+
assert_equal 'person type', Person.t(:'.type')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_lazy_translation_with_default
|
16
|
+
I18n.backend.store_translations 'en', strings: { type: 'person type' }
|
17
|
+
assert_equal 'person type', Person.t(:'.type')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_lazy_translation_with_default_option
|
21
|
+
assert_equal 'default type', Person.t(:'.type', default: 'default type')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_lazy_translation_with_sti
|
25
|
+
I18n.backend.store_translations 'en', activemodel: { strings: { child: { type: 'child type' } } }
|
26
|
+
assert_equal 'child type', Child.t(:'.type')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_lazy_translation_with_ancestors_fallback
|
30
|
+
I18n.backend.store_translations 'en', activemodel: { strings: { person: { type: 'person type' } } }
|
31
|
+
assert_equal 'person type', Child.t(:'.type')
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'activemodel_translation/plural'
|
3
|
+
require 'models/person'
|
4
|
+
|
5
|
+
class ActiveModelHumanNameTests < ::Minitest::Test
|
6
|
+
def setup
|
7
|
+
I18n.backend = I18n::Backend::Simple.new
|
8
|
+
I18n.backend.store_translations 'ru', activemodel: { models: { person: {
|
9
|
+
one: 'Пользователь',
|
10
|
+
few: 'Пользователя',
|
11
|
+
many: 'Пользователей',
|
12
|
+
other: 'Пользователя',
|
13
|
+
plural: 'Пользователи',
|
14
|
+
} } }
|
15
|
+
I18n.locale = :ru
|
16
|
+
@person_name = ActiveModel::Name.new(Person)
|
17
|
+
@child_name = ActiveModel::Name.new(Child)
|
18
|
+
@gender_name = ActiveModel::Name.new(Person::Gender)
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown
|
22
|
+
I18n.locale = :en
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_human_with_translation
|
26
|
+
assert_equal 'Пользователь', @person_name.human
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_human_with_fallback_translation
|
30
|
+
assert_equal 'Пользователь', @child_name.human
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_human_without_translation
|
34
|
+
assert_equal 'Gender', @gender_name.human
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_human_with_count_with_translation
|
38
|
+
assert_equal 'Пользователя', @person_name.human(count: 1.5)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_human_with_count_with_fallback_translation
|
42
|
+
assert_equal 'Пользователя', @child_name.human(count: 1.5)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_human_with_count_without_translation
|
46
|
+
assert_equal 'Gender', @gender_name.human(count: 1.5)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_plural_human_with_translation
|
50
|
+
assert_equal 'Пользователи', @person_name.human(plural: true)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_plural_human_with_fallback_translation
|
54
|
+
assert_equal 'Пользователи', @child_name.human(plural: true)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_plural_human_without_translation
|
58
|
+
assert_equal 'person_genders', @gender_name.human(plural: true)
|
59
|
+
end
|
60
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activemodel_translation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Max Melentiev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.5'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.5'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10'
|
61
|
+
description: Use lazy translations from models
|
62
|
+
email:
|
63
|
+
- melentievm@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- activemodel_translation.gemspec
|
74
|
+
- lib/activemodel_translation.rb
|
75
|
+
- lib/activemodel_translation/helper.rb
|
76
|
+
- lib/activemodel_translation/plural.rb
|
77
|
+
- lib/activemodel_translation/version.rb
|
78
|
+
- test/lib/activemodel_translation/helper_test.rb
|
79
|
+
- test/lib/activemodel_translation/plural_test.rb
|
80
|
+
- test/models/person.rb
|
81
|
+
- test/test_helper.rb
|
82
|
+
homepage: http://github.com/printercu/activemodel_translation
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.2.2
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Translation helpers for ActiveModel
|
106
|
+
test_files:
|
107
|
+
- test/lib/activemodel_translation/helper_test.rb
|
108
|
+
- test/lib/activemodel_translation/plural_test.rb
|
109
|
+
- test/models/person.rb
|
110
|
+
- test/test_helper.rb
|