i18n-one_sky 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -3,3 +3,5 @@
3
3
  == 0.0.1 (03 Jan 2010)
4
4
  * Initial release.
5
5
 
6
+ == 0.0.3 (12 Sep 2011)
7
+ * allows using Active Record as I18n backend. Instead of downloading as files, translated text are stored into Active Record database.
data/CREDITS CHANGED
@@ -4,3 +4,7 @@ Junjun Olympia (@beljun), http://rubyredtomatoes.com
4
4
 
5
5
  * Initial code.
6
6
 
7
+
8
+ Eddie Lau (@tatonlto), http://github.com/3dd13
9
+
10
+ * use Active Record to store translated phrases
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- i18n-one_sky (0.0.1)
4
+ i18n-one_sky (0.0.3)
5
5
  i18n (~> 0.5.0)
6
+ i18n-active_record (~> 0.0.2)
6
7
  one_sky (~> 0.0.2)
7
8
  thor (~> 0.14.4)
8
9
 
@@ -11,12 +12,14 @@ GEM
11
12
  specs:
12
13
  diff-lcs (1.1.2)
13
14
  i18n (0.5.0)
15
+ i18n-active_record (0.0.2)
16
+ i18n (>= 0.5.0)
14
17
  json (1.4.6)
15
18
  mime-types (1.16)
16
19
  one_sky (0.0.2)
17
20
  json (~> 1.4.6)
18
21
  rest-client (~> 1.6.1)
19
- rest-client (1.6.1)
22
+ rest-client (1.6.7)
20
23
  mime-types (>= 1.16)
21
24
  rspec (2.2.0)
22
25
  rspec-core (~> 2.2)
@@ -33,8 +36,5 @@ PLATFORMS
33
36
 
34
37
  DEPENDENCIES
35
38
  bundler (~> 1.0.0)
36
- i18n (~> 0.5.0)
37
39
  i18n-one_sky!
38
- one_sky (~> 0.0.2)
39
40
  rspec (~> 2.2.0)
40
- thor (~> 0.14.4)
data/README.rdoc CHANGED
@@ -4,6 +4,9 @@ OneSky[link:http://www.oneskyapp.com] is a new service that lets developers reac
4
4
 
5
5
  This gem integrates OneSky and I18n. At its most basic, it allows you to take the phrases defined under I18n's default locale and upload them to OneSky[link:http://www.oneskyapp.com] for translation by the community. Afterwards, you can download available translations and save them as Simple backend YAML files.
6
6
 
7
+ added Database backend storage support since version 0.0.3
8
+
9
+
7
10
  = Using inside a Rails project
8
11
 
9
12
  In your Rails root, edit your Gemfile to include this gem:
@@ -43,6 +46,27 @@ Install it like any regular gem:
43
46
 
44
47
  And see I18n::OneSky::SimpleClient for the methods available in Ruby.
45
48
 
49
+ = Using Database as translation storage, YAML as fallback (since version 0.0.3, Rails only)
50
+
51
+ assuming that you have either run one_sky:init or configured "ONESKY_API_KEY", "ONESKY_API_SECRET" and "ONESKY_PROJECT" environment variables. The following command generates rails initializers and run db migration:
52
+
53
+ rails generate one_sky:init_active_record_backend
54
+
55
+ use the upload_phrases rake command to upload your default locale to one sky server:
56
+
57
+ rake one_sky:upload_phrases
58
+
59
+ Then, instead of running one_sky:download_translations, run the following command to download and store the translations into translations database table:
60
+
61
+ rake one_sky:update_active_record_translations
62
+
63
+ = Using i18n-one_sky gem in Heroku
64
+
65
+ Because Heroku is read-only file system, you have to use database storage as storage backend. Either configure the "ONESKY_API_KEY", "ONESKY_API_SECRET" and "ONESKY_PROJECT" environment variables manually or use the OneSky Heroku addon.
66
+
67
+ Read "Using Database as translation storage" sections for details to upload and download phrases.
68
+
69
+
46
70
  = History
47
71
 
48
72
  See CHANGELOG[link:CHANGELOG.html]
data/i18n-one_sky.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.add_dependency "i18n", "~> 0.5.0"
18
18
  s.add_dependency "one_sky", "~> 0.0.2"
19
+ s.add_dependency 'i18n-active_record', "~> 0.0.2"
19
20
  s.add_dependency "thor", "~> 0.14.4"
20
21
 
21
22
  s.add_development_dependency "rspec", "~> 2.2.0"
@@ -0,0 +1,37 @@
1
+ require 'i18n-one_sky'
2
+
3
+ module OneSky
4
+ module Generators
5
+ class InitActiveRecordBackendGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ desc "This generator generates Rails files(initializer and database migration) to use Onesky service"
9
+
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def install_onesky_active_record
13
+ generate_initializers
14
+ generate_db_migration
15
+ end
16
+
17
+ def self.next_migration_number(dirname)
18
+ if ActiveRecord::Base.timestamped_migrations
19
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
20
+ else
21
+ "%.3d" % (current_migration_number(dirname) + 1)
22
+ end
23
+ end
24
+
25
+ protected
26
+
27
+ def generate_initializers
28
+ copy_file "onesky.rb", "config/initializers/onesky.rb"
29
+ end
30
+
31
+ def generate_db_migration
32
+ migration_template 'migration.rb', 'db/migrate/create_translations_table.rb'
33
+ rake("db:migrate")
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ class CreateTranslationsTable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :translations do |t|
4
+ t.string :locale
5
+ t.string :key
6
+ t.text :value
7
+ t.text :interpolations
8
+ t.boolean :is_proc, :default => false
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :translations
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ YAML_BACKEND = I18n.backend
2
+ ACTIVE_RECORD_BACKEND = I18n::OneSky::Translator::Backend.new
3
+
4
+ I18n.backend = I18n::Backend::Chain.new(ACTIVE_RECORD_BACKEND, YAML_BACKEND)
@@ -8,10 +8,17 @@ namespace :one_sky do
8
8
  puts "Phrases uploaded to OneSky. Please ask your translators to... well... get translating."
9
9
  end
10
10
 
11
- desc "Download available translations from OneSky."
11
+ desc "Download available translations from OneSky and store as yml files."
12
12
  task :download_translations do
13
13
  client = I18n::OneSky::SimpleClient.new
14
14
  client.download_translations
15
15
  puts "Translations downloaded and saved to config/locales/*_one_sky.yml files."
16
16
  end
17
+
18
+ desc "Download available translations from OneSky and stores into Active Record database"
19
+ task :update_activerecord_translations do
20
+ client = I18n::OneSky::SimpleClient.new
21
+ client.download_translations(nil, true, true)
22
+ puts "Translations downloaded and saved to database."
23
+ end
17
24
  end
@@ -30,15 +30,17 @@ module I18n
30
30
  # This will load the phrases defined for I18n's default locale.
31
31
  # If not a Rails project, manually supply the path where the I18n yml or rb files for located.
32
32
  def load_phrases(path=nil)
33
+ backend = I18n.backend.is_a?(I18n::Backend::Chain) ? I18n.backend.backends.last : I18n.backend
34
+
33
35
  if defined? Rails
34
- I18n.backend.load_translations
36
+ backend.load_translations
35
37
  else
36
38
  raise ArgumentError, "Please supply the path where locales are located." unless path
37
39
  path = path.chop if path =~ /\/$/
38
- I18n.backend.load_translations(*Dir.glob("#{path}/**/*.{yml,rb}"))
40
+ backend.load_translations(*Dir.glob("#{path}/**/*.{yml,rb}"))
39
41
  end
40
-
41
- @phrases_nested = I18n.backend.instance_variable_get("@translations")[I18n.default_locale]
42
+
43
+ @phrases_nested = backend.instance_variable_get("@translations")[I18n.default_locale]
42
44
 
43
45
  # Flatten the nested hash.
44
46
  flat_keys = flatten_translations(I18n.default_locale, @phrases_nested, true, false)
@@ -65,13 +67,13 @@ module I18n
65
67
 
66
68
  # When your translators are done, call this method to download all available translations and save them as Simple backend *.yml files.
67
69
  # Outside of Rails, manually supply the path where downloaded files should be saved.
68
- def download_translations(path=nil, download_base_locale=false)
69
- if defined? Rails
70
+ def download_translations(path=nil, download_base_locale=false, use_active_record=false)
71
+ if defined?(Rails)
70
72
  path ||= [Rails.root.to_s, "config", "locales"].join("/")
71
73
  else
72
74
  raise ArgumentError, "Please supply the path where locales are to be downloaded." unless path
73
75
  path = path.chop if path =~ /\/$/
74
- end if
76
+ end
75
77
 
76
78
  output = @project.output
77
79
 
@@ -85,39 +87,63 @@ module I18n
85
87
  end
86
88
  end
87
89
 
88
- # Delete all existing one_sky translation files before downloading a new set.
89
- File.delete(*Dir.glob("#{path}/*_one_sky.yml"))
90
+ if use_active_record
91
+ @translations.map { |k,v| save_locale(k, v) unless (k.to_s == @one_sky_locale and !download_base_locale)}
92
+ else
93
+ # Delete all existing one_sky translation files before downloading a new set.
94
+ File.delete(*Dir.glob("#{path}/*_one_sky.yml"))
90
95
 
91
- # Process each locale and save to file
92
- @translations.map { |k,v| save_locale(k, v, "#{path}/#{k}_one_sky.yml") unless (k.to_s == @one_sky_locale and !download_base_locale)}
96
+ # Process each locale and save to file
97
+ @translations.map { |k,v| save_locale(k, v, :filename => "#{path}/#{k}_one_sky.yml") unless (k.to_s == @one_sky_locale and !download_base_locale)}
98
+ end
93
99
  end
94
100
 
95
101
  protected
96
102
 
97
- def save_locale(lang_code, phrases, filename)
103
+ def save_locale(lang_code, phrases, options={})
104
+ nested = to_nested_nodes(phrases)
105
+
106
+ lang = @one_sky_languages.find { |e| e["locale"] == lang_code }
107
+
108
+ if options[:filename]
109
+ filename = options[:filename]
110
+ File.open(filename, 'w') do |f|
111
+ f.puts "# PLEASE DO NOT EDIT THIS FILE."
112
+ f.puts "# This was downloaded from OneSky. Log in to your OneSky account to manage translations on their website."
113
+ f.puts "# Language code: #{lang['locale']}"
114
+ f.puts "# Language name: #{lang['local_name']}"
115
+ f.puts "# Language English name: #{lang['eng_name']}"
116
+ f.print(nested.to_yaml)
117
+ end
118
+ filename
119
+ else
120
+
121
+ lang = @one_sky_languages.find { |e| e["locale"] == lang_code }
122
+
123
+ nested.each do |k, values|
124
+ values.each do |value|
125
+ I18n.backend.store_translations(k, value[0] => value[1])
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ def to_nested_nodes(phrases)
98
132
  nested = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) }
99
- phrases.map do |k,v|
133
+ phrases.each do |k,v|
100
134
  node = nested
101
135
  parts = k.split('.')
102
136
  parts.each_with_index { |segment,i| node[segment]; i == parts.size - 1 ? node[segment] = v : node = node[segment] }
103
137
  end
104
-
105
- lang = @one_sky_languages.find { |e| e["locale"] == lang_code }
106
-
107
- File.open(filename, 'w') do |f|
108
- f.puts "# PLEASE DO NOT EDIT THIS FILE."
109
- f.puts "# This was downloaded from OneSky. Log in to your OneSky account to manage translations on their website."
110
- f.puts "# Language code: #{lang['locale']}"
111
- f.puts "# Language name: #{lang['local_name']}"
112
- f.puts "# Language English name: #{lang['eng_name']}"
113
- f.print(nested.to_yaml)
114
- end
115
- filename
138
+
139
+ nested
116
140
  end
117
141
 
118
142
  def default_options
119
- if defined? Rails
120
- YAML.load_file([Rails.root.to_s, 'config', 'one_sky.yml'].join('/')).symbolize_keys
143
+ config_file = [Rails.root.to_s, 'config', 'one_sky.yml'].join('/')
144
+
145
+ if defined?(Rails) && File.exists?(config_file)
146
+ YAML.load_file(config_file).symbolize_keys
121
147
  else
122
148
  {:api_key => ENV["ONESKY_API_KEY"], :api_secret => ENV["ONESKY_API_SECRET"], :project => ENV["ONESKY_PROJECT"]}
123
149
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Onesky
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/i18n-one_sky.rb CHANGED
@@ -1,10 +1,19 @@
1
1
  require 'i18n'
2
2
  require 'one_sky'
3
3
  require 'i18n-one_sky/rails/railtie.rb' if defined? Rails
4
+ require 'i18n/backend/active_record' if defined? ActiveRecord
4
5
 
5
6
  module I18n
6
7
  module OneSky
7
8
  autoload :SimpleClient, 'i18n-one_sky/simple_client'
8
9
  class DefaultLocaleMismatchError < StandardError; end
10
+
11
+ if defined?(ActiveRecord)
12
+ module Translator
13
+ class Backend < I18n::Backend::ActiveRecord
14
+ include I18n::Backend::Memoize
15
+ end
16
+ end
17
+ end
9
18
  end
10
19
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-one_sky
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 25
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
10
11
  platform: ruby
11
12
  authors:
12
13
  - Junjun Olympia
@@ -14,8 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-02-27 00:00:00 +08:00
18
- default_executable:
18
+ date: 2011-09-11 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: i18n
@@ -25,6 +25,7 @@ dependencies:
25
25
  requirements:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
+ hash: 11
28
29
  segments:
29
30
  - 0
30
31
  - 5
@@ -40,6 +41,7 @@ dependencies:
40
41
  requirements:
41
42
  - - ~>
42
43
  - !ruby/object:Gem::Version
44
+ hash: 27
43
45
  segments:
44
46
  - 0
45
47
  - 0
@@ -48,50 +50,69 @@ dependencies:
48
50
  type: :runtime
49
51
  version_requirements: *id002
50
52
  - !ruby/object:Gem::Dependency
51
- name: thor
53
+ name: i18n-active_record
52
54
  prerelease: false
53
55
  requirement: &id003 !ruby/object:Gem::Requirement
54
56
  none: false
55
57
  requirements:
56
58
  - - ~>
57
59
  - !ruby/object:Gem::Version
60
+ hash: 27
61
+ segments:
62
+ - 0
63
+ - 0
64
+ - 2
65
+ version: 0.0.2
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: thor
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 47
58
77
  segments:
59
78
  - 0
60
79
  - 14
61
80
  - 4
62
81
  version: 0.14.4
63
82
  type: :runtime
64
- version_requirements: *id003
83
+ version_requirements: *id004
65
84
  - !ruby/object:Gem::Dependency
66
85
  name: rspec
67
86
  prerelease: false
68
- requirement: &id004 !ruby/object:Gem::Requirement
87
+ requirement: &id005 !ruby/object:Gem::Requirement
69
88
  none: false
70
89
  requirements:
71
90
  - - ~>
72
91
  - !ruby/object:Gem::Version
92
+ hash: 7
73
93
  segments:
74
94
  - 2
75
95
  - 2
76
96
  - 0
77
97
  version: 2.2.0
78
98
  type: :development
79
- version_requirements: *id004
99
+ version_requirements: *id005
80
100
  - !ruby/object:Gem::Dependency
81
101
  name: bundler
82
102
  prerelease: false
83
- requirement: &id005 !ruby/object:Gem::Requirement
103
+ requirement: &id006 !ruby/object:Gem::Requirement
84
104
  none: false
85
105
  requirements:
86
106
  - - ~>
87
107
  - !ruby/object:Gem::Version
108
+ hash: 23
88
109
  segments:
89
110
  - 1
90
111
  - 0
91
112
  - 0
92
113
  version: 1.0.0
93
114
  type: :development
94
- version_requirements: *id005
115
+ version_requirements: *id006
95
116
  description: A set of I18n extensions that use OneSky. At its most basic, this allows you to easily submit translation requests to the OneSky service and download available translations as Simple backend YAML files.
96
117
  email:
97
118
  - romeo.olympia@gmail.com
@@ -112,6 +133,9 @@ files:
112
133
  - Rakefile
113
134
  - i18n-one_sky.gemspec
114
135
  - lib/generators/one_sky/init/init_generator.rb
136
+ - lib/generators/one_sky/init_active_record_backend/init_active_record_backend_generator.rb
137
+ - lib/generators/one_sky/init_active_record_backend/templates/migration.rb
138
+ - lib/generators/one_sky/init_active_record_backend/templates/onesky.rb
115
139
  - lib/i18n-one_sky.rb
116
140
  - lib/i18n-one_sky/rails/railtie.rb
117
141
  - lib/i18n-one_sky/rails/tasks/i18n-one_sky.rake
@@ -120,7 +144,6 @@ files:
120
144
  - spec/data/en.yml
121
145
  - spec/helpers.rb
122
146
  - spec/simple_client_spec.rb
123
- has_rdoc: true
124
147
  homepage: http://rubygems.org/gems/i18n-one_sky
125
148
  licenses: []
126
149
 
@@ -134,6 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
157
  requirements:
135
158
  - - ">="
136
159
  - !ruby/object:Gem::Version
160
+ hash: 3
137
161
  segments:
138
162
  - 0
139
163
  version: "0"
@@ -142,13 +166,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
166
  requirements:
143
167
  - - ">="
144
168
  - !ruby/object:Gem::Version
169
+ hash: 3
145
170
  segments:
146
171
  - 0
147
172
  version: "0"
148
173
  requirements: []
149
174
 
150
175
  rubyforge_project: i18n-one_sky
151
- rubygems_version: 1.3.7
176
+ rubygems_version: 1.8.10
152
177
  signing_key:
153
178
  specification_version: 3
154
179
  summary: I18n extensions using OneSky -- the community-powered translation service.