Class: N42translation::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/n42translation/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(target, project_name, key, value, language = "en") ⇒ Object



97
98
99
100
# File 'lib/n42translation/cli.rb', line 97

def add(target, project_name, key, value, language = "en")
  # puts "#{target}, #{project_name}, #{key}, #{value}, #{language}"
  insert_string(target, project_name, key, value, language, :override)
end

#build(target, project_name, outputfile_path = nil, default_language = "en") ⇒ Object

Raises:

  • (Thor::Error)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/n42translation/cli.rb', line 55

def build(target, project_name, outputfile_path=nil, default_language="en")
  config = load_config_file(project_name)

  outputfile_path = config["build_path"] if outputfile_path.nil?
  raise Thor::Error, "no output path specified" if outputfile_path.nil?

  source_path = config["source_path"]
  raise Thor::Error, "no source path specified" if source_path.nil?

  path_name = config["target_build_path_names"][target.to_s].to_s
  target_build_path = File.join(outputfile_path, path_name)

  case target.to_sym
  when :all
    self.build(:android, project_name, outputfile_path)
    self.build(:ios, project_name, outputfile_path)
    self.build(:rails, project_name, outputfile_path)
    self.build(:csv, project_name, outputfile_path)
    self.build(:xlsx, project_name, outputfile_path)
  when :android
    raise Thor::Error, "no build path specified for your target: #{target}" if target_build_path.nil?
    build_platform(source_path, project_name, target_build_path, target, :xml)
  when :ios
    raise Thor::Error, "no build path specified for your target: #{target}" if target_build_path.nil?
    build_platform(source_path, project_name, target_build_path, target, :strings)
  when :rails
    raise Thor::Error, "no build path specified for your target: #{target}" if target_build_path.nil?
    build_platform(source_path, project_name, target_build_path, target, :yml)
  when :csv
    raise Thor::Error, "no build path specified for your target: #{target}" if target_build_path.nil?
    build_csv(source_path, project_name, target_build_path, [:all, :ios, :android, :rails], default_language)
  when :xlsx
    raise Thor::Error, "no build path specified for your target: #{target}" if target_build_path.nil?
    build_xlsx(source_path, project_name, target_build_path, [:all, :ios, :android, :rails], default_language)
  when "".to_sym
    # ignore the “” case
  else
    raise Thor::Error, "unknown target: #{target}"
  end
end

#init(project_name, target = nil, languages = nil) ⇒ Object

Raises:

  • (Thor::Error)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/n42translation/cli.rb', line 16

def init(project_name, target = nil, languages = nil)
  config = load_config_file(project_name)

  build_path = config["build_path"]
  source_path = config["source_path"]

  project_name = config["project_name"] if project_name.nil?

  targets = []
  targets = [target] if target != nil && target != ""
  targets = config["targets"]["all"].split(',').map(&:lstrip).map(&:rstrip) if target === "all"
  targets = config["targets"]["mobile"].split(',').map(&:lstrip).map(&:rstrip) if target === "mobile"
  targets << "" # we want a platform named "all", to build from 'platform' + 'all' files

  raise Thor::Error, "no build path found" if build_path.nil?
  raise Thor::Error, "no locale path found" if source_path.nil?
  raise Thor::Error, "no project name was specified" if project_name.nil?
  raise Thor::Error, "no targets specified" if targets.nil?

  # write config File
  puts config.to_yaml
  File.open("config.#{project_name}.yml", 'w'){|file| file.write config.to_yaml} unless File.exists?("config.#{project_name}.yml")

  languages = config["languages"] if languages.nil?
  langs = languages.split(',').map(&:lstrip).map(&:rstrip)
  langs = (langs + get_languages(project_name)).uniq

  targets.each do |_target|
    build(_target, project_name, build_path) unless _target == "all"

    langs.each do |lang|
      filename = File.join(source_path, "#{project_name}.#{lang}.yml") if _target ===""
      filename = File.join(source_path, "#{project_name}.#{lang}.#{_target}.yml") unless _target === ""
      File.open(filename, 'w') { |file| file.write("---") } unless File.exists?(filename)
    end
  end
end

#update(target, project_name, key, new_value, language = "en") ⇒ Object



103
104
105
# File 'lib/n42translation/cli.rb', line 103

def update(target, project_name, key, new_value, language="en")
  insert_string(target, project_name, key, new_value, language, :update)
end