translatomatic 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +19 -0
- data/.gitignore +0 -0
- data/.travis.yml +7 -7
- data/.yardopts +9 -0
- data/Gemfile +4 -4
- data/Guardfile +0 -0
- data/README.de.md +61 -16
- data/README.es.md +60 -15
- data/README.fr.md +61 -16
- data/README.it.md +60 -15
- data/README.ja.md +59 -14
- data/README.ko.md +137 -0
- data/README.md +58 -13
- data/README.ms.md +137 -0
- data/README.pt.md +137 -0
- data/README.ru.md +137 -0
- data/README.sv.md +137 -0
- data/README.zh.md +137 -0
- data/bin/setup +8 -8
- data/bin/translatomatic +6 -6
- data/bin/travis +1 -1
- data/config/locales/translatomatic/de.yml +104 -0
- data/config/locales/translatomatic/en.yml +101 -0
- data/config/locales/translatomatic/es.yml +105 -0
- data/config/locales/translatomatic/fr.yml +105 -0
- data/config/locales/translatomatic/it.yml +103 -0
- data/config/locales/translatomatic/ja.yml +102 -0
- data/config/locales/translatomatic/ko.yml +101 -0
- data/config/locales/translatomatic/ms.yml +103 -0
- data/config/locales/translatomatic/pt.yml +105 -0
- data/config/locales/translatomatic/ru.yml +103 -0
- data/config/locales/translatomatic/sv.yml +103 -0
- data/config/locales/translatomatic/zh.yml +102 -0
- data/db/migrate/201712170000_initial.rb +2 -1
- data/lib/translatomatic/cli/base.rb +73 -0
- data/lib/translatomatic/cli/common_options.rb +14 -0
- data/lib/translatomatic/cli/config.rb +81 -0
- data/lib/translatomatic/cli/database.rb +29 -0
- data/lib/translatomatic/cli/main.rb +112 -0
- data/lib/translatomatic/cli/translate.rb +146 -0
- data/lib/translatomatic/cli.rb +8 -216
- data/lib/translatomatic/config.rb +143 -0
- data/lib/translatomatic/converter.rb +196 -149
- data/lib/translatomatic/converter_stats.rb +18 -14
- data/lib/translatomatic/database.rb +35 -37
- data/lib/translatomatic/escaped_unicode.rb +1 -1
- data/lib/translatomatic/extractor/base.rb +2 -0
- data/lib/translatomatic/extractor/ruby.rb +1 -0
- data/lib/translatomatic/extractor.rb +1 -0
- data/lib/translatomatic/http_request.rb +43 -14
- data/lib/translatomatic/locale.rb +28 -4
- data/lib/translatomatic/logger.rb +21 -13
- data/lib/translatomatic/model/locale.rb +5 -1
- data/lib/translatomatic/model/text.rb +2 -0
- data/lib/translatomatic/model.rb +1 -0
- data/lib/translatomatic/option.rb +75 -10
- data/lib/translatomatic/progress_updater.rb +7 -3
- data/lib/translatomatic/resource_file/base.rb +41 -18
- data/lib/translatomatic/resource_file/html.rb +11 -14
- data/lib/translatomatic/resource_file/markdown.rb +13 -12
- data/lib/translatomatic/resource_file/plist.rb +3 -14
- data/lib/translatomatic/resource_file/properties.rb +21 -3
- data/lib/translatomatic/resource_file/resw.rb +1 -0
- data/lib/translatomatic/resource_file/text.rb +1 -0
- data/lib/translatomatic/resource_file/xcode_strings.rb +23 -14
- data/lib/translatomatic/resource_file/xml.rb +34 -22
- data/lib/translatomatic/resource_file/yaml.rb +39 -5
- data/lib/translatomatic/resource_file.rb +7 -5
- data/lib/translatomatic/string.rb +40 -12
- data/lib/translatomatic/tmx/document.rb +11 -12
- data/lib/translatomatic/tmx/translation_unit.rb +5 -1
- data/lib/translatomatic/tmx.rb +2 -0
- data/lib/translatomatic/translation.rb +30 -0
- data/lib/translatomatic/translation_result.rb +45 -45
- data/lib/translatomatic/translator/base.rb +128 -83
- data/lib/translatomatic/translator/frengly.rb +62 -57
- data/lib/translatomatic/translator/google.rb +35 -31
- data/lib/translatomatic/translator/microsoft.rb +41 -33
- data/lib/translatomatic/translator/my_memory.rb +68 -64
- data/lib/translatomatic/translator/yandex.rb +56 -39
- data/lib/translatomatic/translator.rb +99 -63
- data/lib/translatomatic/util.rb +31 -1
- data/lib/translatomatic/version.rb +4 -1
- data/lib/translatomatic.rb +17 -0
- data/translatomatic.gemspec +5 -4
- metadata +56 -16
@@ -0,0 +1,112 @@
|
|
1
|
+
# Command line interface to translatomatic
|
2
|
+
module Translatomatic::CLI
|
3
|
+
# Main command line interface
|
4
|
+
class Main < Base
|
5
|
+
|
6
|
+
begin
|
7
|
+
config = Translatomatic::Config.instance
|
8
|
+
I18n.default_locale = config.default_locale
|
9
|
+
end
|
10
|
+
|
11
|
+
package_name "Translatomatic"
|
12
|
+
map %W[-v --version] => :version
|
13
|
+
map %W[-L --list] => :translators
|
14
|
+
|
15
|
+
desc "translate", t("cli.translate.subcommand")
|
16
|
+
subcommand "translate", Translate
|
17
|
+
|
18
|
+
desc "database", t("cli.database.subcommand")
|
19
|
+
subcommand "database", Database
|
20
|
+
|
21
|
+
desc "config", t("cli.config.subcommand")
|
22
|
+
subcommand "config", Config
|
23
|
+
|
24
|
+
desc "display file [key...]", t("cli.display_values")
|
25
|
+
thor_options(self, Translatomatic::CLI::CommonOptions)
|
26
|
+
method_option :locales, type: :string, desc: t("cli.locales_to_display")
|
27
|
+
method_option :sentences, type: :boolean, desc: t("cli.display_sentences")
|
28
|
+
# Display values from a resource bundle
|
29
|
+
# @param file [String] Path to resource file
|
30
|
+
# @param keys [Array<String>] Optional list of locales
|
31
|
+
# @return [void]
|
32
|
+
def display(file, *keys)
|
33
|
+
run do
|
34
|
+
source = Translatomatic::ResourceFile.load(file)
|
35
|
+
keys = source.properties.keys if keys.empty?
|
36
|
+
display_keys(source, keys)
|
37
|
+
|
38
|
+
# TODO: if locales not specified, determine the list of locales from
|
39
|
+
# all the files in the resource bundle.
|
40
|
+
locales = parse_list(options[:locales])
|
41
|
+
locales << Translatomatic::Locale.default.to_s if locales.empty?
|
42
|
+
locales.each do |tag|
|
43
|
+
locale = locale(tag)
|
44
|
+
next if locale == source.locale
|
45
|
+
display_properties(source, keys, locale)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc "strings file [file...]", t("cli.extract_strings")
|
51
|
+
thor_options(self, Translatomatic::CLI::CommonOptions)
|
52
|
+
# Extract strings from non-resource files
|
53
|
+
# @param files [Array<String>] List of paths to files
|
54
|
+
# @return [void]
|
55
|
+
def strings(*files)
|
56
|
+
run do
|
57
|
+
strings = []
|
58
|
+
files.each do |file|
|
59
|
+
extractor = Translatomatic::Extractor::Base.new(file)
|
60
|
+
strings << extractor.extract
|
61
|
+
end
|
62
|
+
puts strings.join("\n")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
desc "services", t("cli.list_backends")
|
67
|
+
thor_options(self, Translatomatic::CLI::CommonOptions)
|
68
|
+
# List available translator services
|
69
|
+
# @return [void]
|
70
|
+
def services
|
71
|
+
run { puts Translatomatic::Translator.list }
|
72
|
+
end
|
73
|
+
|
74
|
+
desc 'version', t("cli.display_version")
|
75
|
+
thor_options(self, Translatomatic::CLI::CommonOptions)
|
76
|
+
# Display version number
|
77
|
+
# @return [void]
|
78
|
+
def version
|
79
|
+
puts "Translatomatic v#{Translatomatic::VERSION}"
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def display_properties(source, keys, locale)
|
85
|
+
path = source.locale_path(locale)
|
86
|
+
if path.exist?
|
87
|
+
resource = Translatomatic::ResourceFile.load(path)
|
88
|
+
display_keys(resource, keys)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def display_keys(source, keys)
|
93
|
+
puts t("cli.file_source", file: source)
|
94
|
+
rows = []
|
95
|
+
keys.each do |key|
|
96
|
+
value = source.get(key)
|
97
|
+
rows << [key + ":", value]
|
98
|
+
end
|
99
|
+
print_table(rows, indent: 2)
|
100
|
+
|
101
|
+
if options[:sentences]
|
102
|
+
puts t("cli.sentences")
|
103
|
+
source.sentences.each do |sentence|
|
104
|
+
puts "- " + sentence.to_s
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
puts
|
109
|
+
end
|
110
|
+
|
111
|
+
end # class
|
112
|
+
end # module
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# Command line interface to translatomatic
|
2
|
+
module Translatomatic::CLI
|
3
|
+
# Translation functions for the command line interface
|
4
|
+
class Translate < Base
|
5
|
+
|
6
|
+
define_options(
|
7
|
+
{ name: :translator, type: :array, aliases: "-t",
|
8
|
+
desc: t("converter.translator"), enum: Translatomatic::Translator.names
|
9
|
+
},
|
10
|
+
{ name: :source_locale, desc: t("cli.source_locale") },
|
11
|
+
{ name: :share, desc: t("cli.share"), default: false },
|
12
|
+
{ name: :target_locales, desc: t("cli.target_locales"),
|
13
|
+
type: :array, hidden: true },
|
14
|
+
)
|
15
|
+
|
16
|
+
desc "string text locale...", t("cli.translate.string")
|
17
|
+
thor_options(self, Translatomatic::CLI::CommonOptions)
|
18
|
+
thor_options(self, Translatomatic::CLI::Translate)
|
19
|
+
thor_options(self, Translatomatic::Translator.modules)
|
20
|
+
# Translate a string to target locales
|
21
|
+
# @param text [String] String to translate
|
22
|
+
# @param locales [Array<String>] List of target locales
|
23
|
+
# @return [void]
|
24
|
+
def string(text, *locales)
|
25
|
+
run do
|
26
|
+
setup_translation(locales)
|
27
|
+
|
28
|
+
puts "(%s) %s" % [@source_locale, text]
|
29
|
+
@translators.each do |translator|
|
30
|
+
puts t("cli.using_translator", name: translator.name)
|
31
|
+
@target_locales.each do |target_locale|
|
32
|
+
result = translator.translate([text], @source_locale, target_locale)
|
33
|
+
puts " -> (%s) %s" % [target_locale, result]
|
34
|
+
end
|
35
|
+
puts
|
36
|
+
end
|
37
|
+
|
38
|
+
finish_log
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "file filename locale...", t("cli.translate.file")
|
43
|
+
thor_options(self, Translatomatic::CLI::CommonOptions)
|
44
|
+
thor_options(self, Translatomatic::CLI::Translate)
|
45
|
+
thor_options(self, Translatomatic::Converter)
|
46
|
+
thor_options(self, Translatomatic::Database)
|
47
|
+
thor_options(self, Translatomatic::Translator.modules)
|
48
|
+
# Translate files to target locales
|
49
|
+
# @param file [String] Resource file to translate
|
50
|
+
# @param locales [Array<String>] List of target locales
|
51
|
+
# @return [void]
|
52
|
+
def file(file, *locales)
|
53
|
+
run do
|
54
|
+
setup_translation(locales)
|
55
|
+
|
56
|
+
# load source file
|
57
|
+
raise t("cli.file_not_found", file: file) unless File.exist?(file)
|
58
|
+
source = Translatomatic::ResourceFile.load(file, @source_locale)
|
59
|
+
raise t("cli.file_unsupported", file: file) unless source
|
60
|
+
|
61
|
+
# set up database
|
62
|
+
Translatomatic::Database.new(options)
|
63
|
+
|
64
|
+
log.debug(t("cli.locales_properties", locales: locales, properties: source.properties.length))
|
65
|
+
|
66
|
+
# set up converter
|
67
|
+
translation_count = calculate_translation_count(source, @target_locales)
|
68
|
+
converter_options = options.merge(
|
69
|
+
translator: @translators,
|
70
|
+
listener: progress_updater(translation_count)
|
71
|
+
)
|
72
|
+
converter = Translatomatic::Converter.new(converter_options)
|
73
|
+
|
74
|
+
# convert source to locale(s) and write files
|
75
|
+
@target_locales.each do |i|
|
76
|
+
to_locale = locale(i)
|
77
|
+
next if to_locale.language == source.locale.language
|
78
|
+
converter.translate_to_file(source, to_locale)
|
79
|
+
end
|
80
|
+
|
81
|
+
log.info converter.stats
|
82
|
+
finish_log
|
83
|
+
|
84
|
+
share_translations(converter) if cli_option(:share)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def setup_translation(locales)
|
91
|
+
log.info(t("cli.dry_run")) if cli_option(:dry_run)
|
92
|
+
|
93
|
+
@target_locales = parse_list(locales, cli_option(:target_locales))
|
94
|
+
@source_locale = determine_source_locale
|
95
|
+
raise t("cli.locales_required") if @target_locales.empty?
|
96
|
+
conf.logger.level = Logger::DEBUG if cli_option(:debug)
|
97
|
+
|
98
|
+
# resolve translators
|
99
|
+
@translators = Translatomatic::Translator.resolve(
|
100
|
+
cli_option(:translator), options
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
def determine_source_locale
|
105
|
+
cli_option(:source_locale) || conf.default_locale
|
106
|
+
end
|
107
|
+
|
108
|
+
def calculate_translation_count(source, locales)
|
109
|
+
source.sentences.length * locales.length
|
110
|
+
end
|
111
|
+
|
112
|
+
def share_translations(converter)
|
113
|
+
return if converter.db_translations.empty?
|
114
|
+
|
115
|
+
tmx = Translatomatic::TMX::Document.from_texts(converter.db_translations)
|
116
|
+
available = Translatomatic::Translator.available(options)
|
117
|
+
available.each do |translator|
|
118
|
+
if translator.respond_to?(:upload)
|
119
|
+
log.info(t("cli.uploading_tmx", name: translator.name))
|
120
|
+
translator.upload(tmx)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
ActiveRecord::Base.transaction do
|
125
|
+
converter.db_translations.each do |text|
|
126
|
+
text.update(shared: true) if text.is_translated?
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# create a progress bar and progress updater
|
132
|
+
def progress_updater(translation_count)
|
133
|
+
return nil unless cli_option(:wank)
|
134
|
+
# set up progress bar
|
135
|
+
progressbar = ProgressBar.create(
|
136
|
+
title: t("cli.translating"),
|
137
|
+
format: "%t: |%B| %E ",
|
138
|
+
autofinish: false,
|
139
|
+
total: translation_count
|
140
|
+
)
|
141
|
+
conf.logger.progressbar = progressbar
|
142
|
+
Translatomatic::ProgressUpdater.new(progressbar)
|
143
|
+
end
|
144
|
+
|
145
|
+
end # class
|
146
|
+
end # module
|
data/lib/translatomatic/cli.rb
CHANGED
@@ -1,216 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
desc "translate file locale...", "Translate files to target locales"
|
11
|
-
method_option :translator, enum: Translatomatic::Translator.names
|
12
|
-
method_option :source_locale, desc: "The locale of the source file"
|
13
|
-
method_option :debug, type: :boolean, desc: "Enable debugging output"
|
14
|
-
method_option :wank, type: :boolean, default: true, desc: "Enable Progress bar"
|
15
|
-
method_option :share, type: :boolean, default: false, desc:
|
16
|
-
"Share translations with translators that support upload"
|
17
|
-
Translatomatic::Converter.options.each do |option|
|
18
|
-
method_option option.name, option.to_hash
|
19
|
-
end
|
20
|
-
Translatomatic::Database.options.each do |option|
|
21
|
-
method_option option.name, option.to_hash
|
22
|
-
end
|
23
|
-
Translatomatic::Translator.modules.each do |mod|
|
24
|
-
mod.options.each do |option|
|
25
|
-
method_option option.name, option.to_hash
|
26
|
-
end
|
27
|
-
end
|
28
|
-
def translate(file, *locales)
|
29
|
-
run do
|
30
|
-
log.info("Dry run: files will not be translated or written") if options[:dry_run]
|
31
|
-
raise "One or more locales required" if locales.empty?
|
32
|
-
|
33
|
-
config.logger.level = Logger::DEBUG if options[:debug]
|
34
|
-
|
35
|
-
# load source file
|
36
|
-
raise "File not found: #{file}" unless File.exist?(file)
|
37
|
-
source = Translatomatic::ResourceFile.load(file, options[:source_locale])
|
38
|
-
raise "Unsupported file type #{file}" unless source
|
39
|
-
|
40
|
-
# set up database
|
41
|
-
Translatomatic::Database.new(options)
|
42
|
-
|
43
|
-
# select translator
|
44
|
-
translator = select_translator(options)
|
45
|
-
log.info("Using translator #{translator.name}")
|
46
|
-
log.debug("Locales: #{locales}, Properties: #{source.properties.length}")
|
47
|
-
|
48
|
-
# set up converter
|
49
|
-
translation_count = calculate_translation_count(source, locales)
|
50
|
-
converter_options = options.merge(
|
51
|
-
translator: translator, listener: progress_updater(translation_count)
|
52
|
-
)
|
53
|
-
converter = Translatomatic::Converter.new(converter_options)
|
54
|
-
|
55
|
-
# convert source to locale(s)
|
56
|
-
locales.each { |i| converter.translate(source, i) }
|
57
|
-
|
58
|
-
log.info converter.stats
|
59
|
-
config.logger.finish
|
60
|
-
|
61
|
-
share_translations(converter) if options[:share]
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
desc "display file [key...]", "Display values from a resource bundle"
|
66
|
-
method_option :locales, type: :string, desc: "Locales to display"
|
67
|
-
method_option :sentences, type: :boolean, desc: "Display sentences"
|
68
|
-
def display(file, *keys)
|
69
|
-
run do
|
70
|
-
source = Translatomatic::ResourceFile.load(file)
|
71
|
-
keys = source.properties.keys if keys.empty?
|
72
|
-
display_keys(source, keys)
|
73
|
-
|
74
|
-
# TODO: if locales not specified, determine the list of locales from
|
75
|
-
# all the files in the resource bundle.
|
76
|
-
locales = (options[:locales] || "").split(',').flatten.compact
|
77
|
-
locales << Translatomatic::Locale.default.to_s if locales.empty?
|
78
|
-
locales.each do |locale|
|
79
|
-
display_properties(source, locale)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
desc "strings file [file...]", "Extract strings from non-resource files"
|
85
|
-
def strings(*files)
|
86
|
-
run do
|
87
|
-
strings = []
|
88
|
-
files.each do |file|
|
89
|
-
extractor = Translatomatic::Extractor::Base.new(file)
|
90
|
-
strings << extractor.extract
|
91
|
-
end
|
92
|
-
puts strings.join("\n")
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
desc "list", "List available translation backends"
|
97
|
-
def list
|
98
|
-
run { puts Translatomatic::Translator.list }
|
99
|
-
end
|
100
|
-
|
101
|
-
desc 'version', 'Display version'
|
102
|
-
def version
|
103
|
-
puts "Translatomatic version #{Translatomatic::VERSION}"
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def calculate_translation_count(source, locales)
|
109
|
-
source.sentences.length * locales.length
|
110
|
-
end
|
111
|
-
|
112
|
-
def share_translations(converter)
|
113
|
-
return if converter.db_translations.empty?
|
114
|
-
|
115
|
-
tmx = Translatomatic::TMX::Document.from_texts(converter.db_translations)
|
116
|
-
available = Translatomatic::Translator.available(options)
|
117
|
-
available.each do |translator|
|
118
|
-
if translator.respond_to?(:upload)
|
119
|
-
log.debug("Uploading tmx to #{translator.name}")
|
120
|
-
translator.upload(tmx)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
ActiveRecord::Base.transaction do
|
125
|
-
converter.db_translations.each do |text|
|
126
|
-
text.update(shared: true) if text.is_translated?
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
# create a progress bar and progress updater
|
132
|
-
def progress_updater(translation_count)
|
133
|
-
return nil unless options[:wank]
|
134
|
-
# set up progress bar
|
135
|
-
progressbar = ProgressBar.create(
|
136
|
-
title: "Translating",
|
137
|
-
format: "%t: |%B| %E ",
|
138
|
-
autofinish: false,
|
139
|
-
total: translation_count
|
140
|
-
)
|
141
|
-
config.logger.progressbar = progressbar
|
142
|
-
Translatomatic::ProgressUpdater.new(progressbar)
|
143
|
-
end
|
144
|
-
|
145
|
-
# run the give code block, display exceptions.
|
146
|
-
# return true if the code ran without exceptions
|
147
|
-
def run
|
148
|
-
begin
|
149
|
-
yield
|
150
|
-
true
|
151
|
-
rescue Interrupt
|
152
|
-
puts "\nAborted"
|
153
|
-
false
|
154
|
-
rescue Exception => e
|
155
|
-
log.error(e.message)
|
156
|
-
log.debug(e.backtrace.join("\n"))
|
157
|
-
false
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
def display_properties(source, locale)
|
162
|
-
path = source.locale_path(locale)
|
163
|
-
if path.exist?
|
164
|
-
resource = Translatomatic::ResourceFile.load(path)
|
165
|
-
display_keys(resource, keys)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
def display_keys(source, keys)
|
170
|
-
puts "File: #{source}"
|
171
|
-
table = []
|
172
|
-
keys.each do |key|
|
173
|
-
value = source.get(key)
|
174
|
-
table << [key + ":", value]
|
175
|
-
end
|
176
|
-
print_table(table, indent: 2)
|
177
|
-
|
178
|
-
if options[:sentences]
|
179
|
-
puts "Sentences:"
|
180
|
-
source.sentences.each do |sentence|
|
181
|
-
puts "- " + sentence.to_s
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
puts
|
186
|
-
end
|
187
|
-
|
188
|
-
def config
|
189
|
-
Translatomatic::Config.instance
|
190
|
-
end
|
191
|
-
|
192
|
-
def select_translator(options)
|
193
|
-
# use options translator if specified
|
194
|
-
if options[:translator]
|
195
|
-
klass = Translatomatic::Translator.find(options[:translator])
|
196
|
-
return klass.new(options)
|
197
|
-
end
|
198
|
-
|
199
|
-
# find all available translators that work with the given options
|
200
|
-
available = Translatomatic::Translator.available(options)
|
201
|
-
if available.empty?
|
202
|
-
raise "No translators configured. Use the translators command to see options"
|
203
|
-
end
|
204
|
-
|
205
|
-
return available[0] if available.length == 1
|
206
|
-
|
207
|
-
# prompt user for which translator to use
|
208
|
-
say("Multiple translators available:")
|
209
|
-
available.each_with_index { |mod, i| say(" #{i + 1}) #{mod.name}") }
|
210
|
-
loop do
|
211
|
-
idx = ask("Select translator (1-#{available.length}): ")
|
212
|
-
idx = idx.to_i
|
213
|
-
return available[idx - 1] if (1..available.length).include?(idx)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
1
|
+
module Translatomatic::CLI; end
|
2
|
+
|
3
|
+
require 'translatomatic/cli/common_options'
|
4
|
+
require 'translatomatic/cli/base'
|
5
|
+
require 'translatomatic/cli/translate'
|
6
|
+
require 'translatomatic/cli/database'
|
7
|
+
require 'translatomatic/cli/config'
|
8
|
+
require 'translatomatic/cli/main'
|
@@ -1,12 +1,155 @@
|
|
1
1
|
require 'singleton'
|
2
|
+
|
3
|
+
# Translatomatic configuration
|
2
4
|
class Translatomatic::Config
|
3
5
|
include Singleton
|
6
|
+
include Translatomatic::Util
|
4
7
|
|
8
|
+
# @return [Logger] The logger instance
|
5
9
|
attr_accessor :logger
|
6
10
|
|
11
|
+
# @return [String] The default locale
|
12
|
+
attr_accessor :default_locale
|
13
|
+
|
14
|
+
# Change a configuration setting
|
15
|
+
# @param key [String] configuration key
|
16
|
+
# @param value [String] new value for the configuration
|
17
|
+
# @return [String] the new value
|
18
|
+
def set(key, value)
|
19
|
+
key = check_valid_key(key)
|
20
|
+
@settings[key] = value
|
21
|
+
save
|
22
|
+
end
|
23
|
+
|
24
|
+
# Get a configuration setting
|
25
|
+
# @param key [String] configuration key
|
26
|
+
# @return [String] configuration value
|
27
|
+
def get(key)
|
28
|
+
key = check_valid_key(key)
|
29
|
+
option = option(key)
|
30
|
+
env_name = option.name.to_s.upcase
|
31
|
+
if @settings.include?(key)
|
32
|
+
cast(@settings[key], option.type)
|
33
|
+
elsif option.use_env && ENV.include?(env_name)
|
34
|
+
cast(ENV[env_name], option.type)
|
35
|
+
else
|
36
|
+
option.default
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Remove a configuration setting
|
41
|
+
# @param key [String] configuration key to remove
|
42
|
+
# @return [void]
|
43
|
+
def remove(key)
|
44
|
+
key = check_valid_key(key)
|
45
|
+
@settings.delete(key)
|
46
|
+
save
|
47
|
+
end
|
48
|
+
|
49
|
+
# Test if configuration includes the given key
|
50
|
+
# @param key [String] configuration key
|
51
|
+
# @return [boolean] true if the configuration key is set
|
52
|
+
def include?(key)
|
53
|
+
key = check_valid_key(key)
|
54
|
+
@settings.include?(key)
|
55
|
+
end
|
56
|
+
|
57
|
+
# @return [Hash<String,String>] configuration settings
|
58
|
+
def settings
|
59
|
+
@settings.dup
|
60
|
+
end
|
61
|
+
|
62
|
+
# Save configuration settings
|
63
|
+
def save
|
64
|
+
FileUtils.mkdir_p(File.dirname(@settings_path))
|
65
|
+
File.open(@settings_path, "w") { |f| f.puts @settings.to_yaml }
|
66
|
+
end
|
67
|
+
|
68
|
+
# Load configuration from the config file
|
69
|
+
def load
|
70
|
+
@settings = YAML.load_file(@settings_path) if File.exist?(@settings_path)
|
71
|
+
@settings ||= {}
|
72
|
+
@settings.delete_if { |i| !valid_key?(i) }
|
73
|
+
@settings
|
74
|
+
end
|
75
|
+
|
76
|
+
# @return [Array<Translatomatic::Option] all available options
|
77
|
+
def self.options
|
78
|
+
self.config_options.values
|
79
|
+
end
|
80
|
+
|
7
81
|
private
|
8
82
|
|
83
|
+
SETTINGS_PATH = File.join(Dir.home, ".translatomatic", "config.yml")
|
84
|
+
|
85
|
+
# @return [Hash<String,Translatomatic::Option>] options
|
86
|
+
def self.config_options
|
87
|
+
@config_options ||= begin
|
88
|
+
# create mapping from option name to option object
|
89
|
+
map = {}
|
90
|
+
sources = [
|
91
|
+
Translatomatic::CLI::CommonOptions,
|
92
|
+
Translatomatic::CLI::Translate,
|
93
|
+
Translatomatic::Translator.modules,
|
94
|
+
Translatomatic::Database,
|
95
|
+
Translatomatic::Converter
|
96
|
+
]
|
97
|
+
sources.each do |source|
|
98
|
+
source_options = Translatomatic::Option.options_from_object(source)
|
99
|
+
source_options.each do |sopt|
|
100
|
+
optname = sopt.name.to_s
|
101
|
+
raise "#{optname} already defined" if map.include?(optname)
|
102
|
+
map[optname] = sopt
|
103
|
+
end
|
104
|
+
end
|
105
|
+
map
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def cast(value, type)
|
110
|
+
case type
|
111
|
+
when :boolean
|
112
|
+
return true if ["true", "t", "yes", "on"].include?(value)
|
113
|
+
return false if ["false", "f", "no", "off"].include?(value)
|
114
|
+
return value ? true : false
|
115
|
+
when :string
|
116
|
+
return value.nil? ? value : value.to_s
|
117
|
+
when :array
|
118
|
+
unless value.nil?
|
119
|
+
value = [value] unless value.kind_of?(Array)
|
120
|
+
value = value.collect { |i| i.split(/[, ]/) }.flatten.compact
|
121
|
+
end
|
122
|
+
end
|
123
|
+
value
|
124
|
+
end
|
125
|
+
|
126
|
+
def option(key)
|
127
|
+
self.class.config_options[key.to_s]
|
128
|
+
end
|
129
|
+
|
130
|
+
def check_valid_key(key)
|
131
|
+
key = key.to_s
|
132
|
+
raise t("config.invalid_key", key: key) unless valid_key?(key)
|
133
|
+
key
|
134
|
+
end
|
135
|
+
|
136
|
+
def valid_key?(key)
|
137
|
+
self.class.config_options.include?(key.to_s)
|
138
|
+
end
|
139
|
+
|
140
|
+
# override settings path, used for testing
|
141
|
+
def settings_path=(path)
|
142
|
+
@settings = {}
|
143
|
+
@settings_path = path
|
144
|
+
load
|
145
|
+
end
|
146
|
+
|
9
147
|
def initialize
|
10
148
|
@logger = Translatomatic::Logger.new
|
149
|
+
lang = (ENV['LANG'] || '').split(/\./)[0]
|
150
|
+
@default_locale = Translatomatic::Locale.parse(lang).language || "en"
|
151
|
+
@settings_path = SETTINGS_PATH
|
152
|
+
load
|
11
153
|
end
|
154
|
+
|
12
155
|
end
|