is_translatable 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +8 -4
- data/lib/generators/is_translatable/migration_generator.rb +26 -0
- data/lib/{rails/generators/is_translatable_migration → generators/is_translatable}/templates/migration.rb +0 -0
- data/lib/is_translatable/version.rb +1 -1
- data/lib/is_translatable.rb +5 -0
- data/spec/lib/is_translatable_spec.rb +11 -2
- metadata +9 -9
- data/lib/rails/generators/is_translatable_migration/is_translatable_migration_generator.rb +0 -24
data/README.markdown
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
# is\_translatable
|
2
2
|
|
3
|
-
(development in progress)
|
4
|
-
|
5
3
|
This is a simple library geared towards translating dynamic text in your database.
|
6
|
-
Unlike
|
4
|
+
Unlike many similar libaries, is_translatable uses a single table for all translations, so once it's hooked up you won't
|
7
5
|
need to do anything fancy in your DB migrations to add support for translations.
|
8
6
|
|
9
7
|
## Usage
|
@@ -30,7 +28,13 @@ TODO: hookup behavior for default locale and fallbacking.
|
|
30
28
|
|
31
29
|
## Installation
|
32
30
|
|
33
|
-
|
31
|
+
Add the following line to your Gemfile:
|
32
|
+
|
33
|
+
gem 'is_translatable'
|
34
|
+
|
35
|
+
Then run:
|
36
|
+
|
37
|
+
bundle exec rails g is_translatable_migration
|
34
38
|
|
35
39
|
## Credits
|
36
40
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
# This article helps figure out what's going on with Rails 3 generators:
|
5
|
+
# http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
|
6
|
+
module IsTranslatable
|
7
|
+
class MigrationGenerator < Rails::Generators::Base
|
8
|
+
include Rails::Generators::Migration
|
9
|
+
source_root File.expand_path('../templates', __FILE__)
|
10
|
+
|
11
|
+
# Ugh :(
|
12
|
+
# Implement the required interface for Rails::Generators::Migration.
|
13
|
+
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
14
|
+
def self.next_migration_number(dirname)
|
15
|
+
if ActiveRecord::Base.timestamped_migrations
|
16
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
17
|
+
else
|
18
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_translatable_migration
|
23
|
+
migration_template 'migration.rb', 'db/migrate/is_translatable_migration'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
File without changes
|
data/lib/is_translatable.rb
CHANGED
@@ -26,6 +26,10 @@ module IsTranslatable
|
|
26
26
|
def set_translation(kind, t, locale=nil)
|
27
27
|
validate_kind(kind)
|
28
28
|
locale ||= I18n.locale
|
29
|
+
if locale.to_s == I18n.default_locale.to_s
|
30
|
+
write_attribute(kind, t)
|
31
|
+
return
|
32
|
+
end
|
29
33
|
t_obj = find_translation(kind, locale)
|
30
34
|
if t_obj.nil?
|
31
35
|
translations.build({:kind => kind.to_s, :translation => t, :locale => locale.to_s})
|
@@ -37,6 +41,7 @@ module IsTranslatable
|
|
37
41
|
def get_translation(kind, locale=nil)
|
38
42
|
validate_kind(kind)
|
39
43
|
locale ||= I18n.locale
|
44
|
+
return read_attribute(kind) if locale.to_s == I18n.default_locale.to_s
|
40
45
|
t = translations.find_by_kind(kind.to_s, :conditions => {:locale => locale.to_s})
|
41
46
|
t ||= find_translation(kind, locale)
|
42
47
|
t.translation unless t.nil?
|
@@ -11,7 +11,6 @@ describe IsTranslatable do
|
|
11
11
|
}
|
12
12
|
end
|
13
13
|
|
14
|
-
it 'should test default locale (just using the actual field)'
|
15
14
|
it 'should be even more awesome'
|
16
15
|
|
17
16
|
context 'article translations' do
|
@@ -21,6 +20,17 @@ describe IsTranslatable do
|
|
21
20
|
specify {subject.save.should be_true}
|
22
21
|
it {should be_valid}
|
23
22
|
|
23
|
+
context 'with default locale' do
|
24
|
+
before {I18n.locale = :en}
|
25
|
+
it {subject.get_translation(:title).should == @titles[:en]}
|
26
|
+
|
27
|
+
context 'with title translation' do
|
28
|
+
before {@article.set_translation(:title, 'override')}
|
29
|
+
it {subject.get_translation(:title).should == 'override'}
|
30
|
+
it {subject.title.should == 'override'}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
24
34
|
context 'with spanish locale' do
|
25
35
|
before {I18n.locale = :es}
|
26
36
|
|
@@ -67,7 +77,6 @@ describe IsTranslatable do
|
|
67
77
|
before {@article.set_translation(:title, @titles[:fr], :fr)}
|
68
78
|
it {should be_valid}
|
69
79
|
|
70
|
-
it 'should allow specifying fallback behavior?'
|
71
80
|
it {subject.get_translation(:title).should == nil}
|
72
81
|
it {subject.get_translation(:title, :fr).should == @titles[:fr]}
|
73
82
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: is_translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-12-15 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70114897641700 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.2
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70114897641700
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3
|
27
|
-
requirement: &
|
27
|
+
requirement: &70114897641260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70114897641260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70114897640700 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70114897640700
|
47
47
|
description:
|
48
48
|
email:
|
49
49
|
- dixo0015+is_translatable@gmail.com
|
@@ -51,10 +51,10 @@ executables: []
|
|
51
51
|
extensions: []
|
52
52
|
extra_rdoc_files: []
|
53
53
|
files:
|
54
|
+
- lib/generators/is_translatable/migration_generator.rb
|
55
|
+
- lib/generators/is_translatable/templates/migration.rb
|
54
56
|
- lib/is_translatable/version.rb
|
55
57
|
- lib/is_translatable.rb
|
56
|
-
- lib/rails/generators/is_translatable_migration/is_translatable_migration_generator.rb
|
57
|
-
- lib/rails/generators/is_translatable_migration/templates/migration.rb
|
58
58
|
- lib/tasks/is_translatable_tasks.rake
|
59
59
|
- lib/translation.rb
|
60
60
|
- MIT-LICENSE
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'rails/generators'
|
2
|
-
require 'rails/generators/migration'
|
3
|
-
|
4
|
-
# This article helps figure out what's going on with Rails 3 generators:
|
5
|
-
# http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
|
6
|
-
class IsTranslatableMigrationGenerator < Rails::Generators::Base
|
7
|
-
include Rails::Generators::Migration
|
8
|
-
source_root File.expand_path('../templates', __FILE__)
|
9
|
-
|
10
|
-
# Ugh :(
|
11
|
-
# Implement the required interface for Rails::Generators::Migration.
|
12
|
-
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
13
|
-
def self.next_migration_number(dirname)
|
14
|
-
if ActiveRecord::Base.timestamped_migrations
|
15
|
-
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
16
|
-
else
|
17
|
-
"%.3d" % (current_migration_number(dirname) + 1)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def create_translatable_migration
|
22
|
-
migration_template 'migration.rb', 'db/migrate/is_translatable_migration'
|
23
|
-
end
|
24
|
-
end
|