Module: Translate
- Defined in:
- lib/language.rb,
lib/translate.rb
Defined Under Namespace
Modules: Language Classes: Item, Translation
Constant Summary collapse
- URL =
You can change default options below. Default settings translate from english to french without additional translations.
'http://www.wordreference.com/'- WIDTH =
Default width, can be overriden.
78- MIN_WIDTH =
40- CONFIG_FILE =
"translate.yml"
Instance Method Summary collapse
-
#config ⇒ Object
Load options from config file if exists.
-
#config_file_path ⇒ Object
Define config file path.
-
#has_config_file? ⇒ Boolean
Check if a config file has been set or not.
-
#home ⇒ Object
Try to return the home path using environement var.
-
#parse_command_line ⇒ Object
Parse the command line to register user’s options or display help or examples.
-
#translate ⇒ Object
Call the above methods to show results.
Instance Method Details
#config ⇒ Object
Load options from config file if exists.
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/translate.rb', line 182 def config if has_config_file? section = YAML::load(File.open(config_file_path))["translate"] return section.nil? ? {} : section.keys.inject({}) { |h, k| h[k.to_sym] = section[k] h } end {} end |
#config_file_path ⇒ Object
Define config file path.
194 195 196 |
# File 'lib/translate.rb', line 194 def config_file_path File.join(home, CONFIG_FILE) end |
#has_config_file? ⇒ Boolean
Check if a config file has been set or not.
199 200 201 |
# File 'lib/translate.rb', line 199 def has_config_file? File.readable?(config_file_path) end |
#home ⇒ Object
Try to return the home path using environement var. Home path is not set on the same var on unix & windows.
205 206 207 |
# File 'lib/translate.rb', line 205 def home ENV['HOME'] || ENV['USERPROFILE'] end |
#parse_command_line ⇒ Object
Parse the command line to register user’s options or display help or examples.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/translate.rb', line 210 def parse_command_line = {} OptionParser.new do |@parser| @parser. = 'Usage: translate word [options]' @parser.on('-e', '--example', 'Show some examples.') do |option| puts "Examples :\n" puts "\t ./translate house (default translation from english to french)" puts "\t ./translate house -m (show additional translations)" puts "\t ./translate maison -f fr -t en (translate from french to english)" puts "\t ./translate -f fr -t en 'se moquer' (use single or double quote to escape multiple words)" exit end @parser.on('-f', '--from language', String, 'Define original language.') do |option| [:from] = option end @parser.on('-l', '--language', 'Show supported languages / translations.') do |option| Translate::Language.available_translations exit end @parser.on('-m', '--more', 'Show additional translations.') do |option| [:more] = option end @parser.on('-t', '--to language', String, 'Define destination language.') do |option| [:to] = option end @parser.on('-w', '--width number', String, "Define width, min width is #{MIN_WIDTH}.") do |option| [:width] = option end @parser.on('-v', '--version', String, 'Show version.') do |option| puts "translate 0.2 Martin Catty <[email protected]>" exit end @parser.on_tail('-h', '--help', 'Show this message.') do puts @parser exit end begin @parser.parse!(ARGV) expression = ARGV.empty? ? "" : ARGV.shift.strip return Translation.new(expression, Translation::OPTIONS.merge(config).merge()) rescue puts $! puts @parser exit end end end |
#translate ⇒ Object
Call the above methods to show results.
268 269 270 271 272 273 |
# File 'lib/translate.rb', line 268 def translate trap(:INT) { puts "Bye." ; exit } translation = parse_command_line translation.translate translation.errors.empty? ? translation.print : translation.print_errors end |