Skip to content

Commit

Permalink
make rtranslate compatible with AS 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sishen committed Oct 31, 2010
1 parent d09f776 commit 30585ce
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
3 changes: 3 additions & 0 deletions History.txt
@@ -1,3 +1,6 @@
V1.5 Oct 31th, 2010
* Compatible with ActiveSupport 3.0

V1.3 Dec 18th, 2009
* Enable the translation for the long string which is supposed to be more than 500 characters. The lib will split the text into several groups 500 by 500 and then join the response translated text together.

Expand Down
5 changes: 3 additions & 2 deletions lib/rtranslate.rb
Expand Up @@ -14,10 +14,11 @@
require 'json'
end

$KCODE = 'u'

$KCODE = 'u' unless RUBY_VERSION >= "1.9"
unless defined?(ActiveSupport)
require 'active_support'
require 'active_support/core_ext'
end

include Translate
Expand Down
4 changes: 2 additions & 2 deletions lib/rtranslate/rtranslate.rb
Expand Up @@ -67,12 +67,12 @@ def translate(text, options = { })
to = Google::Language.abbrev(to)
langpair = "#{from}|#{to}"

text.mb_chars.scan(/(.{1,300})/).inject("") do |result, st|
text.mb_chars.scan(/(.{1,300})/).flatten.inject("") do |result, st|
url = "#{GOOGLE_TRANSLATE_URL}?q=#{CGI.escape(st.to_s)}&langpair=#{CGI.escape(langpair)}&v=#{@version}"
url << "&key=#{@key}" if @key
url << "&userip=#{options[:userip]}" if options[:userip]

result += do_translate(url)
result += CGI.unescapeHTML(do_translate(url))
end
else
raise UnsupportedLanguagePair, "Translation from '#{from}' to '#{to}' isn't supported yet!"
Expand Down
4 changes: 2 additions & 2 deletions sishen-rtranslate.gemspec
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{sishen-rtranslate}
s.version = "1.4"
s.version = "1.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ye Dingding"]
s.date = %q{2010-08-31}
s.date = %q{2010-10-31}
s.default_executable = %q{rtranslate}
s.description = %q{A simple, unofficial, ruby client API for using Google Translate.}
s.email = %q{yedingding@gmail.com}
Expand Down
14 changes: 7 additions & 7 deletions tests/ts_translate.rb
Expand Up @@ -8,25 +8,25 @@ class Translate::TranslateTest < Test::Unit::TestCase
include Google::Language
def test_english_translate
assert_equal("مرحبا العالم", Translate.t("Hello world", "ENGLISH", "ARABIC"));
assert_equal("世界您好", Translate.t("Hello world", "ENGLISH", "CHINESE_SIMPLIFIED"));
assert_equal("你好世界", Translate.t("Hello world", "ENGLISH", "CHINESE_SIMPLIFIED"));
assert_equal("Bonjour tout le monde", Translate.t("Hello world", "ENGLISH", "FRENCH"));
assert_equal("Hallo Welt", Translate.t("Hello world", "ENGLISH", "GERMAN"));
assert_equal("Ciao a tutti", Translate.t("Hello world", "ENGLISH", "ITALIAN"));
assert_equal("こんにちは、世界", Translate.t("Hello world", "ENGLISH", "JAPANESE"));
assert_equal("안녕하세요 세상", Translate.t("Hello world", "ENGLISH", "KOREAN"));
assert_equal("안녕하세요", Translate.t("Hello world", "ENGLISH", "KOREAN"));
assert_equal("Olá mundo", Translate.t("Hello world", "ENGLISH", "PORTUGUESE"));
assert_equal("Привет мир", Translate.t("Hello world", "ENGLISH", "RUSSIAN"));
assert_equal("¡Hola, mundo", Translate.t("Hello world", "ENGLISH", "SPANISH"));
end

def test_auto_detect_translate
assert_equal("مرحبا العالم", Translate.t("Hello world", nil, "ARABIC"));
assert_equal("世界您好", Translate.t("Hello world", nil, "CHINESE_SIMPLIFIED"));
assert_equal("你好世界", Translate.t("Hello world", nil, "CHINESE_SIMPLIFIED"));
assert_equal("Bonjour tout le monde", Translate.t("Hello world", nil, "FRENCH"));
assert_equal("Hallo Welt", Translate.t("Hello world", nil, "GERMAN"));
assert_equal("Ciao a tutti", Translate.t("Hello world", nil, "ITALIAN"));
assert_equal("こんにちは、世界", Translate.t("Hello world", nil, "JAPANESE"));
assert_equal("안녕하세요 세상", Translate.t("Hello world", nil, "KOREAN"));
assert_equal("안녕하세요", Translate.t("Hello world", nil, "KOREAN"));
assert_equal("Olá mundo", Translate.t("Hello world", nil, "PORTUGUESE"));
assert_equal("Привет мир", Translate.t("Hello world", nil, "RUSSIAN"));
assert_equal("¡Hola, mundo", Translate.t("Hello world", nil, "SPANISH"));
Expand All @@ -44,15 +44,15 @@ def test_unsupported_translate
end

def test_translate_strings
assert_equal(["", "世界"], Translate::RTranslate.translate_strings(["Hello", "World"], "en", "zh-CN"))
assert_equal(["你好", "世界"], Translate::RTranslate.translate_strings(["Hello", "World"], "en", "zh-CN"))
end

def test_translate_string_to_languages
assert_equal(["世界您好", "ハローワールド"], Translate::RTranslate.translate_string_to_languages("Hello World", {:from => "en", :to => ["zh-CN", "ja"]}))
assert_equal(["你好世界", "ハローワールド"], Translate::RTranslate.translate_string_to_languages("Hello World", {:from => "en", :to => ["zh-CN", "ja"]}))
end

def test_batch_translate
assert_equal(["世界您好", "ハローワールド"],
assert_equal(["你好世界", "ハローワールド"],
Translate::RTranslate.batch_translate([["Hello World", {:from => "en", :to => "zh-CN"}], ["Hello World", {:from => "en", :to => "ja"}]]))
end
end

0 comments on commit 30585ce

Please sign in to comment.