translate-rails3 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,7 +11,9 @@ To get the translation UI to write the YAML files in UTF8 you need to install th
11
11
 
12
12
  The translation UI finds all I18n keys by extracting them from I18n lookups in your application source code. In addition it adds all :en and default locale keys from the I18n backend.
13
13
 
14
- Each string in the UI has an "Auto Translate" link which will send the original text to Google Translate and will input the returned translation into the form field for further clean up and review prior to saving.
14
+ Strings in the UI can have an "Auto Translate" link (if configured, see below),
15
+ which will send the original text to translation API and will input the returned
16
+ translation into the form field for further clean up and review prior to saving.
15
17
 
16
18
 
17
19
  Rake Tasks
@@ -31,6 +33,7 @@ The missing task shows you any I18n keys in your code that do not have translati
31
33
  The merge_keys task is supposed to be used in conjunction with Sven Fuch's Rails I18n TextMate bundle (http://github.com/svenfuchs/rails-i18n/tree/master). Texts and keys extracted with the TextMate bundle end up in the temporary file log/translations.yml. When you run the merge_keys rake task the keys are moved over to the corresponding I18n locale file, i.e. config/locales/sv.yml. The merge_keys task also checks for overwrites of existing keys by warning you that one of your extracted keys already exists with a different translation.
32
34
 
33
35
  The google task is used for auto translating from one locale to another using Google Translate.
36
+ * Note: this task is currently broken, as Google is now charging for the Google Translate service.
34
37
 
35
38
  The changed rake task can show you between one YAML file to another which keys have had their texts changed.
36
39
 
@@ -53,6 +56,16 @@ Configuration
53
56
 
54
57
  Where [:en] and [:ja, :es, :fr] could be replaced by locale list of your choice.
55
58
 
59
+ (Optional) You can bring back "Auto Translate" support by specifying Bing AppId or
60
+ Google API Key in config/initializers/translate.rb with:
61
+
62
+ Translate.app_id = 'myappid'
63
+
64
+ or
65
+
66
+ Translate.api_key = 'mysecretkey'
67
+
68
+
56
69
  Dependencies
57
70
  ------------
58
71
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -3,12 +3,12 @@ module TranslateHelper
3
3
  def render_translate_form(from_locale, to_locale, key)
4
4
  from_text = lookup(from_locale, key)
5
5
  if from_text.is_a?(String)
6
- render :partial => 'string_form', :locals =>
6
+ render :partial => 'string_form', :locals =>
7
7
  {:from_locale => from_locale,
8
8
  :to_locale => to_locale,
9
9
  :key => key}
10
10
  elsif from_text.is_a?(Array)
11
- render :partial => 'array_form', :locals =>
11
+ render :partial => 'array_form', :locals =>
12
12
  {:from_locale => from_locale,
13
13
  :to_locale => to_locale,
14
14
  :key => key}
@@ -47,7 +47,7 @@ module TranslateHelper
47
47
  filter << link_to(label, link_params)
48
48
  end
49
49
  end
50
- filter.join(" | ")
50
+ filter.join(" | ")
51
51
  end
52
52
 
53
53
  def n_lines(text, line_size)
@@ -60,17 +60,28 @@ module TranslateHelper
60
60
  end
61
61
  n_lines
62
62
  end
63
-
63
+
64
64
  def translate_javascript_includes
65
65
  sources = []
66
66
  if File.exists?(File.join(Rails.root, "public", "javascripts", "prototype.js"))
67
67
  sources << "/javascripts/prototype.js"
68
68
  else
69
- sources << "http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"
69
+ sources << "http://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"
70
70
  end
71
- sources << "http://www.google.com/jsapi"
72
71
  sources.map do |src|
73
72
  %Q{<script src="#{src}" type="text/javascript"></script>}
74
- end.join("\n")
73
+ end.join("\n").html_safe
74
+ end
75
+
76
+ def translate_link(key, text, from, to)
77
+ method = if Translate.app_id
78
+ 'getBingTranslation'
79
+ elsif Translate.api_key
80
+ 'getGoogleTranslation'
81
+ else
82
+ nil
83
+ end
84
+ return nil unless method
85
+ link_to_function 'Auto Translate', "#{method}('#{key}', \"#{escape_javascript(text)}\", '#{from}', '#{to}')", :style => 'padding: 0; margin: 0;'
75
86
  end
76
87
  end
@@ -5,21 +5,52 @@
5
5
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
6
6
  <title><%= h(@page_title) %></title>
7
7
 
8
- <%= raw translate_javascript_includes %>
8
+ <%= translate_javascript_includes %>
9
9
  <script type="text/javascript">
10
- google.load("language", "1");
10
+ var source_ids = [];
11
11
 
12
- function getGoogleTranslation(id, text, from_language, to_language) {
13
- text = text.replace(/\{\{/, '__').replace(/\}\}/, '__')
14
- google.language.translate(text, from_language, to_language, function(result) {
15
- if (!result.error) {
16
- result_text = result.translation.unescapeHTML().gsub(/__(.+)__/, function(match){
12
+ function googleCallback(response) {
13
+ if (response.error) {
14
+ alert(response.error.message);
15
+ return;
16
+ }
17
+ var result_text = response.data.translations[0].translatedText.gsub(/__(.+)__/, function(match) {
18
+ return '{{' + match[1] + '}}';
19
+ });
20
+ var id = source_ids.shift();
21
+ if (id) {
22
+ Form.Element.setValue(id, result_text);
23
+ }
24
+ }
25
+
26
+ function getGoogleTranslation(id, text, from_language, to_language) {
27
+ source_ids.push(id);
28
+ text = text.replace(/\{\{/, '__').replace(/\}\}/, '__');
29
+ var s = document.createElement('script'), api_key = '<%= Translate.api_key %>';
30
+ s.type = 'text/javascript';
31
+ s.src = 'https://www.googleapis.com/language/translate/v2?key=' + api_key + '&source=' +
32
+ from_language + '&target=' + to_language + '&callback=googleCallback&q=' + text;
33
+ document.getElementsByTagName("head")[0].appendChild(s);
34
+ }
35
+
36
+ function bingCallback(text) {
37
+ var id = source_ids.shift();
38
+ if (text && id) {
39
+ var result_text = text.gsub(/__(.+)__/, function(match) {
17
40
  return '{{' + match[1] + '}}';
18
41
  });
19
42
  Form.Element.setValue(id, result_text);
20
- }
21
- });
43
+ }
44
+ }
22
45
 
46
+ function getBingTranslation(id, text, from_language, to_language) {
47
+ source_ids.push(id);
48
+ text = text.replace(/\{\{/, '__').replace(/\}\}/, '__');
49
+ var s = document.createElement("script"), app_id = '<%= Translate.app_id %>';
50
+ s.type = 'text/javascript';
51
+ s.src = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate?oncomplete=bingCallback&appId=' +
52
+ app_id + '&from=' + from_language + '&to=' + to_language + '&text=' + text;
53
+ document.getElementsByTagName("head")[0].appendChild(s);
23
54
  }
24
55
 
25
56
  /*
@@ -24,7 +24,7 @@
24
24
  <% else %>
25
25
  <%= text_field_tag("#{field_name}[#{index}]", to_text[index], :size => line_size, :id => "#{key}[#{index}]") %>
26
26
  <% end %>
27
- <%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}[#{index}]', \"#{escape_javascript(from_text_section)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
27
+ <%= translate_link("#{key}[#{index}]", from_text_section, @from_locale, @to_locale) %>
28
28
  </p>
29
29
  <% end %>
30
30
  </ol>
@@ -21,7 +21,7 @@
21
21
  </p>
22
22
  <p>
23
23
  <em>
24
- <%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}', \"#{escape_javascript(from_text)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
24
+ <%= translate_link(key, from_text, @from_locale, @to_locale) %>
25
25
  <br/>
26
26
  <strong>Key:</strong><%=h key %><br/>
27
27
  <% if @files[key] %>
@@ -1,6 +1,15 @@
1
+ require 'ya2yaml'
2
+
1
3
  module Translate
2
4
  class Engine < Rails::Engine
3
5
  end if defined?(Rails) && Rails::VERSION::MAJOR == 3
6
+
7
+ class << self
8
+ # For configuring Google Translate API key
9
+ attr_accessor :api_key
10
+ # For configuring Bing Application id
11
+ attr_accessor :app_id
12
+ end
4
13
  end
5
14
 
6
15
  Dir[File.join(File.dirname(__FILE__), "translate", "*.rb")].each do |file|
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "translate-rails3"
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Peter Marklund", "Milan Novota", "Roman Shterenzon"]
12
- s.date = "2011-11-23"
12
+ s.date = "2011-12-16"
13
13
  s.description = "This plugin provides a web interface for translating Rails I18n texts\n(requires Rails 3.0 or higher) from one locale to another.\nThe plugin has been tested only with the simple I18n backend that ships\nwith Rails.\nI18n texts are read from and written to YAML files under config/locales.\n\nThis gem is a fork of the original https://github.com/mynewsdesk/translate\nand also includes work from this fork: https://github.com/milann/translate\n"
14
14
  s.email = "romanbsd@yahoo.com"
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: translate-rails3
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Peter Marklund
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2011-11-23 00:00:00 Z
15
+ date: 2011-12-16 00:00:00 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: ya2yaml