Class: WebTranslateIt::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/web_translate_it/command_line.rb

Constant Summary collapse

OPTIONS =
<<-OPTION
pull            Pull target language file(s) from Web Translate It.
push            Push master language file(s) to Web Translate It.
autoconf        Configure your project to sync with Web Translate It.
stats           Fetch and display your project statistics.

OPTIONAL PARAMETERS:
--------------------
-l --locale     The ISO code of a specific locale to pull or push.
-c --config     Path to a translation.yml file. If this option
                is absent, looks for config/translation.yml.
--all           Respectively download or upload all files.
--force         Force wti pull to re-download the language file,
                regardless if local version is current.
OTHER:
------
-v --version    Show version.
-h --help       This page.
OPTION

Class Method Summary collapse

Class Method Details

.autoconfObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/web_translate_it/command_line.rb', line 68

def self.autoconf
  puts "We will attempt to configure your project automagically"
  puts "Please enter your project API Key:"
  api_key = STDIN.gets.strip
  if api_key == ""
    puts "You must enter your project API key provided by Web Translate It"
    exit
  end
  puts "Where should we create the configuration file? (Default: `config/translation.yml`)"
  path = STDIN.gets.strip
  path = "config/translation.yml" if path == ""
  FileUtils.mkpath(path.split('/')[0..path.split('/').size-1])
  puts "Where are you language files located? (Default: `config/locales/`)"
  path_to_locale_files = STDIN.gets.strip
  path_to_locale_files = "config/locales/" if path_to_locale_files == ""
  FileUtils.mkpath(path.split('/')[0..path.split('/').size-1])
  File.open(path, 'w'){ |file| file << generate_configuration(api_key, path_to_locale_files) }
  puts "Done! You can now use `wti` to push and pull your language files."
  puts "Check `wti --help` for more information."
end

.fetch_configurationObject



111
112
113
114
115
116
117
118
# File 'lib/web_translate_it/command_line.rb', line 111

def self.fetch_configuration
  if (index = ARGV.index('-c') || ARGV.index('--config')).nil?
    configuration = WebTranslateIt::Configuration.new('.')
  else
    configuration = WebTranslateIt::Configuration.new('.', ARGV[index+1])
  end
  return configuration
end

.fetch_locales_to_pull(configuration) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/web_translate_it/command_line.rb', line 120

def self.fetch_locales_to_pull(configuration)
  if (index = ARGV.index('-l') || ARGV.index('--locale')).nil?
    locales = configuration.target_locales
  else
    locales = [ARGV[index+1]]
  end
  locales.push(configuration.source_locale) if ARGV.index('--all')
  return locales.uniq
end

.fetch_locales_to_push(configuration) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/web_translate_it/command_line.rb', line 130

def self.fetch_locales_to_push(configuration)
  if (index = ARGV.index('-l') || ARGV.index('--locale')).nil?
    locales = [configuration.source_locale]
  else
    locales = [ARGV[index+1]]
  end
  locales.push(configuration.target_locales) if ARGV.index('--all')
  return locales.uniq
end

.generate_configuration(api_key, path_to_locale_files) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/web_translate_it/command_line.rb', line 140

def self.generate_configuration(api_key, path_to_locale_files)
  project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
  project = project_info['project']
  file = <<-FILE
api_key: #{api_key}

# The locales not to sync with Web Translate It.
# Pass an array of string, or an array of symbols, a string or a symbol.
# eg. [:en, :fr] or just 'en'
ignore_locales: '#{project["source_locale"]["code"]}'

# A list of files to translate
# You can name your language files as you want, as long as the locale name match the
# locale name you set in Web Translate It, and that the different language files names are
# differenciated by their locale name.
# For example, if you set to translate a project in en_US in WTI, you should use the locale en_US in your app
#
files:
FILE
  project["project_files"].each do |project_file|
    if project_file["master"]
      file << "  #{project_file["id"]}: #{path_to_locale_files}" + project_file["name"].gsub(project["source_locale"]["code"], "[locale]") + "\n"
    end
  end
  return file
end

.pullObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/web_translate_it/command_line.rb', line 46

def self.pull
  configuration = fetch_configuration
  locales = fetch_locales_to_pull(configuration)
  configuration.files.each do |file|
    locales.each do |locale|
      puts "Pulling #{file.file_path_for_locale(locale)}"
      file.fetch(locale, ARGV.index('--force'))
    end
  end
end

.pushObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/web_translate_it/command_line.rb', line 57

def self.push
  configuration = fetch_configuration
  locales = fetch_locales_to_push(configuration)
  configuration.files.each do |file|
    locales.each do |locale|
      puts "Pushing #{file.file_path_for_locale(locale)}"
      file.upload(locale)
    end
  end
end

.runObject



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

def self.run
  case ARGV[0]
  when 'pull'
    pull
  when 'push'
    push
  when 'autoconf'
    autoconf
  when 'stats'
    stats
  when '-v', '--version'
    show_version
  when '-h', '--help'
    show_options
  else
    puts "Command not found"
    show_options
  end
end

.show_optionsObject



99
100
101
102
103
104
# File 'lib/web_translate_it/command_line.rb', line 99

def self.show_options
  puts ""
  puts "Web Translate It Help:"
  puts "**********************"
  $stdout.puts OPTIONS
end

.show_versionObject



106
107
108
109
# File 'lib/web_translate_it/command_line.rb', line 106

def self.show_version
  puts ""
  puts "Web Translate It #{WebTranslateIt::Util.version}"
end

.statsObject



89
90
91
92
93
94
95
96
97
# File 'lib/web_translate_it/command_line.rb', line 89

def self.stats
  configuration = fetch_configuration
  stats = YAML.load(Project.fetch_stats(configuration.api_key))
  stats.each do |locale, values|
    percent_translated = Util.calculate_percentage(values['count_strings_to_proofread'] + values['count_strings_done'] + values['count_strings_to_verify'], values['count_strings'])
    percent_completed  = Util.calculate_percentage(values['count_strings_done'], values['count_strings'])
    puts "#{locale}: #{percent_translated}% translated, #{percent_completed}% completed #{values['stale'] ? "Stale" : ""}"
  end
end