vizi_translator 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,56 +1,56 @@
1
- = ViziTranslator
2
-
3
- == Introduction
4
-
5
- This gem module provides a classes to support the language translation of text files or text strings.
6
- The Frengly on-line service requires registration and prvoides access with a username and password.
7
- The gem module uses the NET::HTTP library to access the Frengly service.
8
- Find out more about the translation service at www.frengly.com.
9
-
10
- == Installation
11
-
12
- Just run:
13
-
14
- gem install vizi_translator
15
-
16
- The following gems are required 'net/http', 'uri' and 'xmlsimple'. You can install xmlsimple with 'get install xml-simple'
17
-
18
- This sample application will read a text file for translation.
19
-
20
- The sample text file is a index markdown file written in English.
21
- The English markdown file provides content for Vizitrax's home page.
22
- The translated file can be used to replace the English content with content for another language.
23
-
24
- == Usage
25
-
26
- require 'vizi_translator'
27
-
28
- # Set username and password. Username must be registered at Frengly
29
- username = "your_username"
30
- password = "your_password"
31
-
32
- # Set target language for translation
33
- targetlanguage = "fr"
34
-
35
- begin
36
- # Open test file for translation
37
- @test = File.read(Dir.pwd+'./data/index.md')
38
-
39
- # Initiate Translator class
40
- translator = Vizi::Translator.new
41
- # Get array with available language codes
42
- @langcodes = translator.getcodes
43
- # Get hashed list of languages and language codes
44
- @langlist = translator.getlist
45
-
46
- # Call method to do the translation
47
- @testnew = translator.gettext(username, password, @test, targetlanguage)
48
-
49
- end
50
-
51
-
52
- == License
53
-
54
- This code is made available under the MIT license.
55
-
56
- All users of this code should read and follow the acceptable use policies for the Frengly translation service.
1
+ = ViziTranslator
2
+
3
+ == Introduction
4
+
5
+ This gem module provides a classes to support the language translation of text files or text strings.
6
+ The Frengly on-line service requires registration and prvoides access with a username and password.
7
+ The gem module uses the NET::HTTP library to access the Frengly service.
8
+ Find out more about the translation service at www.frengly.com.
9
+
10
+ == Installation
11
+
12
+ Just run:
13
+
14
+ gem install vizi_translator
15
+
16
+ The following gems are required 'net/http', 'uri' and 'xmlsimple'. You can install xmlsimple with 'get install xml-simple'
17
+
18
+ This sample application will read a text file for translation.
19
+
20
+ The sample text file is a index markdown file written in English.
21
+ The English markdown file provides content for Vizitrax's home page.
22
+ The translated file can be used to replace the English content with content for another language.
23
+
24
+ == Usage
25
+
26
+ require 'vizi_translator'
27
+
28
+ # Set username and password. Username must be registered at Frengly
29
+ username = "your_username"
30
+ password = "your_password"
31
+
32
+ # Set target language for translation
33
+ targetlanguage = "fr"
34
+
35
+ begin
36
+ # Open test file for translation
37
+ @test = File.read(Dir.pwd+'./data/index.md')
38
+
39
+ # Initiate Translator class
40
+ translator = Vizi::Translator.new
41
+ # Get array with available language codes
42
+ @langcodes = translator.getcodes
43
+ # Get hashed list of languages and language codes
44
+ @langlist = translator.getlist
45
+
46
+ # Call method to do the translation
47
+ @testnew = translator.gettext(username, password, @test, targetlanguage)
48
+
49
+ end
50
+
51
+
52
+ == License
53
+
54
+ This code is made available under the MIT license.
55
+
56
+ All users of this code should read and follow the acceptable use policies for the Frengly translation service.
data/build_gem.sh ADDED
@@ -0,0 +1,9 @@
1
+ # run with ... sudo bash build_gem.sh
2
+
3
+ gem uninstall vizi_translator
4
+
5
+ chmod -R a+r ./lib
6
+
7
+ gem build vizi_translator.gemspec
8
+
9
+ gem install -l vizi_translator-0.4.0.gem
@@ -0,0 +1,84 @@
1
+ # This gem module provides a language translator class based on the Frengly
2
+ # language translation service. The language translation is provided through an
3
+ # HTTP service and requires a registered username and password.
4
+ #
5
+ # Author:: Al Kivi <al.kivi@vizitrax.com>
6
+ # License:: MIT
7
+
8
+ module Vizi
9
+ require 'net/http'
10
+ require 'uri'
11
+ require 'xmlsimple'
12
+
13
+ class UnSupportedLanguage < RuntimeError
14
+ attr :msg
15
+ def initialize(message='')
16
+ @msg = "Language pair not supported yet."
17
+ end
18
+ end
19
+
20
+ # This class includes a set of methods to translate using the Frengly service
21
+ class Translator
22
+ SUPPORTED_LANG_CODES = ['fr','de','es','pt','it','nl','tl','fi','el','iw','pl','ru','sv']
23
+ SUPPORTED_LANG_LIST = {"English" => "en", "French" => "fr", "German" => "de", "Spanish" => "es",
24
+ "Portuguese" => "pt", "Italian" => "it", "Filipino" => "tl","Finnish" => "fi","Greek" => "el",
25
+ "Hebrew" => "iw", "Polish" => "pl", "Russian" => "ru", "Swedish" => "sv"}
26
+
27
+ # method to get array of language codes
28
+ def getcodes
29
+ result = SUPPORTED_LANG_CODES
30
+ end
31
+
32
+ # method to get hash list of languages
33
+ def getlist
34
+ result = SUPPORTED_LANG_LIST
35
+ end
36
+
37
+ # method to translate from one language to another
38
+ def gettext(username, password, input_text, output_code)
39
+ default_src = 'en'
40
+ default_action = 'translateREST'
41
+
42
+ input_text = URI.escape(input_text)
43
+ input_text = File.read(Dir.pwd+'/data/index.md') # xxxxxx
44
+ #input_text.gsub!("(","<(")
45
+ #input_text.gsub!(")",")>")
46
+ #input_array = input_text.split(/[<>]/)
47
+ #new_array = input_text.split(/[<>]/)
48
+ #chash = Hash.new
49
+ #for i in 0..new_array.length-1
50
+ #if new_array[i][0..0] == "("
51
+ #new_array[i] = "<"+i.to_s+">"
52
+ #chash[new_array[i]] = input_array[i]
53
+ #end
54
+ #end
55
+ ## input_text = new_array.join
56
+ p input_text
57
+ begin
58
+ raise UnSupportedLanguage unless SUPPORTED_LANG_CODES.include?(output_code)
59
+ site_url = 'www.syslang.com'
60
+ uri_method = '/frengly/controller?'
61
+ uri_string = 'action='+default_action+'&src='+default_src+'&dest='+output_code+'&text='+input_text+'&username='+username+'&password='+password
62
+ response = Net::HTTP.get_response(site_url, uri_method + uri_string)
63
+ p response
64
+ p response.code
65
+ if response.code == "200"
66
+ xml_data = response.body
67
+ data = XmlSimple.xml_in(xml_data)
68
+ p 'in 200 loop'
69
+ result = data["translation"][0]
70
+ p result
71
+ else
72
+ puts response.body
73
+ raise StandardError, response.body
74
+ end
75
+ rescue UnSupportedLanguage
76
+ raise UnSupportedLanguage.new
77
+ rescue => err_msg
78
+ puts "#{err_msg}"
79
+ end
80
+ end
81
+
82
+ end # Class
83
+
84
+ end # Vizi module
@@ -0,0 +1,66 @@
1
+ # This gem module provides a language translator class based on the Frengly
2
+ # language translation service. The language translation is provided through an
3
+ # HTTP service and requires a registered username and password.
4
+ #
5
+ # Author:: Al Kivi <al.kivi@vizitrax.com>
6
+ # License:: MIT
7
+
8
+ module Vizi
9
+ require 'net/http'
10
+ require 'uri'
11
+ require 'xmlsimple'
12
+
13
+ class UnSupportedLanguage < RuntimeError
14
+ attr :msg
15
+ def initialize(message='')
16
+ @msg = "Language pair not supported yet."
17
+ end
18
+ end
19
+
20
+ # This class includes a set of methods to translate using the Frengly service
21
+ class Translator
22
+ SUPPORTED_LANG_CODES = ['fr','de','es','pt','it','nl','tl','fi','el','iw','pl','ru','sv']
23
+ SUPPORTED_LANG_LIST = {"English" => "en", "French" => "fr", "German" => "de", "Spanish" => "es",
24
+ "Portuguese" => "pt", "Italian" => "it", "Filipino" => "tl","Finnish" => "fi","Greek" => "el",
25
+ "Hebrew" => "iw", "Polish" => "pl", "Russian" => "ru", "Swedish" => "sv"}
26
+
27
+ # method to get array of language codes
28
+ def getcodes
29
+ result = SUPPORTED_LANG_CODES
30
+ end
31
+
32
+ # method to get hash list of languages
33
+ def getlist
34
+ result = SUPPORTED_LANG_LIST
35
+ end
36
+
37
+ # method to translate from one language to another
38
+ def gettext(username, password, input_text, output_code)
39
+ default_src = 'en'
40
+ default_action = 'translateREST'
41
+
42
+ input_text = URI.escape(input_text)
43
+ begin
44
+ raise UnSupportedLanguage unless SUPPORTED_LANG_CODES.include?(output_code)
45
+ site_url = 'www.syslang.com'
46
+ uri_method = '/frengly/controller?'
47
+ uri_string = 'action='+default_action+'&src='+default_src+'&dest='+output_code+'&text='+input_text+'&username='+username+'&password='+password
48
+ response = Net::HTTP.get_response(site_url, uri_method + uri_string)
49
+ if response.code == "200"
50
+ xml_data = response.body
51
+ data = XmlSimple.xml_in(xml_data)
52
+ result = data["translation"][0]
53
+ else
54
+ puts response.body
55
+ raise StandardError, response.body
56
+ end
57
+ rescue UnSupportedLanguage
58
+ raise UnSupportedLanguage.new
59
+ rescue => err_msg
60
+ puts "#{err_msg}"
61
+ end
62
+ end
63
+
64
+ end # Class
65
+
66
+ end # Vizi module
@@ -6,63 +6,79 @@
6
6
  # License:: MIT
7
7
 
8
8
  module Vizi
9
- require 'net/http'
10
- require 'uri'
11
- require 'xmlsimple'
12
-
13
- class UnSupportedLanguage < RuntimeError
14
- attr :msg
15
- def initialize(message='')
16
- @msg = "Language pair not supported yet."
17
- end
18
- end
9
+ require 'net/http'
10
+ require 'uri'
11
+ require 'xmlsimple'
12
+
13
+ class UnSupportedLanguage < RuntimeError
14
+ attr :msg
15
+ def initialize(message='')
16
+ @msg = "Language pair not supported yet."
17
+ end
18
+ end
19
19
 
20
20
  # This class includes a set of methods to translate using the Frengly service
21
- class Translator
22
- SUPPORTED_LANG_CODES = ['fr','de','es','pt','it','nl','tl','fi','el','iw','pl','ru','sv']
23
- SUPPORTED_LANG_LIST = {"English" => "en", "French" => "fr", "German" => "de", "Spanish" => "es",
24
- "Portuguese" => "pt", "Italian" => "it", "Filipino" => "tl","Finnish" => "fi","Greek" => "el",
25
- "Hebrew" => "iw", "Polish" => "pl", "Russian" => "ru", "Swedish" => "sv"}
21
+ class Translator
22
+ SUPPORTED_LANG_CODES = ['fr','de','es','pt','it','nl','tl','fi','el','iw','pl','ru','sv']
23
+ SUPPORTED_LANG_LIST = {"English" => "en", "French" => "fr", "German" => "de", "Spanish" => "es",
24
+ "Portuguese" => "pt", "Italian" => "it", "Filipino" => "tl","Finnish" => "fi","Greek" => "el",
25
+ "Hebrew" => "iw", "Polish" => "pl", "Russian" => "ru", "Swedish" => "sv"}
26
+
27
+ # method to get array of language codes
28
+ def getcodes
29
+ result = SUPPORTED_LANG_CODES
30
+ end
26
31
 
27
- # method to get array of language codes
28
- def getcodes
29
- result = SUPPORTED_LANG_CODES
30
- end
32
+ # method to get hash list of languages
33
+ def getlist
34
+ result = SUPPORTED_LANG_LIST
35
+ end
31
36
 
32
- # method to get hash list of languages
33
- def getlist
34
- result = SUPPORTED_LANG_LIST
35
- end
37
+ # method to translate from one language to another
38
+ def gettext(username, password, input_text, output_code)
39
+ default_src = 'en'
40
+ default_action = 'translateREST'
41
+ new_text = ''
42
+ new_text = input_text
43
+ new_text.gsub!("(","<(")
44
+ new_text.gsub!(")",")>")
45
+ new_array = new_text.split(/[<>]/)
46
+ chash = Hash.new
47
+ for i in 0..new_array.length-1
48
+ if new_array[i][0..0] == "("
49
+ old_value = new_array[i]
50
+ new_array[i] = "!"+i.to_s+"!"
51
+ chash[new_array[i]] = old_value
52
+ end
53
+ end
54
+ input_text = new_array.join
55
+ input_text = URI.escape(input_text)
56
+ p input_text
57
+ begin
58
+ raise UnSupportedLanguage unless SUPPORTED_LANG_CODES.include?(output_code)
59
+ site_url = 'www.syslang.com'
60
+ uri_method = '/frengly/controller?'
61
+ uri_string = 'action='+default_action+'&src='+default_src+'&dest='+output_code+'&text='+input_text+'&username='+username+'&password='+password
62
+ response = Net::HTTP.get_response(site_url, uri_method + uri_string)
63
+ if response.code == "200"
64
+ xml_data = response.body
65
+ data = XmlSimple.xml_in(xml_data)
66
+ result = data["translation"][0]
67
+ chash.each {|key, value| result.gsub!(key, value) }
68
+ result
69
+ else
70
+ puts response.body
71
+ raise StandardError, response.body
72
+ end
73
+ rescue UnSupportedLanguage
74
+ raise UnSupportedLanguage.new
75
+ rescue => err_msg
76
+ puts "#{err_msg}"
77
+ end
78
+ end
36
79
 
37
- # method to translate from one language to another
38
- def gettext(username, password, input_text, output_code)
39
- default_src = 'en'
40
- default_action = 'translateREST'
41
-
42
- input_text = URI.escape(input_text)
43
- begin
44
- raise UnSupportedLanguage unless SUPPORTED_LANG_CODES.include?(output_code)
45
- site_url = 'www.syslang.com'
46
- uri_method = '/frengly/controller?'
47
- uri_string = 'action='+default_action+'&src='+default_src+'&dest='+output_code+'&text='+input_text+'&username='+username+'&password='+password
48
- response = Net::HTTP.get_response(site_url, uri_method + uri_string)
49
- if response.code == "200"
50
- xml_data = response.body
51
- data = XmlSimple.xml_in(xml_data)
52
- result = data["translation"][0]
53
- else
54
- puts response.body
55
- raise StandardError, response.body
56
- end
57
- rescue UnSupportedLanguage
58
- raise UnSupportedLanguage.new
59
- rescue => err_msg
60
- puts "#{err_msg}"
61
- end
62
- end
63
-
64
- end
65
-
66
- end
80
+ end # Class
81
+
82
+ end # Vizi module
67
83
 
68
84
 
@@ -1,5 +1,4 @@
1
- # encoding: utf-8
2
- module Vizi # :doc:
1
+ # module Vizi # :doc:
3
2
  require 'vizi/vizi_translator'
4
- ## load 'c:\ramaze\viziapp\lib\vizi\vizi_translator.rb'
5
- end
3
+ ## load './lib/vizi/vizi_translator.rb'
4
+ # end