i18nable 0.1.2 → 0.2.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 +4 -4
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +3 -3
- data/README.md +35 -8
- data/lib/i18nable/key_builder.rb +6 -3
- data/lib/i18nable/version.rb +1 -1
- data/lib/i18nable.rb +18 -9
- metadata +20 -31
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e92de26c183d7f2e2654bd469fb3782673c5bf98de0aa4c257e2f38431eb4c0
|
|
4
|
+
data.tar.gz: ed83fd861880fc555ef38d57407fa3fb6691968d9003d6837478c866f6fbbe18
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 85527f5a306b12afe7f44333b98d8db551d47c1e5cbbabf972b8112419589c0ed30ef96262b634a0a781aa6c72772d7a67f3f781005e82dd049762baef5f7daa
|
|
7
|
+
data.tar.gz: 8ba645355ec42526205fb37c430564841a772c9c64186b297895a149b58adca74a3b5cbdbbab755f25df10d4d1112c650ccb86388f4b5c10f386cef95f862f62
|
data/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
5
|
+
|
|
6
|
+
## [Unreleased]
|
|
7
|
+
|
|
8
|
+
## [0.2.0]
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- **Breaking:** require Ruby >= 3.1 and `i18n` >= 1.8.
|
|
12
|
+
- `translate` and `localize` take keyword options (`**options`) instead of a positional hash.
|
|
13
|
+
- Development tooling: RSpec ~> 3.13, RuboCop ~> 1.60, `bin/console` uses IRB (`pry` removed).
|
|
14
|
+
|
|
15
|
+
### Fixed
|
|
16
|
+
- `translate` and `localize` options (`locale:`, `default:`, `count:`, interpolation, `format:`) work on Ruby 3.
|
|
17
|
+
- `KeyBuilder#build` no longer mutates its memoized scope.
|
|
18
|
+
- Anonymous classes raise a descriptive `ArgumentError` instead of `NoMethodError`.
|
|
19
|
+
|
|
20
|
+
### Added
|
|
21
|
+
- GitHub Actions CI, Dependabot configuration, gemspec metadata, CONTRIBUTING guide.
|
|
22
|
+
|
|
23
|
+
## [0.1.2]
|
|
24
|
+
- Previous release.
|
data/CODE_OF_CONDUCT.md
CHANGED
|
@@ -68,7 +68,7 @@ members of the project's leadership.
|
|
|
68
68
|
## Attribution
|
|
69
69
|
|
|
70
70
|
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
-
available at [
|
|
71
|
+
available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct/][version]
|
|
72
72
|
|
|
73
|
-
[homepage]:
|
|
74
|
-
[version]:
|
|
73
|
+
[homepage]: https://www.contributor-covenant.org
|
|
74
|
+
[version]: https://www.contributor-covenant.org/version/1/4/code-of-conduct/
|
data/README.md
CHANGED
|
@@ -18,14 +18,22 @@ Or install it yourself as:
|
|
|
18
18
|
|
|
19
19
|
$ gem install i18nable
|
|
20
20
|
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
- Ruby >= 3.1
|
|
24
|
+
- `i18n` >= 1.8 (works standalone or with Rails)
|
|
25
|
+
|
|
21
26
|
## Usage
|
|
22
27
|
|
|
28
|
+
Include `I18nable` in any class. Keys starting with a dot are scoped by the underscored class name
|
|
29
|
+
(`AmazingModule::PoorClass` becomes `amazing_module.poor_class`); other keys are passed to I18n unchanged.
|
|
30
|
+
|
|
23
31
|
```yaml
|
|
24
32
|
en:
|
|
25
33
|
amazing_module:
|
|
26
34
|
poor_class:
|
|
27
35
|
mixed_feelings: 'Mixed feelings'
|
|
28
|
-
|
|
36
|
+
greeting: 'Hello %{name}'
|
|
29
37
|
```
|
|
30
38
|
|
|
31
39
|
```ruby
|
|
@@ -36,23 +44,42 @@ module AmazingModule
|
|
|
36
44
|
def what_do_you_feel?
|
|
37
45
|
translate '.mixed_feelings'
|
|
38
46
|
end
|
|
47
|
+
|
|
48
|
+
def greet(name)
|
|
49
|
+
translate '.greeting', name: name
|
|
50
|
+
end
|
|
39
51
|
end
|
|
40
52
|
end
|
|
41
53
|
|
|
54
|
+
poor_class = AmazingModule::PoorClass.new
|
|
55
|
+
poor_class.what_do_you_feel? # => "Mixed feelings"
|
|
56
|
+
poor_class.greet('Ann') # => "Hello Ann"
|
|
57
|
+
AmazingModule::PoorClass.translate '.mixed_feelings' # also available on the class
|
|
42
58
|
```
|
|
43
59
|
|
|
44
|
-
|
|
45
|
-
|
|
60
|
+
### Options
|
|
61
|
+
|
|
62
|
+
`translate` accepts any `I18n.translate` option (`locale:`, `default:`, `count:`, `raise:`, interpolation values)
|
|
63
|
+
as keyword arguments. `localize(object, **options)` delegates to `I18n.localize`.
|
|
46
64
|
|
|
47
|
-
|
|
48
|
-
|
|
65
|
+
```ruby
|
|
66
|
+
translate '.mixed_feelings', locale: :pl, default: 'n/a'
|
|
67
|
+
localize Date.today, format: :short
|
|
49
68
|
```
|
|
50
69
|
|
|
51
|
-
|
|
70
|
+
Keys without a leading dot or non-String keys (e.g. `:fake`) are not scoped. Anonymous classes
|
|
71
|
+
cannot be scoped and raise `ArgumentError`.
|
|
52
72
|
|
|
53
|
-
|
|
73
|
+
### Rails
|
|
74
|
+
|
|
75
|
+
Put translations in `config/locales/*.yml` under the underscored class path and include `I18nable`
|
|
76
|
+
in models, services or any plain Ruby object.
|
|
77
|
+
|
|
78
|
+
## Development
|
|
54
79
|
|
|
55
|
-
|
|
80
|
+
Run `bin/setup` to install dependencies, `bundle exec rake` to run specs and RuboCop, and `bin/console` for an
|
|
81
|
+
interactive prompt. To release, update `lib/i18nable/version.rb` and `CHANGELOG.md`, then run `bundle exec rake release`.
|
|
82
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
56
83
|
|
|
57
84
|
## Contributing
|
|
58
85
|
|
data/lib/i18nable/key_builder.rb
CHANGED
|
@@ -7,7 +7,7 @@ module I18nable
|
|
|
7
7
|
def build(key)
|
|
8
8
|
return key unless key.is_a?(String) && scopable?(key)
|
|
9
9
|
|
|
10
|
-
scope
|
|
10
|
+
"#{scope}#{key}"
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
private
|
|
@@ -21,11 +21,14 @@ module I18nable
|
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
def class_name
|
|
24
|
-
|
|
24
|
+
name = @base_class.name
|
|
25
|
+
raise ArgumentError, 'I18nable cannot build keys for anonymous classes' if name.nil?
|
|
26
|
+
|
|
27
|
+
underscore name
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
def underscore(key)
|
|
28
|
-
key.gsub(
|
|
31
|
+
key.gsub('::', '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
|
|
29
32
|
end
|
|
30
33
|
end
|
|
31
34
|
end
|
data/lib/i18nable/version.rb
CHANGED
data/lib/i18nable.rb
CHANGED
|
@@ -5,26 +5,35 @@ require 'i18nable/key_builder'
|
|
|
5
5
|
module I18nable
|
|
6
6
|
def self.included(base)
|
|
7
7
|
base.extend ClassMethods
|
|
8
|
-
base.
|
|
8
|
+
base.include InstanceMethods
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
module InstanceMethods
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
# @see ClassMethods#translate
|
|
13
|
+
def translate(key, **options)
|
|
14
|
+
self.class.translate(key, **options)
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
# @see ClassMethods#localize
|
|
18
|
+
def localize(object, **options)
|
|
19
|
+
self.class.localize(object, **options)
|
|
18
20
|
end
|
|
19
21
|
end
|
|
20
22
|
|
|
21
23
|
module ClassMethods
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
# Translates a key; keys starting with '.' are scoped by the underscored class name.
|
|
25
|
+
# @param key [String, Symbol]
|
|
26
|
+
# @param options [Hash] any I18n.translate option
|
|
27
|
+
# @return [String]
|
|
28
|
+
# @example
|
|
29
|
+
# translate '.title' # looks up "my_class.title"
|
|
30
|
+
def translate(key, **options)
|
|
31
|
+
I18n.translate(i18nable_key_builder.build(key), **options)
|
|
24
32
|
end
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
# @see I18n.localize
|
|
35
|
+
def localize(object, **options)
|
|
36
|
+
I18n.localize(object, **options)
|
|
28
37
|
end
|
|
29
38
|
|
|
30
39
|
private
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: i18nable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tkowalewski
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: i18n
|
|
@@ -16,71 +15,58 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '1.8'
|
|
20
19
|
type: :runtime
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '1.8'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: rake
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
30
|
- - "~>"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '13.0'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
37
|
- - "~>"
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
39
|
+
version: '13.0'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: rspec
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
44
|
- - "~>"
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: 3.
|
|
46
|
+
version: '3.13'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
51
|
- - "~>"
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: 3.
|
|
53
|
+
version: '3.13'
|
|
55
54
|
- !ruby/object:Gem::Dependency
|
|
56
55
|
name: rubocop
|
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
|
58
|
-
requirements:
|
|
59
|
-
- - ">="
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: 0.49.0
|
|
62
|
-
type: :development
|
|
63
|
-
prerelease: false
|
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
-
requirements:
|
|
66
|
-
- - ">="
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: 0.49.0
|
|
69
|
-
- !ruby/object:Gem::Dependency
|
|
70
|
-
name: pry
|
|
71
56
|
requirement: !ruby/object:Gem::Requirement
|
|
72
57
|
requirements:
|
|
73
58
|
- - "~>"
|
|
74
59
|
- !ruby/object:Gem::Version
|
|
75
|
-
version:
|
|
60
|
+
version: '1.60'
|
|
76
61
|
type: :development
|
|
77
62
|
prerelease: false
|
|
78
63
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
64
|
requirements:
|
|
80
65
|
- - "~>"
|
|
81
66
|
- !ruby/object:Gem::Version
|
|
82
|
-
version:
|
|
83
|
-
description:
|
|
67
|
+
version: '1.60'
|
|
68
|
+
description: Adds translate and localize helpers to any Ruby class, with keys starting
|
|
69
|
+
with a dot scoped by the class name.
|
|
84
70
|
email:
|
|
85
71
|
- me@tkowalewski.pl
|
|
86
72
|
executables: []
|
|
@@ -97,8 +83,12 @@ files:
|
|
|
97
83
|
homepage: https://github.com/tkowalewski/i18nable
|
|
98
84
|
licenses:
|
|
99
85
|
- MIT
|
|
100
|
-
metadata:
|
|
101
|
-
|
|
86
|
+
metadata:
|
|
87
|
+
homepage_uri: https://github.com/tkowalewski/i18nable
|
|
88
|
+
source_code_uri: https://github.com/tkowalewski/i18nable
|
|
89
|
+
changelog_uri: https://github.com/tkowalewski/i18nable/blob/master/CHANGELOG.md
|
|
90
|
+
bug_tracker_uri: https://github.com/tkowalewski/i18nable/issues
|
|
91
|
+
rubygems_mfa_required: 'true'
|
|
102
92
|
rdoc_options: []
|
|
103
93
|
require_paths:
|
|
104
94
|
- lib
|
|
@@ -106,15 +96,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
106
96
|
requirements:
|
|
107
97
|
- - ">="
|
|
108
98
|
- !ruby/object:Gem::Version
|
|
109
|
-
version: '
|
|
99
|
+
version: '3.1'
|
|
110
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
101
|
requirements:
|
|
112
102
|
- - ">="
|
|
113
103
|
- !ruby/object:Gem::Version
|
|
114
104
|
version: '0'
|
|
115
105
|
requirements: []
|
|
116
|
-
rubygems_version: 3.
|
|
117
|
-
signing_key:
|
|
106
|
+
rubygems_version: 3.6.9
|
|
118
107
|
specification_version: 4
|
|
119
108
|
summary: Use I18n within any class
|
|
120
109
|
test_files: []
|