interpret 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in interpret.gemspec
4
4
  gemspec
5
+
6
+ group :development, :test do
7
+ gem 'sqlite3-ruby', :require => 'sqlite3'
8
+ end
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -16,7 +16,7 @@ module Interpret
16
16
  # Generates a hash representing the tree structure of the translations
17
17
  # for the given locale. It includes only "folders" in the sense of
18
18
  # locale keys that includes some real translations, or other keys.
19
- def get_tree(lang = I18n.locale)
19
+ def get_tree(lang = I18n.default_locale)
20
20
  t = arel_table
21
21
  all_trans = locale(lang).select(t[:key]).where(t[:key].matches("%.%")).all
22
22
 
@@ -40,6 +40,9 @@ module Interpret
40
40
  translations.each do |e|
41
41
  LazyHash.lazy_add(res, "#{e.locale}.#{e.key}", e.value)
42
42
  end
43
+ if res.keys.size != 1
44
+ raise IndexError, "Generated hash must have only one root key. Your translation data in datrabase may be corrupted."
45
+ end
43
46
  res
44
47
  end
45
48
 
@@ -152,24 +155,24 @@ module Interpret
152
155
  files = Dir[Rails.root.join("config", "locales", "*.yml").to_s]
153
156
 
154
157
  @languages = {}
155
- @main = {}
156
158
  files.each do |f|
157
159
  ar = YAML.load_file f
158
160
  lang = ar.keys.first
159
- @languages[lang.to_s] = ar.first[1]
160
- if I18n.default_locale.to_s == lang.to_s
161
- @main = ar.first[1]
162
- @main_lang = lang
161
+ if @languages.has_key?(lang.to_s)
162
+ @languages[lang.to_s] = @languages[lang.to_s].deep_merge(ar.first[1])
163
+ else
164
+ @languages[lang.to_s] = ar.first[1]
163
165
  end
164
166
  end
165
167
 
168
+ @main = @languages[I18n.default_locale.to_s].clone
166
169
  sync(@main)
167
170
  end
168
171
 
169
172
  private
170
173
  def sync(hash, prefix = "", existing = nil)
171
174
  if existing.nil?
172
- translations = locale(@main_lang).all
175
+ translations = locale(I18n.default_locale).all
173
176
  existing = export(translations)
174
177
  existing = existing.first[1] unless existing.empty?
175
178
  end
@@ -180,7 +183,7 @@ module Interpret
180
183
  if hash[x].kind_of?(Hash)
181
184
  sync(hash[x], "#{prefix}#{x}.", existing[x])
182
185
  else
183
- old = locale(@main_lang).find_by_key("#{prefix}#{x}")
186
+ old = locale(I18n.default_locale).find_by_key("#{prefix}#{x}")
184
187
 
185
188
  unless old
186
189
  # Creates the new entry
@@ -200,29 +203,34 @@ module Interpret
200
203
  # Check if the the given key exists in all languages except @main. If
201
204
  # not, create an entry.
202
205
  def check_in_other_langs(key)
203
- (@languages.keys - [@main_lang]).each do |lang|
206
+ (@languages.keys - [I18n.default_locale]).each do |lang|
204
207
  trans = locale(lang).find_by_key(key)
205
208
  if trans.nil?
206
209
  create! :locale => lang,
207
210
  :key => key,
208
- :value => ""
211
+ :value => "untranslated"
209
212
  Interpret.logger.info "Created inexistent key [#{key}] for lang [#{lang}]"
210
213
  end
211
214
  end
212
215
  end
213
216
 
214
- def create_new_translation(key)
217
+ def create_new_translation(missing_key)
215
218
  @languages.keys.each do |lang|
216
219
  origin_hash = @languages[lang].clone
217
- key.split(".")[0..-2].each do |key|
220
+ value = nil
221
+ missing_key.split(".")[0..-2].each do |key|
222
+ if origin_hash[key].nil?
223
+ value = "untranslated"
224
+ break
225
+ end
218
226
  origin_hash = origin_hash[key]
219
227
  end
220
- value = origin_hash[key.split(".").last]
228
+ value ||= origin_hash[missing_key.split(".").last]
221
229
  create! :locale => lang,
222
- :key => key,
230
+ :key => missing_key,
223
231
  :value => value
224
232
  end
225
- Interpret.logger.info "New key created [#{key}] for languages #{@languages.keys.inspect}"
233
+ Interpret.logger.info "New key created [#{missing_key}] for languages #{@languages.keys.inspect}"
226
234
  end
227
235
 
228
236
  def remove_unused_keys(hash, prefix = "")
data/interpret.gemspec CHANGED
@@ -26,5 +26,5 @@ Gem::Specification.new do |s|
26
26
  s.add_dependency "will_paginate", ">= 3.0.pre2"
27
27
  s.add_dependency "best_in_place", ">= 0.1.7"
28
28
 
29
- s.add_development_dependency "rspec-rails", ">= 2.5"
29
+ s.add_development_dependency "rspec-rails", "~> 2.5"
30
30
  end
@@ -13,9 +13,7 @@ module Interpret
13
13
  end
14
14
 
15
15
  def copy_images
16
- copy_file "stylesheets/plus_icon.gif", "public/stylesheets/plus_icon.gif"
17
- copy_file "stylesheets/minus_icon.gif", "public/stylesheets/minus_icon.gif"
18
-
16
+ copy_file "stylesheets/folder.png", "public/stylesheets/folder.png"
19
17
  end
20
18
  end
21
19
  end
@@ -1,15 +1,18 @@
1
- namespace :interpret do
2
- desc "Update translations keys in database"
3
- task :update, :roles => :app, :except => { :no_release => true } do
4
- commands = [
5
- "RAILS_ENV=#{rails_env} rake interpret:update",
6
- ]
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :interpret do
3
+ def rails_env
4
+ fetch(:rails_env, false) ? "RAILS_ENV=#{fetch(:rails_env)}" : ''
5
+ end
7
6
 
8
- run <<-CMD
9
- cd #{release_path} &&
10
- #{commands.join(" && ")}
11
- CMD
7
+ def roles
8
+ fetch(:delayed_job_server_role, :app)
9
+ end
10
+
11
+ desc "Update translations keys in database"
12
+ task :update, :roles => lambda {roles} do
13
+ run "cd #{current_path};#{rails_env} rake interpret:update"
14
+ end
12
15
  end
13
- end
14
16
 
15
- after "deploy:update_code", "interpret:update"
17
+ after "deploy:update_code", "interpret:update"
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Interpret
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
Binary file
@@ -353,25 +353,26 @@ html body * dd.clear
353
353
  }
354
354
 
355
355
  body {
356
- font-family: Helvetica;
356
+ font-family: "Lucida Grande", Helvetica;
357
357
  }
358
358
 
359
359
  #container {
360
360
  border: .3em solid #333;
361
361
  }
362
362
 
363
- #header {
363
+ #header, .header {
364
364
  padding: 0 1em;
365
365
  margin-top: 1em;
366
366
  color: #FFF;
367
367
  background-color: #666;
368
+ border-radius: 5px;
368
369
  }
369
370
 
370
371
  table {
371
372
  border-collapse:collapse;
372
373
  width: 100%;
373
- border-right: 1px solid #444;
374
- border-left: 1px solid #444;
374
+ border-right: 1px solid #CCC;
375
+ border-left: 1px solid #CCC;
375
376
  }
376
377
 
377
378
  td, th {
@@ -388,7 +389,7 @@ td:last-child, td:last-child {
388
389
  }
389
390
 
390
391
  tr {
391
- border-bottom:1px solid gray;
392
+ border-bottom:1px solid #CCC;
392
393
  cursor: hand;
393
394
  cursor: pointer;
394
395
  }
@@ -396,7 +397,6 @@ tr {
396
397
 
397
398
  tr:hover {
398
399
  background-color: #DDD;
399
- height: 6.5em;
400
400
  -moz-transition: background .4s linear;
401
401
  -o-transition: background .4s linear;
402
402
  -webkit-transition: background .4s linear;
@@ -415,7 +415,7 @@ tr.header:hover {
415
415
  -webkit-transition: none;
416
416
  height: 2em;
417
417
  }
418
- tr.header th {
418
+ tr.header th {;
419
419
  color: #EEE;
420
420
  }
421
421
 
@@ -506,7 +506,6 @@ tbody td a {
506
506
 
507
507
  .best_in_place textarea {
508
508
  width: 100%;
509
- height: 6em;
510
509
  border: transparent;
511
510
  font-size: .9em;
512
511
  }
@@ -516,17 +515,10 @@ tbody td a {
516
515
  padding: .4em;
517
516
  }
518
517
  #tree_sidebar li {
519
- list-style-image: url("/stylesheets/plus_icon.gif");
518
+ list-style-image: url("/stylesheets/folder.png");
520
519
  font-weight: normal;
521
- }
522
-
523
-
524
- #tree_sidebar li.current {
525
- list-style-image: url("/stylesheets/minus_icon.gif");
526
- }
527
-
528
- #tree_sidebar li.current {
529
- font-weight: bold;
520
+ padding-bottom: 5px;
521
+ letter-spacing: 1px;
530
522
  }
531
523
 
532
524
  #tree_sidebar a {
@@ -536,7 +528,3 @@ tbody td a {
536
528
  #tree_sidebar a:hover {
537
529
  text-decoration: underline;
538
530
  }
539
-
540
- #loading_sidebar {
541
- display: none;
542
- }
@@ -0,0 +1,15 @@
1
+ module DatabaseHelpers
2
+
3
+ def migrate_database
4
+ silence_stream(STDOUT) do
5
+ ActiveRecord::Migrator.migrate File.expand_path('../../test_app/db/migrate/', __FILE__)
6
+ end
7
+ end
8
+
9
+ def drop_all_tables
10
+ ActiveRecord::Base.connection.tables.each do |table|
11
+ ActiveRecord::Base.connection.drop_table(table)
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Interpret::Translation do
4
+
5
+ let(:en_yml) {"""
6
+ en:
7
+ p1: Hello world!
8
+ folder1:
9
+ pr1: Hi
10
+ folder2:
11
+ pr1: Some other text here
12
+ folder3:
13
+ pr1: More phrases
14
+ sub:
15
+ name: This is a 2 level subfolder
16
+ subsub:
17
+ name: With another nested folder inside
18
+ other:
19
+ name: folder
20
+ """
21
+ }
22
+
23
+ def file2db(string_file)
24
+ hash = YAML.load string_file
25
+
26
+ lang = hash.keys.first
27
+ records = Interpret::Translation.send(:parse_hash, hash.first[1], lang)
28
+ Interpret::Translation.transaction do
29
+ records.each {|x| x.save!}
30
+ end
31
+ end
32
+
33
+ describe ".get_tree" do
34
+ it "should return a hash representing a tree folder structure of the i18n keys" do
35
+ file2db(en_yml)
36
+ Interpret::Translation.get_tree('en').should == {'index' => {
37
+ 'folder1' => {},
38
+ 'folder2' => {},
39
+ 'folder3' => {
40
+ 'sub' => {
41
+ 'subsub' => {}
42
+ },
43
+ 'other' => {}
44
+ }
45
+ }}
46
+ end
47
+ end
48
+
49
+ describe ".export" do
50
+ it "should return a hash representing the yml locale file for the given translations" do
51
+ file2db(en_yml)
52
+ translations = Interpret::Translation.all
53
+ Interpret::Translation.export(translations).should == YAML.load(en_yml)
54
+ end
55
+ end
56
+
57
+ describe ".import" do
58
+ pending
59
+ end
60
+
61
+ describe ".dump" do
62
+ pending
63
+ end
64
+
65
+ describe ".update" do
66
+ pending
67
+ end
68
+ end
@@ -0,0 +1,35 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path('../../test_app/config/environment', __FILE__)
5
+ require "rails/test_help"
6
+ require "rspec/rails"
7
+ require "database_helpers"
8
+
9
+ ActionMailer::Base.delivery_method = :test
10
+ ActionMailer::Base.perform_deliveries = true
11
+ ActionMailer::Base.default_url_options[:host] = "test.com"
12
+
13
+ Rails.backtrace_cleaner.remove_silencers!
14
+
15
+ include DatabaseHelpers
16
+ # Run any available migration
17
+ puts 'Setting up database...'
18
+ drop_all_tables
19
+ migrate_database
20
+
21
+ # Load support files
22
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each{|f| require f}
23
+
24
+
25
+ RSpec.configure do |config|
26
+ # Remove this line if you don't want RSpec's should and should_not
27
+ # methods or matchers
28
+ require 'rspec/expectations'
29
+
30
+ config.include RSpec::Matchers
31
+ config.include DatabaseHelpers
32
+
33
+ # == Mock Framework
34
+ config.mock_with :rspec
35
+ end
data/test_app/Capfile ADDED
@@ -0,0 +1,4 @@
1
+ load 'deploy' if respond_to?(:namespace) # cap2 differentiator
2
+ Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
3
+
4
+ load 'config/deploy' # remove this line to skip loading any of the default tasks
data/test_app/Gemfile CHANGED
@@ -1,11 +1,10 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
- gem 'rails', '~> 3.0.0'
3
+ gem 'rails', '~> 3.0.3'
4
4
  gem 'interpret', :path => ".."
5
5
  gem 'haml-rails'
6
+ gem 'capistrano'
6
7
 
7
8
  group :development, :test do
8
9
  gem 'sqlite3-ruby', :require => 'sqlite3'
9
10
  end
10
-
11
-
@@ -0,0 +1,24 @@
1
+ require '../lib/interpret/capistrano'
2
+
3
+ set :application, "set your application name here"
4
+ set :repository, "set your repository location here"
5
+
6
+ set :scm, :subversion
7
+ # Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
8
+
9
+ role :web, "your web-server here" # Your HTTP server, Apache/etc
10
+ role :app, "your app-server here" # This may be the same as your `Web` server
11
+ role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
12
+ role :db, "your slave db-server here"
13
+
14
+ # If you are using Passenger mod_rails uncomment this:
15
+ # if you're still using the script/reapear helper you will need
16
+ # these http://github.com/rails/irs_process_scripts
17
+
18
+ # namespace :deploy do
19
+ # task :start do ; end
20
+ # task :stop do ; end
21
+ # task :restart, :roles => :app, :except => { :no_release => true } do
22
+ # run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
23
+ # end
24
+ # end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interpret
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Roger Campos
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-02-14 00:00:00 +01:00
17
+ date: 2011-02-15 00:00:00 +01:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 1
30
28
  segments:
31
29
  - 3
32
30
  - 0
@@ -42,7 +40,6 @@ dependencies:
42
40
  requirements:
43
41
  - - ~>
44
42
  - !ruby/object:Gem::Version
45
- hash: 11
46
43
  segments:
47
44
  - 0
48
45
  - 5
@@ -58,7 +55,6 @@ dependencies:
58
55
  requirements:
59
56
  - - ">="
60
57
  - !ruby/object:Gem::Version
61
- hash: 3
62
58
  segments:
63
59
  - 0
64
60
  version: "0"
@@ -72,7 +68,6 @@ dependencies:
72
68
  requirements:
73
69
  - - ">="
74
70
  - !ruby/object:Gem::Version
75
- hash: 103
76
71
  segments:
77
72
  - 0
78
73
  - 30
@@ -88,7 +83,6 @@ dependencies:
88
83
  requirements:
89
84
  - - ">="
90
85
  - !ruby/object:Gem::Version
91
- hash: -1876988247
92
86
  segments:
93
87
  - 3
94
88
  - 0
@@ -104,7 +98,6 @@ dependencies:
104
98
  requirements:
105
99
  - - ">="
106
100
  - !ruby/object:Gem::Version
107
- hash: 21
108
101
  segments:
109
102
  - 0
110
103
  - 1
@@ -118,9 +111,8 @@ dependencies:
118
111
  requirement: &id007 !ruby/object:Gem::Requirement
119
112
  none: false
120
113
  requirements:
121
- - - ">="
114
+ - - ~>
122
115
  - !ruby/object:Gem::Version
123
- hash: 9
124
116
  segments:
125
117
  - 2
126
118
  - 5
@@ -172,6 +164,10 @@ files:
172
164
  - public/javascripts/jquery.purr.js
173
165
  - public/stylesheets/folder.png
174
166
  - public/stylesheets/interpret_style.css
167
+ - spec/database_helpers.rb
168
+ - spec/models/translation_spec.rb
169
+ - spec/spec_helper.rb
170
+ - test_app/Capfile
175
171
  - test_app/Gemfile
176
172
  - test_app/README
177
173
  - test_app/Rakefile
@@ -186,6 +182,7 @@ files:
186
182
  - test_app/config/application.rb
187
183
  - test_app/config/boot.rb
188
184
  - test_app/config/database.yml
185
+ - test_app/config/deploy.rb
189
186
  - test_app/config/environment.rb
190
187
  - test_app/config/environments/development.rb
191
188
  - test_app/config/environments/production.rb
@@ -238,7 +235,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
238
235
  requirements:
239
236
  - - ">="
240
237
  - !ruby/object:Gem::Version
241
- hash: 3
242
238
  segments:
243
239
  - 0
244
240
  version: "0"
@@ -247,7 +243,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
243
  requirements:
248
244
  - - ">="
249
245
  - !ruby/object:Gem::Version
250
- hash: 3
251
246
  segments:
252
247
  - 0
253
248
  version: "0"