Skip to content

Commit

Permalink
Use Spree:I18nUtils from spree_core gem to assist in rake tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
schof committed Oct 31, 2010
1 parent f9d132a commit 6e50639
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 41 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'http://rubygems.org'

gem "spree_core", :git => 'git://github.com/railsdog/spree.git' #:path => '../spree/core'

115 changes: 115 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
GIT
remote: git://github.com/railsdog/spree.git
revision: 2be3805b4ea18f7cb06c5850c6ce0885c0b60062
specs:
spree_core (0.30.0.beta2)
activemerchant (>= 1.7.1)
acts_as_list (>= 0.1.2)
faker (>= 0.3.1)
highline (>= 1.5.1)
jquery-rails (>= 0.2.2)
paperclip (>= 2.3.1.1)
rails (>= 3.0.1)
rd_awesome_nested_set (>= 1.4.4)
rd_resource_controller
rd_searchlogic (>= 3.0.0.rc3)
rd_unobtrusive_date_picker (>= 0.1.0)
state_machine (>= 0.9.4)
stringex (>= 1.0.3)
will_paginate (>= 3.0.pre)

GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.1)
actionpack (= 3.0.1)
mail (~> 2.2.5)
actionpack (3.0.1)
activemodel (= 3.0.1)
activesupport (= 3.0.1)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
activemerchant (1.9.0)
activesupport (>= 2.3.2)
braintree (>= 2.0.0)
builder (>= 2.0.0)
activemodel (3.0.1)
activesupport (= 3.0.1)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.1)
activemodel (= 3.0.1)
activesupport (= 3.0.1)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
activeresource (3.0.1)
activemodel (= 3.0.1)
activesupport (= 3.0.1)
activesupport (3.0.1)
acts_as_list (0.1.2)
arel (1.0.1)
activesupport (~> 3.0.0)
braintree (2.6.1)
builder
builder (2.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
faker (0.3.1)
highline (1.6.1)
i18n (0.4.2)
jquery-rails (0.2.4)
rails (~> 3.0)
mail (2.2.9)
activesupport (>= 2.3.6)
i18n (~> 0.4.1)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
paperclip (2.3.5)
activerecord
activesupport
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.1)
actionmailer (= 3.0.1)
actionpack (= 3.0.1)
activerecord (= 3.0.1)
activeresource (= 3.0.1)
activesupport (= 3.0.1)
bundler (~> 1.0.0)
railties (= 3.0.1)
railties (3.0.1)
actionpack (= 3.0.1)
activesupport (= 3.0.1)
rake (>= 0.8.4)
thor (~> 0.14.0)
rake (0.8.7)
rd_awesome_nested_set (1.4.4)
activerecord (>= 1.1)
rd_resource_controller (1.0.0.rc)
rd_searchlogic (3.0.0.rc4)
activerecord (>= 3.0.0)
rd_unobtrusive_date_picker (0.1.0)
state_machine (0.9.4)
stringex (1.2.0)
thor (0.14.3)
treetop (1.4.8)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
will_paginate (3.0.pre2)

PLATFORMS
ruby

DEPENDENCIES
spree_core!
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require "rubygems"
require "bundler/setup"

require 'rake'
require 'rails'
#require 'spree_core'

# Load any custom rakefiles for extension
Dir[File.dirname(__FILE__) + '/lib/tasks/*.rake'].sort.each { |f| load f }
49 changes: 8 additions & 41 deletions lib/tasks/i18n.rake
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
require 'spree/i18n_utils'

include Spree::I18nUtils

namespace :spree do
namespace :i18n do

language_root = File.dirname(__FILE__) + "/../generators/templates/config/locales"
default_dir = File.dirname(__FILE__) + "/../../default"

desc "Update by retrieving the latest Spree locale fils"
task :update_default do

puts "Fetching latest Spree locale file to #{language_root}"
#TODO also pull the auth and dash locales once they exist
exec %(
Expand Down Expand Up @@ -35,7 +41,8 @@ namespace :spree do
end

write_file "#{language_root}/#{ENV['LOCALE']}.yml", "#{ENV['LOCALE']}", '---', composite_keys
print "Also, download the rails translation from: http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale\n"
print "New locale generated.\n"
print "Don't forget to also download the rails translation from: http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale\n"
end

desc "Show translation status for all supported locales other than en."
Expand Down Expand Up @@ -72,46 +79,6 @@ def get_translation_keys(gem_name)
words
end

# #Retrieve comments, translation data in hash form
def read_file(filename, basename)
(comments, data) = IO.read(filename).split(/\n#{basename}:\s*\n/) #Add error checking for failed file read?
return comments, create_hash(data)
end

#Creates hash of translation data
def create_hash(data)
words = Hash.new
return words if !data
parent = Array.new
previous_key = 'base'
data.split("\n").each do |w|
next if w.strip.blank?
(key, value) = w.split(':', 2)
value ||= ''
shift = (key =~ /\w/)/2 - parent.size #Determine level of current key in comparison to parent array
key = key.sub(/^\s+/,'')
parent << previous_key if shift > 0 #If key is child of previous key, add previous key as parent
(shift*-1).times { parent.pop } if shift < 0 #If key is not related to previous key, remove parent keys
previous_key = key #Track key in case next key is child of this key
words[parent.join(':')+':'+key] = value
end
words
end

#Writes to file from translation data hash structure
def write_file(filename,basename,comments,words,comment_values=true, fallback_values={})
File.open(filename, "w") do |log|
log.puts(comments+"\n"+basename+": \n")
words.sort.each do |k,v|
keys = k.split(':')
(keys.size-1).times { keys[keys.size-1] = ' ' + keys[keys.size-1] } #Add indentation for children keys
value = v.strip
value = ("#" + value) if comment_values and not value.blank?
log.puts "#{keys[keys.size-1]}: #{value}\n"
end
end
end

# Returns a composite hash of all relevant translation keys from each of the gems
def composite_keys
api_keys = get_translation_keys "spree_api"
Expand Down

0 comments on commit 6e50639

Please sign in to comment.