Skip to content

Commit

Permalink
Add Rakefile.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Catty committed Dec 31, 2008
1 parent 5c8f0bb commit 186dca5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
21 changes: 10 additions & 11 deletions README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Translation
= Translate

Translate provides an easy to translate word or expression into another,
using wordreference. This website does'nt provide API yet so I decided
Expand All @@ -20,38 +20,37 @@ Download and install translate with the following:

Checkout the source code on github: git clone git://github.com/fuse/translate.git

# ruby install.rb
ruby install.rb

It will ask you where you want to install the translate library and binary in
your path.

Warning:
Be aware that library is prior to gem. If you have an old version of the library
and install recent gem it might not work as expected.
_Warning_:
Be carefull to *NOT* install translate with the both way.

== Usage

=== From command line

$ translate world
translate world
Will translate world from english to french.

$ translate casa -f it -t en
translate casa -f it -t en
Will translate casa from italian to english.

If you want to know which language can be used :
$ translate -l
translate -l

If you want to know all options of translate :
$ translate -h
translate -h

=== From another program

You only have to create a new instance or the translation class by giving
the expression to translate and your options and explicitly call translate.

translation = Translation.new("maison", { :from => :fr, :to => :en, :more => true })
translation.translate
translation = Translation.new("maison", { :from => :fr, :to => :en, :more => true })
translation.translate

Results are directly available through translation.items

Expand Down
Empty file modified install.rb
100755 → 100644
Empty file.
7 changes: 7 additions & 0 deletions lib/language.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Translate
module Language
# Language module contains the different languages available and defined
# the directions which can be used.
EN = "English"
FR = "French"
IT = "Italian"
Expand All @@ -18,6 +20,10 @@ module Language
IT => [ "EN" ]
}

# Show the available translations on the command line. You can invoke
# this method by using :
# $ translate -l
# It will print the language's abbreviation and real name (in english).
def self.available_translations
puts "Available translations :\n"
(Language.constants - [DIRECTIONS]).sort.each do |l|
Expand All @@ -28,6 +34,7 @@ def self.available_translations
end
end # available_translations

# Check if the original and final languages are available.
def self.available_translation?(*args)
from, to = args.map { |a| a.to_s.strip.upcase }
return false if from.blank? or to.blank?
Expand Down
12 changes: 6 additions & 6 deletions lib/string.extend.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class String
def blank?
nil? or strip.empty?
end # blank
def blank?
self !~ /\S/
end # blank

def truncate(length = 30, truncate_string = "...")
return if blank?
(self.length > length ? self[0...(length - truncate_string.length)] + truncate_string : self).to_s
end # truncate
return if blank?
(self.length > length ? self[0...(length - truncate_string.length)] + truncate_string : self).to_s
end # truncate

def sanitize
# strip html tags and html code of the arrow (=>)
Expand Down
56 changes: 28 additions & 28 deletions lib/translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ class Translation

# Initialize translation with expression and options.
# Ensure width will be an integer, and provides an empty array of items.
def initialize(expression, options = {})
self.expression = expression
options = OPTIONS.merge(options)
for k in options.keys
self.send("#{k}=", options[k]) if self.respond_to?(k)
end
def initialize(expression, options = {})
self.expression = expression
options = OPTIONS.merge(options)
for k in options.keys
self.send("#{k}=", options[k]) if self.respond_to?(k)
end
@items = []
@width = @width.to_i
end # initialize
end # initialize

# Provide the from, to combination. Example: from en to fr will give enfr.
# Usefull to build the final url.
Expand All @@ -91,29 +91,29 @@ def separator
# Parse the document retrieved from tree and add items into
# the final results array.
def translate
return nil unless valid?
return nil unless valid?
continue = false
@items.clear
klass = Language.respond_to?("klass_#{combination}") ? Language.send("klass_#{combination}") : "2"
tree.search("table.Rtbl#{klass}/tr").each do |child|
head = child.search("td.Head:first").first
tree.search("table.Rtbl#{klass}/tr").each do |child|
head = child.search("td.Head:first").first
continue = !! (
head.html =~ /Principal/ ||
head.attributes['title'] =~ /Principal/ ||
(head.html =~ /Additional/ and more) ||
(head.attributes['title'] =~ /Additional/)) if head.respond_to?(:html)
unless ! continue or child.classes =~ /(evenEx|oddEx)/
# strip html tags
description = child.search("td.FrCN#{klass}/*").to_s.sanitize
translation = child.search("td.ToW#{klass}/*").to_s.sanitize
unless ! continue or child.classes =~ /(evenEx|oddEx)/
# strip html tags
description = child.search("td.FrCN#{klass}/*").to_s.sanitize
translation = child.search("td.ToW#{klass}/*").to_s.sanitize
@items << Item.new({
:description => description,
:translation => translation,
:new_line => ! child.search("td.FrW#{klass}").empty?
}) unless description.empty? and translation.empty?
end
end
end
end # translate
end # translate

# Only used when translate is called from command line.
# Print results in the shell.
Expand Down Expand Up @@ -161,21 +161,21 @@ def valid?

# Call a wordreference url, dynamiquely builded with from expression and combination.
# We need to provide an User-Agent otherwise we're redirected to yahoo.
def retrieve
request = Net::HTTP::Get.new(url.path)
request.add_field('User-Agent', 'translate')
Net::HTTP.new(url.host, url.port).request(request).body
end # retrieve
def retrieve
request = Net::HTTP::Get.new(url.path)
request.add_field('User-Agent', 'translate')
Net::HTTP.new(url.host, url.port).request(request).body
end # retrieve
end

class Item
attr_accessor :description, :translation, :new_line

def initialize(attributes = {})
for k in attributes.keys
self.send("#{k}=", attributes[k]) if self.respond_to?(k)
end
end # initialize
for k in attributes.keys
self.send("#{k}=", attributes[k]) if self.respond_to?(k)
end
end # initialize
end

# Load options from config file if exists.
Expand Down Expand Up @@ -265,10 +265,10 @@ def parse_command_line

# Only used when translate is called from command line.
# Call the above methods to show results.
def translate
def translate
trap(:INT) { puts "Bye." ; exit }
translation = parse_command_line
translation = parse_command_line
translation.translate
translation.errors.empty? ? translation.print : translation.print_errors
end # translate
end # translate
end

0 comments on commit 186dca5

Please sign in to comment.