Class: Vocab::Extractor::Rails

Inherits:
Base
  • Object
show all
Defined in:
lib/vocab/extractor/rails.rb

Constant Summary collapse

DIFF =
'en.yml'
FULL =
'en.full.yml'
DIFF_SUFFIX =
'diff.yml'
FULL_SUFFIX =
'full.yml'

Class Method Summary collapse

Methods inherited from Base

diff, extract, git_path, git_root, mkdir_examples, previous_file, update_settings

Class Method Details

.current_pluralsObject

Treat this as a no-op because plurals handled like normal strings



57
58
59
# File 'lib/vocab/extractor/rails.rb', line 57

def current_plurals
  return {}
end

.current_strings(locales_root = nil) ⇒ Object



46
47
48
49
# File 'lib/vocab/extractor/rails.rb', line 46

def current_strings( locales_root = nil )
  locales_root ||= "#{Vocab.root}/config/locales"
  return translations( locales_root )
end

.examplesObject



90
91
92
# File 'lib/vocab/extractor/rails.rb', line 90

def examples
  return [ "#{Vocab.root}/tmp/translations" ]
end

.extract_all(locales_root = nil, result_dir = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/vocab/extractor/rails.rb', line 61

def extract_all( locales_root = nil, result_dir = nil )
  locales_root ||= "#{Vocab.root}/config/locales"
  result_dir ||= Vocab.root
  
  translator = Vocab::Translator::Rails.new
  translator.load_dir( locales_root )

  translator.available_locales.each do |locale|
    strings = translations( locales_root, locale )
    path = "#{result_dir}/#{locale}.full.yml"
    write( strings, path, locale )
  end
end

.hasherize(diff, locale = :en) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/vocab/extractor/rails.rb', line 81

def hasherize( diff, locale = :en )
  translator = Vocab::Translator::Rails.new( locale )
  diff.each do |key, value|
    key = key.to_s.gsub!( /^#{locale.to_s}\./, '' )
    translator.store( key, value )
  end
  return translator.translations( :prefix => true )
end

.previous_pluralsObject

Treat this as a no-op because plurals handled like normal strings



52
53
54
# File 'lib/vocab/extractor/rails.rb', line 52

def previous_plurals
  return {}
end

.previous_strings(locales_root = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vocab/extractor/rails.rb', line 26

def previous_strings( locales_root = nil )
  locales_root ||= "config/locales"
  tmpdir = "#{Vocab.root}/tmp/last_translation"
  `rm -rf #{tmpdir}/*`

  sha = Vocab.settings.last_translation
  translation_files = `git ls-tree --name-only -r #{sha}:#{locales_root}`.split( "\n" )
  translation_files = translation_files.select { |f| f =~ /en.(yml|rb)$/ }
  translation_files.each do |path|
    tmpdir_path = "#{tmpdir}/#{path}"
    FileUtils.mkdir_p( File.dirname( tmpdir_path ) )
    File.open( tmpdir_path, "w+" ) do |f|
      yml = previous_file( "#{locales_root}/#{path}", sha )
      f.write( yml )
    end
  end

  return translations( tmpdir )
end


94
95
96
97
98
99
100
101
102
103
# File 'lib/vocab/extractor/rails.rb', line 94

def print_instructions( values = {} )
  values[ :diff ] = DIFF
  values[ :full ] = FULL
  values[ :tree ] = <<-EOS
tmp/translations/es.yml
tmp/translations/zh.yml
  EOS

  super( values )
end

.translations(dir, locale = :en) ⇒ Object



75
76
77
78
79
# File 'lib/vocab/extractor/rails.rb', line 75

def translations( dir, locale = :en )
  translator = Vocab::Translator::Rails.new( locale )
  translator.load_dir( dir )
  return translator.flattened_translations( :prefix => true )
end

.write(translations, path, locale = :en) ⇒ Object



20
21
22
23
24
# File 'lib/vocab/extractor/rails.rb', line 20

def write( translations, path, locale = :en )
  data = hasherize( translations, locale ).to_yaml
  File.open( path, "w+" ) { |f| f.write( data ) }
  Vocab.ui.say( "Extracted to #{path}" )
end

.write_diff(strings, plurals, path, locale = :en) ⇒ Object



10
11
12
13
# File 'lib/vocab/extractor/rails.rb', line 10

def write_diff( strings, plurals, path, locale = :en )
  path ||= "#{Vocab.root}/#{locale}.#{DIFF_SUFFIX}"
  write( strings, path )
end

.write_full(strings, plurals, path, locale = :en) ⇒ Object



15
16
17
18
# File 'lib/vocab/extractor/rails.rb', line 15

def write_full( strings, plurals, path, locale = :en )
  path ||= "#{Vocab.root}/#{locale}.#{FULL_SUFFIX}"
  write( strings, path )
end