traco 5.0.0 → 5.3.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 +5 -5
- data/CHANGELOG.md +34 -0
- data/lib/traco/attributes.rb +6 -5
- data/lib/traco/class_methods.rb +2 -0
- data/lib/traco/locale_fallbacks.rb +2 -0
- data/lib/traco/version.rb +1 -1
- data/spec/locale_fallbacks_spec.rb +16 -2
- data/spec/spec_helper.rb +3 -0
- data/spec/spec_helper_models.rb +1 -1
- data/spec/traco_spec.rb +45 -14
- metadata +28 -22
- data/.gitignore +0 -5
- data/.travis.yml +0 -10
- data/Gemfile +0 -8
- data/README.md +0 -176
- data/Rakefile +0 -6
- data/benchmarks/overhead.rb +0 -41
- data/traco.gemspec +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8578dcbe5ffad338b2a486d18610d2c018d2a7f1cbf1e051037a0780eee98f30
|
4
|
+
data.tar.gz: c8df336aeedb3649d5629a88076bb11f89ff4e19d465bc8471110df24ea49098
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b2a96991498a6ca004cc5e804a489c856e8e769e3b12d7a1c9194134ca261beed7dd9de383fa46b48a2dfdabdac20bffa4d00667e33c2d7ddc14502c94fcdac
|
7
|
+
data.tar.gz: b0b95f3fc8904fb754554a7af9f468ca26089ae241cc140829c37c3c233fc7982a2d525f473adb8b604901e2467b729717ec17fc8382d473143b2bfedd0396c6
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,39 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 5.3.2
|
4
|
+
|
5
|
+
* This version is only to actually include this CHANGELOG in the gem, so you can see the below if you compare gem diffs 😅
|
6
|
+
|
7
|
+
## 5.3.1
|
8
|
+
|
9
|
+
* This version is only to clarify a backwards incompatibility if relying on undocumented behaviour:
|
10
|
+
|
11
|
+
Readers like `post.title` no longer accept (and ignore) arbitrary keyword arguments.
|
12
|
+
|
13
|
+
So if you would e.g. override a reader like this before:
|
14
|
+
|
15
|
+
def my_title(my_arg:)
|
16
|
+
super || "my fallback #{my_arg}"
|
17
|
+
end
|
18
|
+
|
19
|
+
You will instead need to do:
|
20
|
+
|
21
|
+
def my_title(my_arg:)
|
22
|
+
super() || "my fallback #{my_arg}"
|
23
|
+
end
|
24
|
+
|
25
|
+
## 5.3.0
|
26
|
+
|
27
|
+
* Feature: You can pass a desired locale to readers and query methods, e.g. `post.title(locale: :de)` to get the German-locale title, and `post.title?(locale: :de)` to check if one exists. This ignores fallback settings – use `I18n.with_locale(:de) { post.title }` if you want fallbacks.
|
28
|
+
|
29
|
+
## 5.2.0
|
30
|
+
|
31
|
+
* Feature: `locale_columns` without a passed column name returns all locale columns. Thanks to [manuelmeurer](https://github.com/manuelmeurer)!
|
32
|
+
|
33
|
+
## 5.1.0
|
34
|
+
|
35
|
+
* Feature: Add `fallback: :i18n` to use the fallbacks from `I18n.fallbacks`. Thanks to [sunny](https://github.com/sunny)!
|
36
|
+
|
3
37
|
## 5.0.0
|
4
38
|
|
5
39
|
* Change `locale_columns` and `locales_for_attribute` to sort current locale first, then default locale (previously, it was default locale first). This makes more sense for our own apps, and hopefully other apps as well.
|
data/lib/traco/attributes.rb
CHANGED
@@ -21,10 +21,11 @@ module Traco
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def define_reader(attribute)
|
24
|
+
default_fallback = @options.fetch(:fallback, LocaleFallbacks::DEFAULT_FALLBACK)
|
25
|
+
|
24
26
|
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
25
|
-
def #{attribute}(
|
26
|
-
|
27
|
-
fallback = options.fetch(:fallback, default_fallback)
|
27
|
+
def #{attribute}(locale: nil, fallback: #{default_fallback.inspect})
|
28
|
+
return send(Traco.column(:#{attribute}, locale)) if locale
|
28
29
|
|
29
30
|
columns_to_try = self.class._locale_columns_for_attribute(:#{attribute}, fallback: fallback)
|
30
31
|
columns_to_try.each do |column|
|
@@ -48,8 +49,8 @@ module Traco
|
|
48
49
|
|
49
50
|
def define_query(attribute)
|
50
51
|
class_eval <<-EOM, __FILE__, __LINE__ + 1
|
51
|
-
def #{attribute}?
|
52
|
-
|
52
|
+
def #{attribute}?(locale: nil)
|
53
|
+
send(:#{attribute}, locale: locale).present?
|
53
54
|
end
|
54
55
|
EOM
|
55
56
|
end
|
data/lib/traco/class_methods.rb
CHANGED
@@ -7,6 +7,7 @@ module Traco
|
|
7
7
|
ANY_FALLBACK = :any,
|
8
8
|
NO_FALLBACK = false,
|
9
9
|
DEFAULT_FIRST_FALLBACK = :default_first,
|
10
|
+
I18N_FALLBACK = :i18n,
|
10
11
|
]
|
11
12
|
|
12
13
|
attr_reader :fallback_option
|
@@ -26,6 +27,7 @@ module Traco
|
|
26
27
|
when ANY_FALLBACK then [ current_locale, @default_locale, *@available_locales ].uniq
|
27
28
|
when NO_FALLBACK then [ current_locale ]
|
28
29
|
when DEFAULT_FIRST_FALLBACK then [ @default_locale, *@available_locales ].uniq
|
30
|
+
when I18N_FALLBACK then I18n.fallbacks[current_locale]
|
29
31
|
when Array then [ current_locale, *fallback_option ]
|
30
32
|
else raise "Unknown fallback." # Should never get here.
|
31
33
|
end
|
data/lib/traco/version.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
require "spec_helper"
|
2
1
|
require "traco/locale_fallbacks"
|
3
2
|
|
4
|
-
describe Traco::LocaleFallbacks do
|
3
|
+
RSpec.describe Traco::LocaleFallbacks do
|
5
4
|
describe ".new" do
|
6
5
|
it "raises ArgumentError if an unknown argument passed in" do
|
7
6
|
expect { Traco::LocaleFallbacks.new(:foo) }.to raise_error(ArgumentError)
|
@@ -51,5 +50,20 @@ describe Traco::LocaleFallbacks do
|
|
51
50
|
expect(subject[:sv]).to eq [ :uk, :de, :en, :sv ]
|
52
51
|
end
|
53
52
|
end
|
53
|
+
|
54
|
+
context "with the ':i18n' option" do
|
55
|
+
it "returns what is configured as a I18n fallback" do
|
56
|
+
I18n.fallbacks = I18n::Locale::Fallbacks.new(
|
57
|
+
en: [ :en, :uk, :de, :sv ],
|
58
|
+
uk: [ :uk, :en, :de, :sv ],
|
59
|
+
de: [ :de, :uk, :en, :sv ],
|
60
|
+
sv: [ :sv, :en, :uk, :de ],
|
61
|
+
)
|
62
|
+
|
63
|
+
subject = Traco::LocaleFallbacks.new(:i18n)
|
64
|
+
expect(subject[:sv]).to eq [ :sv, :en, :uk, :de ]
|
65
|
+
expect(subject[:de]).to eq [ :de, :uk, :en, :sv ]
|
66
|
+
end
|
67
|
+
end
|
54
68
|
end
|
55
69
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,6 +3,9 @@ require "i18n"
|
|
3
3
|
RSpec.configure do |config|
|
4
4
|
config.filter_run focus: true
|
5
5
|
config.run_all_when_everything_filtered = true
|
6
|
+
config.disable_monkey_patching!
|
6
7
|
end
|
7
8
|
|
8
9
|
I18n.enforce_available_locales = false
|
10
|
+
|
11
|
+
I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
|
data/spec/spec_helper_models.rb
CHANGED
data/spec/traco_spec.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require "spec_helper"
|
4
3
|
require "spec_helper_models"
|
5
4
|
require "traco"
|
6
5
|
|
7
|
-
describe Traco, ".split_localized_column" do
|
6
|
+
RSpec.describe Traco, ".split_localized_column" do
|
8
7
|
subject { described_class }
|
9
8
|
|
10
9
|
it "returns attribute and locale" do
|
@@ -24,7 +23,7 @@ describe Traco, ".split_localized_column" do
|
|
24
23
|
end
|
25
24
|
end
|
26
25
|
|
27
|
-
describe ActiveRecord::Base, ".translates" do
|
26
|
+
RSpec.describe ActiveRecord::Base, ".translates" do
|
28
27
|
it "is available" do
|
29
28
|
expect(Post).to respond_to :translates
|
30
29
|
end
|
@@ -57,7 +56,7 @@ describe ActiveRecord::Base, ".translates" do
|
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
|
-
describe Post, ".translatable_attributes" do
|
59
|
+
RSpec.describe Post, ".translatable_attributes" do
|
61
60
|
before do
|
62
61
|
Post.translates :title
|
63
62
|
end
|
@@ -77,7 +76,7 @@ describe Post, ".translatable_attributes" do
|
|
77
76
|
end
|
78
77
|
end
|
79
78
|
|
80
|
-
describe Post, ".locales_for_attribute" do
|
79
|
+
RSpec.describe Post, ".locales_for_attribute" do
|
81
80
|
before do
|
82
81
|
Post.translates :title
|
83
82
|
I18n.default_locale = :"pt-BR"
|
@@ -95,7 +94,7 @@ describe Post, ".locales_for_attribute" do
|
|
95
94
|
end
|
96
95
|
end
|
97
96
|
|
98
|
-
describe Post, ".locale_columns" do
|
97
|
+
RSpec.describe Post, ".locale_columns" do
|
99
98
|
before do
|
100
99
|
Post.translates :title
|
101
100
|
I18n.default_locale = :"pt-BR"
|
@@ -114,17 +113,23 @@ describe Post, ".locale_columns" do
|
|
114
113
|
expect(Post.locale_columns(:title)).not_to include(:title_ru)
|
115
114
|
end
|
116
115
|
|
117
|
-
it "supports multiple attributes" do
|
118
|
-
|
116
|
+
it "supports multiple attributes" do
|
117
|
+
Post.translates :body
|
119
118
|
|
120
119
|
expect(Post.locale_columns(:body, :title)).to eq [
|
121
120
|
:body_en, :body_pt_br, :body_de, :body_sv,
|
122
121
|
:title_en, :title_pt_br, :title_de, :title_sv,
|
123
122
|
]
|
124
123
|
end
|
124
|
+
|
125
|
+
it "returns the columns of all translated attributes if no params are supplied" do
|
126
|
+
expect(Post.locale_columns).to eq [
|
127
|
+
:title_en, :title_pt_br, :title_de, :title_sv
|
128
|
+
]
|
129
|
+
end
|
125
130
|
end
|
126
131
|
|
127
|
-
describe Post, ".current_locale_column" do
|
132
|
+
RSpec.describe Post, ".current_locale_column" do
|
128
133
|
before do
|
129
134
|
Post.translates :title
|
130
135
|
end
|
@@ -140,7 +145,7 @@ describe Post, ".current_locale_column" do
|
|
140
145
|
end
|
141
146
|
end
|
142
147
|
|
143
|
-
describe Post, "#title" do
|
148
|
+
RSpec.describe Post, "#title" do
|
144
149
|
let(:post) {
|
145
150
|
Post.new(title_sv: "Hej", title_en: "Halloa", title_pt_br: "Olá")
|
146
151
|
}
|
@@ -225,6 +230,19 @@ describe Post, "#title" do
|
|
225
230
|
expect(post.title).to eq "Hej"
|
226
231
|
end
|
227
232
|
|
233
|
+
context "when given a 'locale' argument" do
|
234
|
+
it "gives the title in that locale, without fallback" do
|
235
|
+
post = Post.new(title_sv: "Hej", title_en: "Halloa")
|
236
|
+
I18n.default_locale = :en
|
237
|
+
I18n.locale = :en
|
238
|
+
|
239
|
+
expect(post.title(locale: :sv)).to eq "Hej"
|
240
|
+
|
241
|
+
post.title_sv = nil
|
242
|
+
expect(post.title(locale: :sv)).to eq nil
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
228
246
|
context "when the translation was defined with fallback: false" do
|
229
247
|
let(:post) {
|
230
248
|
Post.new(title_sv: "Hej", title_en: "Halloa")
|
@@ -271,7 +289,7 @@ describe Post, "#title" do
|
|
271
289
|
end
|
272
290
|
end
|
273
291
|
|
274
|
-
describe Post, "#title=" do
|
292
|
+
RSpec.describe Post, "#title=" do
|
275
293
|
before do
|
276
294
|
Post.translates :title
|
277
295
|
end
|
@@ -298,14 +316,14 @@ describe Post, "#title=" do
|
|
298
316
|
end
|
299
317
|
end
|
300
318
|
|
301
|
-
describe Post, "#title?" do
|
319
|
+
RSpec.describe Post, "#title?" do
|
302
320
|
before do
|
303
321
|
Post.translates :title
|
304
322
|
I18n.locale = :sv
|
305
323
|
I18n.default_locale = :en
|
306
324
|
end
|
307
325
|
|
308
|
-
it "is true if the title is present" do
|
326
|
+
it "is true if the title is present, with fallback" do
|
309
327
|
post = Post.new(title_sv: "", title_en: " ", title_pt_br: nil)
|
310
328
|
expect(post.title?).to be false
|
311
329
|
|
@@ -313,9 +331,22 @@ describe Post, "#title?" do
|
|
313
331
|
|
314
332
|
expect(post.title?).to be true
|
315
333
|
end
|
334
|
+
|
335
|
+
context "when given a 'locale' argument" do
|
336
|
+
it "is true if the title if present in that locale, with no fallback" do
|
337
|
+
post = Post.new(title_sv: "Hej", title_en: "Halloa")
|
338
|
+
I18n.default_locale = :en
|
339
|
+
I18n.locale = :en
|
340
|
+
|
341
|
+
expect(post.title?(locale: :sv)).to be true
|
342
|
+
|
343
|
+
post.title_sv = nil
|
344
|
+
expect(post.title?(locale: :sv)).to be false
|
345
|
+
end
|
346
|
+
end
|
316
347
|
end
|
317
348
|
|
318
|
-
describe Post, ".human_attribute_name" do
|
349
|
+
RSpec.describe Post, ".human_attribute_name" do
|
319
350
|
before do
|
320
351
|
Post.translates :title
|
321
352
|
I18n.locale = :sv
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,21 +66,29 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: appraisal
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
70
84
|
email:
|
71
85
|
- henrik@barsoom.se
|
72
86
|
executables: []
|
73
87
|
extensions: []
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
|
-
- ".gitignore"
|
77
|
-
- ".travis.yml"
|
78
90
|
- CHANGELOG.md
|
79
|
-
- Gemfile
|
80
91
|
- LICENSE.txt
|
81
|
-
- README.md
|
82
|
-
- Rakefile
|
83
|
-
- benchmarks/overhead.rb
|
84
92
|
- lib/traco.rb
|
85
93
|
- lib/traco/attributes.rb
|
86
94
|
- lib/traco/class_methods.rb
|
@@ -93,12 +101,11 @@ files:
|
|
93
101
|
- spec/spec_helper.rb
|
94
102
|
- spec/spec_helper_models.rb
|
95
103
|
- spec/traco_spec.rb
|
96
|
-
- traco.gemspec
|
97
104
|
homepage: ''
|
98
105
|
licenses:
|
99
106
|
- MIT
|
100
107
|
metadata: {}
|
101
|
-
post_install_message:
|
108
|
+
post_install_message:
|
102
109
|
rdoc_options: []
|
103
110
|
require_paths:
|
104
111
|
- lib
|
@@ -113,15 +120,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
120
|
- !ruby/object:Gem::Version
|
114
121
|
version: '0'
|
115
122
|
requirements: []
|
116
|
-
|
117
|
-
|
118
|
-
signing_key:
|
123
|
+
rubygems_version: 3.1.4
|
124
|
+
signing_key:
|
119
125
|
specification_version: 4
|
120
|
-
summary: Translatable columns for Rails
|
126
|
+
summary: Translatable columns for Rails 4.2 or better, stored in the model table itself.
|
121
127
|
test_files:
|
122
|
-
- spec/app/post.rb
|
123
|
-
- spec/app/sv.yml
|
124
|
-
- spec/locale_fallbacks_spec.rb
|
125
128
|
- spec/spec_helper.rb
|
126
|
-
- spec/
|
129
|
+
- spec/app/sv.yml
|
130
|
+
- spec/app/post.rb
|
127
131
|
- spec/traco_spec.rb
|
132
|
+
- spec/spec_helper_models.rb
|
133
|
+
- spec/locale_fallbacks_spec.rb
|
data/.travis.yml
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
rvm:
|
3
|
-
- 2.5.1
|
4
|
-
- 2.4.4
|
5
|
-
- 2.3.0
|
6
|
-
|
7
|
-
# Avoid some errors, and speed things up:
|
8
|
-
# https://github.com/bundler/bundler/issues/3558#issuecomment-171347979
|
9
|
-
# https://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
10
|
-
sudo: false
|
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,176 +0,0 @@
|
|
1
|
-
# Traco
|
2
|
-
|
3
|
-
[](http://travis-ci.org/barsoom/traco)
|
4
|
-
|
5
|
-
Translatable attributes for Ruby on Rails, stored in the model table itself.
|
6
|
-
|
7
|
-
Inspired by Iain Hecker's [translatable_columns](https://github.com/iain/translatable_columns/).
|
8
|
-
|
9
|
-
To store translations outside the model, see Sven Fuchs' [Globalize](https://github.com/globalize/globalize).
|
10
|
-
|
11
|
-
|
12
|
-
## Usage
|
13
|
-
|
14
|
-
Say you want `Post#title` and `Post#body` to support both English and Swedish values.
|
15
|
-
|
16
|
-
Write a migration to get database columns with locale suffixes, e.g. `title_sv` and `title_en`, like:
|
17
|
-
|
18
|
-
```ruby
|
19
|
-
class CreatePosts < ActiveRecord::Migration
|
20
|
-
def change
|
21
|
-
create_table :posts do |t|
|
22
|
-
t.string :title_sv, :title_en
|
23
|
-
t.text :body_sv, :body_en
|
24
|
-
|
25
|
-
t.timestamps
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
```
|
30
|
-
|
31
|
-
Don't create a database column named `title` without a suffix, since Traco will define a method with that name.
|
32
|
-
|
33
|
-
If you use a locale format like `pt-BR`, the column name would be `title_pt_br`.
|
34
|
-
|
35
|
-
Declare the attributes in the model:
|
36
|
-
|
37
|
-
```ruby
|
38
|
-
class Post < ActiveRecord::Base
|
39
|
-
translates :title, :body
|
40
|
-
end
|
41
|
-
```
|
42
|
-
|
43
|
-
You can still use your accessors like `title_sv` and `title_sv=` in forms, validations and other code, but you also get:
|
44
|
-
|
45
|
-
`#title`: Shows the title in the current locale. If blank, falls back to default locale. Otherwise nil.
|
46
|
-
|
47
|
-
`#title=`: Assigns the title to the column for the current locale, if present. Raises if the column doesn't exist.
|
48
|
-
|
49
|
-
`#title?`: Is the title present? Respects the Traco [fallback](#fallbacks) setting.
|
50
|
-
|
51
|
-
`.human_attribute_name(:title_sv)`: Extends this standard method to return "Title (Swedish)" if you have a translation key `i18n.languages.sv = "Swedish"` and "Title (SV)" otherwise. Rails uses this method to build validation error messages and form labels.
|
52
|
-
|
53
|
-
`.translatable_attributes`: Returns an array like `[:title, :body]`.
|
54
|
-
|
55
|
-
`.locale_columns(:title)`: Returns an array like `[:title_sv, :title_en]` sorted with current locale first, then default locale, and then alphabetically. Suitable for looping in forms:
|
56
|
-
|
57
|
-
```erb
|
58
|
-
<% Post.locale_columns(:title).each do |column| %>
|
59
|
-
<p>
|
60
|
-
<%= form.label column %>
|
61
|
-
<%= form.text_field column %>
|
62
|
-
</p>
|
63
|
-
<% end %>
|
64
|
-
```
|
65
|
-
|
66
|
-
Or perhaps for things like:
|
67
|
-
|
68
|
-
```ruby
|
69
|
-
attr_accessible *locale_columns(:title)
|
70
|
-
|
71
|
-
validates *locale_columns(:title), :uniqueness => true
|
72
|
-
```
|
73
|
-
|
74
|
-
You can also pass multiple attributes if you like:
|
75
|
-
|
76
|
-
```ruby
|
77
|
-
attr_accessible *locale_columns(:title, :body)
|
78
|
-
```
|
79
|
-
|
80
|
-
The return value will be sorted like `[:title_sv, :title_en, :body_sv, :body_en]`.
|
81
|
-
|
82
|
-
`.current_locale_column(:title)`: Returns `:title_sv` if `:sv` is the current locale. Suitable for some SQL queries, such as sorting.
|
83
|
-
|
84
|
-
`.locales_for_attribute(:title)`: Returns an array like `[:sv, :en]` sorted with current locale first, then default locale, and then alphabetically.
|
85
|
-
|
86
|
-
And the equivalent methods for `body`, of course.
|
87
|
-
|
88
|
-
Please note that your `translates :title, :body` declaration must be called before you call `locale_columns`. Otherwise you will get an error like "NoMethodError: undefined method `locale\_columns' for #\<Class:0x00000003f69188\>".
|
89
|
-
|
90
|
-
|
91
|
-
### Fallbacks
|
92
|
-
|
93
|
-
By default, Traco will fall back to the default locale if there is no translation in the current locale.
|
94
|
-
|
95
|
-
You can specify e.g. `translates :title, fallback: false` to never fall back and instead return `nil`.
|
96
|
-
|
97
|
-
You can specify e.g. `translates :title, fallback: :any` to fall back first to the default locale, then to any other locale.
|
98
|
-
|
99
|
-
You can specify e.g. `translates :title, fallback: [:sv]` to explicitly declare fallbacks as an array of any length.
|
100
|
-
|
101
|
-
You can override the default fallback strategy with a parameter passed to the reader: `post.title(fallback: :any)`.
|
102
|
-
|
103
|
-
If you need to declare the default locale fallback, do `post.title(fallback: :default)`.
|
104
|
-
|
105
|
-
|
106
|
-
### Overriding methods
|
107
|
-
|
108
|
-
Methods are defined in an included module, so you can just override them and call Traco's implementation with `super`:
|
109
|
-
|
110
|
-
```ruby
|
111
|
-
class Post < ActiveRecord::Base
|
112
|
-
translates :title
|
113
|
-
|
114
|
-
def title
|
115
|
-
super.reverse
|
116
|
-
end
|
117
|
-
end
|
118
|
-
```
|
119
|
-
|
120
|
-
## Installation
|
121
|
-
|
122
|
-
Add this to your `Gemfile`:
|
123
|
-
|
124
|
-
```ruby
|
125
|
-
gem "traco"
|
126
|
-
```
|
127
|
-
|
128
|
-
Then run
|
129
|
-
|
130
|
-
bundle
|
131
|
-
|
132
|
-
to install it.
|
133
|
-
|
134
|
-
|
135
|
-
## Running the tests
|
136
|
-
|
137
|
-
bundle
|
138
|
-
rake
|
139
|
-
|
140
|
-
|
141
|
-
## Benchmark
|
142
|
-
|
143
|
-
ruby benchmarks/overhead.rb
|
144
|
-
|
145
|
-
|
146
|
-
<!-- Keeping this a hidden brain dump for now.
|
147
|
-
|
148
|
-
## TODO
|
149
|
-
|
150
|
-
We've intentionally kept this simple with no features we do not need.
|
151
|
-
We'd be happy to merge additional features that others contribute.
|
152
|
-
|
153
|
-
Possible improvements to make:
|
154
|
-
|
155
|
-
* Validation that checks that at least one translation for a column exists.
|
156
|
-
* Validation that checks that every translation for a column exists.
|
157
|
-
* Scopes like `translated`, `translated_to(locale)`.
|
158
|
-
* Support for region locales, like `en-US` and `en-GB`.
|
159
|
-
|
160
|
-
-->
|
161
|
-
|
162
|
-
## Contributors
|
163
|
-
|
164
|
-
* [Henrik Nyh](http://henrik.nyh.se)
|
165
|
-
* Andrii Malyshko
|
166
|
-
* Tobias Bohwalli
|
167
|
-
* Mario Alberto Chavez
|
168
|
-
* Philip Arndt
|
169
|
-
* [PikachuEXE](https://github.com/PikachuEXE)
|
170
|
-
* Fernando Morgenstern
|
171
|
-
* Tomáš Horáček
|
172
|
-
* Joakim Kolsjö
|
173
|
-
|
174
|
-
## License
|
175
|
-
|
176
|
-
[MIT](LICENSE.txt)
|
data/Rakefile
DELETED
data/benchmarks/overhead.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# This benchmark tests how fast a Traco-wrapped attribute is
|
2
|
-
# compared to the plain Active Record attribute.
|
3
|
-
|
4
|
-
require "bundler/setup"
|
5
|
-
require "benchmark/ips"
|
6
|
-
require "active_record"
|
7
|
-
require "traco"
|
8
|
-
|
9
|
-
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
10
|
-
|
11
|
-
I18n.enforce_available_locales = false
|
12
|
-
I18n.available_locales = [ :en, :de, :sv ]
|
13
|
-
I18n.default_locale = :en
|
14
|
-
I18n.locale = :sv
|
15
|
-
|
16
|
-
COLUMNS = %w(title body long_title seo_title)
|
17
|
-
|
18
|
-
silence_stream(STDOUT) do
|
19
|
-
ActiveRecord::Schema.define(version: 0) do
|
20
|
-
create_table :posts, force: true do |t|
|
21
|
-
I18n.available_locales.each do |locale|
|
22
|
-
COLUMNS.each do |column|
|
23
|
-
t.string "#{column}_#{locale}"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class Post < ActiveRecord::Base
|
31
|
-
translates *COLUMNS
|
32
|
-
end
|
33
|
-
|
34
|
-
post = Post.new(title_en: "hello", title_sv: "Hej")
|
35
|
-
|
36
|
-
Benchmark.ips do |x|
|
37
|
-
x.report("activerecord") { post.title_sv }
|
38
|
-
x.report("traco") { post.title }
|
39
|
-
|
40
|
-
x.compare!
|
41
|
-
end
|
data/traco.gemspec
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "traco/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "traco"
|
7
|
-
s.version = Traco::VERSION
|
8
|
-
s.authors = ["Henrik Nyh"]
|
9
|
-
s.email = ["henrik@barsoom.se"]
|
10
|
-
s.homepage = ""
|
11
|
-
s.summary = "Translatable columns for Rails 3 or better, stored in the model table itself."
|
12
|
-
s.license = "MIT"
|
13
|
-
|
14
|
-
s.files = `git ls-files`.split("\n")
|
15
|
-
s.test_files = `git ls-files -- spec/*`.split("\n")
|
16
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
-
s.require_paths = ["lib"]
|
18
|
-
|
19
|
-
s.required_ruby_version = ">= 2.3.0"
|
20
|
-
|
21
|
-
s.add_dependency "activerecord", ">= 3.0"
|
22
|
-
|
23
|
-
s.add_development_dependency "sqlite3"
|
24
|
-
|
25
|
-
s.add_development_dependency "rake"
|
26
|
-
s.add_development_dependency "rspec"
|
27
|
-
end
|