traco 4.0.0 → 5.3.1
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/lib/traco/attributes.rb +6 -5
- data/lib/traco/class_methods.rb +6 -4
- 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 +3 -3
- data/spec/traco_spec.rb +49 -18
- metadata +28 -23
- data/.gitignore +0 -5
- data/.travis.yml +0 -10
- data/CHANGELOG.md +0 -71
- 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: 757d10aa7918278367c530ff4a7e52fa65d607662b06290b4d2df47450e64ed6
|
4
|
+
data.tar.gz: a4e5cde1acefb4d6efc458ceaaea3ca27ee8b1e58ff8a7c4c50ef618feb3c748
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 922f7a02f82a244ff7f4a7c6ea352059a9025b45bf4893fea120cd2997c5df4e53cbfdcd7f9431e09c01898b95e0cc58a004c66bc28c8fdf4409c84e4e11f7b0
|
7
|
+
data.tar.gz: e7214616b01da7c33956a7bc85cfcb14c5ac0385af2187bb666fe50a3308243a9e9073cc02d64841d312a9875bfc85615d1323a14c0ecee6e164754371a242c8
|
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
@@ -1,13 +1,15 @@
|
|
1
1
|
module Traco
|
2
2
|
module ClassMethods
|
3
3
|
def locales_for_attribute(attribute)
|
4
|
-
traco_cache(attribute, fallback: LocaleFallbacks::
|
4
|
+
traco_cache(attribute, fallback: LocaleFallbacks::ANY_FALLBACK).keys
|
5
5
|
end
|
6
6
|
|
7
7
|
def locale_columns(*attributes)
|
8
|
-
attributes.
|
9
|
-
|
10
|
-
|
8
|
+
attributes = attributes.presence || translatable_attributes
|
9
|
+
|
10
|
+
attributes.flat_map { |attribute|
|
11
|
+
_locale_columns_for_attribute(attribute, fallback: LocaleFallbacks::ANY_FALLBACK)
|
12
|
+
}
|
11
13
|
end
|
12
14
|
|
13
15
|
# Consider this method internal.
|
@@ -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
@@ -1,5 +1,5 @@
|
|
1
1
|
RSpec.configure do |config|
|
2
|
-
config.before
|
2
|
+
config.before do
|
3
3
|
# Clear class state before each spec.
|
4
4
|
Object.send(:remove_const, "Post")
|
5
5
|
Object.send(:remove_const, "SubPost")
|
@@ -21,8 +21,8 @@ ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
|
21
21
|
|
22
22
|
ActiveRecord::Schema.define(version: 0) do
|
23
23
|
create_table :posts, force: true do |t|
|
24
|
-
t.string :title_sv, :title_en, :title_pt_br
|
25
|
-
t.string :body_sv, :body_en, :body_pt_br
|
24
|
+
t.string :title_sv, :title_en, :title_pt_br, :title_de
|
25
|
+
t.string :body_sv, :body_en, :body_pt_br, :body_de
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
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,15 +76,15 @@ 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"
|
84
83
|
I18n.locale = :en
|
85
84
|
end
|
86
85
|
|
87
|
-
it "lists the locales for that attribute,
|
88
|
-
expect(Post.locales_for_attribute(:title)).to eq [ :"pt-BR", :
|
86
|
+
it "lists the locales for that attribute, current locale first, then default locale, and then alphabetically" do
|
87
|
+
expect(Post.locales_for_attribute(:title)).to eq [ :en, :"pt-BR", :de, :sv ]
|
89
88
|
end
|
90
89
|
|
91
90
|
it "doesn't include a locale if there's no corresponding column for it" do
|
@@ -95,16 +94,16 @@ 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"
|
102
101
|
I18n.locale = :en
|
103
102
|
end
|
104
103
|
|
105
|
-
it "lists the columns-with-locale for that attribute,
|
104
|
+
it "lists the columns-with-locale for that attribute, current locale first, then default locale, and then alphabetically" do
|
106
105
|
expect(Post.locale_columns(:title)).to eq [
|
107
|
-
:title_pt_br, :
|
106
|
+
:title_en, :title_pt_br, :title_de, :title_sv,
|
108
107
|
]
|
109
108
|
end
|
110
109
|
|
@@ -118,13 +117,19 @@ describe Post, ".locale_columns" do
|
|
118
117
|
Post.translates :body
|
119
118
|
|
120
119
|
expect(Post.locale_columns(:body, :title)).to eq [
|
121
|
-
:body_pt_br, :
|
122
|
-
:title_pt_br, :
|
120
|
+
:body_en, :body_pt_br, :body_de, :body_sv,
|
121
|
+
:title_en, :title_pt_br, :title_de, :title_sv,
|
122
|
+
]
|
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
|
123
128
|
]
|
124
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:
|
4
|
+
version: 5.3.1
|
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,28 @@ 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
|
-
- CHANGELOG.md
|
79
|
-
- Gemfile
|
80
90
|
- LICENSE.txt
|
81
|
-
- README.md
|
82
|
-
- Rakefile
|
83
|
-
- benchmarks/overhead.rb
|
84
91
|
- lib/traco.rb
|
85
92
|
- lib/traco/attributes.rb
|
86
93
|
- lib/traco/class_methods.rb
|
@@ -93,12 +100,11 @@ files:
|
|
93
100
|
- spec/spec_helper.rb
|
94
101
|
- spec/spec_helper_models.rb
|
95
102
|
- spec/traco_spec.rb
|
96
|
-
- traco.gemspec
|
97
103
|
homepage: ''
|
98
104
|
licenses:
|
99
105
|
- MIT
|
100
106
|
metadata: {}
|
101
|
-
post_install_message:
|
107
|
+
post_install_message:
|
102
108
|
rdoc_options: []
|
103
109
|
require_paths:
|
104
110
|
- lib
|
@@ -113,15 +119,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
119
|
- !ruby/object:Gem::Version
|
114
120
|
version: '0'
|
115
121
|
requirements: []
|
116
|
-
|
117
|
-
|
118
|
-
signing_key:
|
122
|
+
rubygems_version: 3.1.4
|
123
|
+
signing_key:
|
119
124
|
specification_version: 4
|
120
|
-
summary: Translatable columns for Rails
|
125
|
+
summary: Translatable columns for Rails 4.2 or better, stored in the model table itself.
|
121
126
|
test_files:
|
122
|
-
- spec/app/post.rb
|
123
|
-
- spec/app/sv.yml
|
124
|
-
- spec/locale_fallbacks_spec.rb
|
125
127
|
- spec/spec_helper.rb
|
126
|
-
- spec/
|
128
|
+
- spec/app/sv.yml
|
129
|
+
- spec/app/post.rb
|
127
130
|
- spec/traco_spec.rb
|
131
|
+
- spec/spec_helper_models.rb
|
132
|
+
- 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/CHANGELOG.md
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
# Changelog
|
2
|
-
|
3
|
-
## 4.0.0
|
4
|
-
|
5
|
-
* Drop support for end-of-lifed Ruby 2.1 and 2.2.
|
6
|
-
|
7
|
-
## 3.3.0
|
8
|
-
|
9
|
-
* Traco now automatically adds query methods, e.g. `Item#title?` when `title` is translated.
|
10
|
-
|
11
|
-
## 3.2.2
|
12
|
-
|
13
|
-
* Internal cleanup.
|
14
|
-
|
15
|
-
## 3.2.1
|
16
|
-
|
17
|
-
* Bugfix: with `fallback: [:sv]`, always look at current locale before any fallbacks.
|
18
|
-
|
19
|
-
## 3.2.0
|
20
|
-
|
21
|
-
* Introduce e.g. `fallback: [:sv]` to explicitly specify fallbacks.
|
22
|
-
|
23
|
-
## 3.1.6
|
24
|
-
|
25
|
-
Make [license (MIT)](LICENSE.txt) easier to autodetect.
|
26
|
-
|
27
|
-
## 3.1.5
|
28
|
-
|
29
|
-
* Bugfix: don't raise error loading models before the DB is created. Thanks to PikachuEXE and Andrii Malyshko.
|
30
|
-
|
31
|
-
## 3.1.4
|
32
|
-
|
33
|
-
* Bugfix: restore sorting of `locale_columns` and `locales_for_attribute` to put default locale first, not current locale. Thanks to PikachuEXE.
|
34
|
-
|
35
|
-
## 3.1.3
|
36
|
-
|
37
|
-
* ~20 time speedup thanks to optimizations by Andrii Malyshko.
|
38
|
-
|
39
|
-
Reading a Traco translated attribute used to be ~250x slower than an untranslated attribute; now it's down to ~10x slower.
|
40
|
-
|
41
|
-
## 3.1.2
|
42
|
-
|
43
|
-
* Bugfix: `.current_locale_column` handles dashed locales like "pt-BR" correctly. Thanks to PikachuEXE.
|
44
|
-
|
45
|
-
## 3.1.1
|
46
|
-
|
47
|
-
* Bugfix around fallbacks and memoization. Thanks to PikachuEXE.
|
48
|
-
|
49
|
-
## 3.1.0
|
50
|
-
|
51
|
-
* Introduce `.current_locale_column`, e.g. `Post.current_locale_column(:title) # => :title_sv`.
|
52
|
-
|
53
|
-
## 3.0.0
|
54
|
-
|
55
|
-
* Backwards incompatible: `fallback: true` is now `fallback: :default`. Since this was the implicit default value, you shouldn't have a problem unless you explicitly declared this value.
|
56
|
-
|
57
|
-
## 2.2.0
|
58
|
-
|
59
|
-
* `fallback: :any` to fall back to any other locale if the text is blank in the current and default locales.
|
60
|
-
|
61
|
-
## 2.1.0
|
62
|
-
|
63
|
-
* Attribute readers can override the attribute's `fallback` setting, e.g. `item.title(fallback: false)`.
|
64
|
-
|
65
|
-
## 2.0.0
|
66
|
-
|
67
|
-
* Backwards incompatible: for dashed locales like "pt-BR", the column names are now expected to end in e.g. `_pt_br`, not `_pt-BR`.
|
68
|
-
|
69
|
-
## 1.3.0
|
70
|
-
|
71
|
-
Whatever we had before introducing this changelog.
|
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 default locale first 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 default locale first 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
|