i18n_backend_database 0.0.1

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.
Files changed (67) hide show
  1. data/LICENSE +29 -0
  2. data/README.textile +125 -0
  3. data/data/locales.yml +4 -0
  4. data/generators/i18n_backend_database/i18n_backend_database_generator.rb +8 -0
  5. data/generators/i18n_backend_database/templates/migrate/create_i18n_tables.rb +25 -0
  6. data/init.rb +1 -0
  7. data/lib/controllers/locales_controller.rb +86 -0
  8. data/lib/controllers/translations_controller.rb +141 -0
  9. data/lib/ext/i18n.rb +68 -0
  10. data/lib/google_language.rb +33 -0
  11. data/lib/i18n_backend_database.rb +9 -0
  12. data/lib/i18n_backend_database/database.rb +263 -0
  13. data/lib/i18n_util.rb +148 -0
  14. data/lib/models/locale.rb +67 -0
  15. data/lib/models/translation.rb +46 -0
  16. data/lib/models/translation_option.rb +26 -0
  17. data/lib/public/images/custom1_bar.gif +0 -0
  18. data/lib/public/images/custom1_box.gif +0 -0
  19. data/lib/public/images/percentImage.png +0 -0
  20. data/lib/public/images/percentImage_back.png +0 -0
  21. data/lib/public/images/percentImage_back1.png +0 -0
  22. data/lib/public/images/percentImage_back2.png +0 -0
  23. data/lib/public/images/percentImage_back3.png +0 -0
  24. data/lib/public/images/percentImage_back4.png +0 -0
  25. data/lib/public/javascripts/jsProgressBarHandler.js +509 -0
  26. data/lib/routing.rb +15 -0
  27. data/lib/views/layouts/translations.html.erb +23 -0
  28. data/lib/views/locales/edit.html.erb +20 -0
  29. data/lib/views/locales/index.html.erb +22 -0
  30. data/lib/views/locales/new.html.erb +19 -0
  31. data/lib/views/locales/show.html.erb +14 -0
  32. data/lib/views/translations/asset_translations.html.erb +32 -0
  33. data/lib/views/translations/edit.html.erb +35 -0
  34. data/lib/views/translations/index.html.erb +27 -0
  35. data/lib/views/translations/new.html.erb +19 -0
  36. data/lib/views/translations/show.html.erb +32 -0
  37. data/lib/views/translations/translations.html.erb +28 -0
  38. data/lib/views/translations/update.rjs +7 -0
  39. data/routes.rb +3 -0
  40. data/spec/assets/public/es/favicons/favicon1.gif +0 -0
  41. data/spec/assets/public/es/images/icons/icon1.gif +0 -0
  42. data/spec/assets/public/es/images/image1.gif +0 -0
  43. data/spec/assets/public/favicons/favicon1.gif +0 -0
  44. data/spec/assets/public/favicons/favicon2.gif +0 -0
  45. data/spec/assets/public/images/icons/icon1.gif +0 -0
  46. data/spec/assets/public/images/icons/icon2.gif +0 -0
  47. data/spec/assets/public/images/image1.gif +0 -0
  48. data/spec/assets/public/images/image2.gif +0 -0
  49. data/spec/assets/public/images/promo/sfc08_140x400_3.gif +0 -0
  50. data/spec/assets/views/test_view1.html.erb +1 -0
  51. data/spec/assets/views/test_view2.html.erb +30 -0
  52. data/spec/caching_spec.rb +87 -0
  53. data/spec/controllers/locales_controller_spec.rb +173 -0
  54. data/spec/controllers/locales_routing_spec.rb +59 -0
  55. data/spec/controllers/translations_controller_spec.rb +189 -0
  56. data/spec/controllers/translations_routing_spec.rb +59 -0
  57. data/spec/database_spec.rb +199 -0
  58. data/spec/i18n_ext_spec.rb +40 -0
  59. data/spec/localize_spec.rb +66 -0
  60. data/spec/localize_text_spec.rb +76 -0
  61. data/spec/models/locale_spec.rb +111 -0
  62. data/spec/models/translation_spec.rb +44 -0
  63. data/spec/spec_helper.rb +16 -0
  64. data/spec/translate_asset_spec.rb +57 -0
  65. data/spec/translate_spec.rb +546 -0
  66. data/tasks/i18n.rake +60 -0
  67. metadata +155 -0
data/lib/routing.rb ADDED
@@ -0,0 +1,15 @@
1
+ module I18n
2
+ module BackendDatabase
3
+ module Routing
4
+ # Loads the set of routes from within a plugin and
5
+ # evaluates them at this point within an application’s
6
+ # main routes.rb file.
7
+ def from_plugin(name)
8
+ map = self # to make 'map' available within the plugin route file
9
+ plugin_root = File.join(RAILS_ROOT, 'vendor', 'plugins')
10
+ routes_path = File.join(plugin_root, name.to_s, 'routes.rb')
11
+ eval(IO.read(routes_path), binding, routes_path) if File.file?(routes_path)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7
+ <title>Translation Administration: <%= controller.action_name %></title>
8
+ <%= stylesheet_link_tag 'scaffold' %>
9
+ <%= javascript_include_tag :defaults %>
10
+ <%= javascript_include_tag 'jsProgressBarHandler.js' %>
11
+ </head>
12
+ <body>
13
+ <p>
14
+ <%= link_to 'Textual Translations', translations_url %> &middot; <%= link_to 'Asset Translations', asset_translations_url %>
15
+ </p>
16
+ <body class="content">
17
+ <div class="content">
18
+ <%= yield %>
19
+ </div>
20
+ </body>
21
+
22
+ </body>
23
+ </html>
@@ -0,0 +1,20 @@
1
+ <h1>Editing locale</h1>
2
+
3
+ <% form_for(@locale) do |f| %>
4
+ <%= f.error_messages %>
5
+
6
+ <p>
7
+ <%= f.label :code %><br />
8
+ <%= f.text_field :code %>
9
+ </p>
10
+ <p>
11
+ <%= f.label :name %><br />
12
+ <%= f.text_field :name %>
13
+ </p>
14
+ <p>
15
+ <%= f.submit "Update" %>
16
+ </p>
17
+ <% end %>
18
+
19
+ <%= link_to 'Show', @locale %> |
20
+ <%= link_to 'Back', locales_path %>
@@ -0,0 +1,22 @@
1
+ <h1>Listing locales</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th>Code</th>
6
+ <th>Name</th>
7
+ </tr>
8
+
9
+ <% for locale in @locales %>
10
+ <tr>
11
+ <td><%=h locale.code %></td>
12
+ <td><%=h locale.name %></td>
13
+ <td><%= link_to 'Show', locale %></td>
14
+ <td><%= link_to 'Edit', edit_locale_path(locale) %></td>
15
+ <td><%= link_to 'Destroy', locale, :confirm => 'Are you sure?', :method => :delete %></td>
16
+ </tr>
17
+ <% end %>
18
+ </table>
19
+
20
+ <br />
21
+
22
+ <%= link_to 'New locale', new_locale_path %>
@@ -0,0 +1,19 @@
1
+ <h1>New locale</h1>
2
+
3
+ <% form_for(@locale) do |f| %>
4
+ <%= f.error_messages %>
5
+
6
+ <p>
7
+ <%= f.label :code %><br />
8
+ <%= f.text_field :code %>
9
+ </p>
10
+ <p>
11
+ <%= f.label :name %><br />
12
+ <%= f.text_field :name %>
13
+ </p>
14
+ <p>
15
+ <%= f.submit "Create" %>
16
+ </p>
17
+ <% end %>
18
+
19
+ <%= link_to 'Back', locales_path %>
@@ -0,0 +1,14 @@
1
+ <p>
2
+ <b>Code:</b>
3
+ <%=h @locale.code %>
4
+ </p>
5
+
6
+ <p>
7
+ <b>Name:</b>
8
+ <%=h @locale.name %>
9
+ </p>
10
+
11
+
12
+ <%= link_to 'Edit', edit_locale_path(@locale) %> |
13
+ <%= link_to 'Translations', locale_translations_path(@locale) %> |
14
+ <%= link_to 'Back', locales_path %>
@@ -0,0 +1,32 @@
1
+ <% form_tag asset_translations_url do -%>
2
+ <%= select_tag :locale_id, options_from_collection_for_select(Locale.all, :code, :name, @locale.code) -%>
3
+ <%= select_tag :translation_option, options_from_collection_for_select(TranslationOption.all, :code, :description, @translation_option.code) -%>
4
+ <%= submit_tag 'Go' %>
5
+ <% end -%>
6
+
7
+ <script type="text/javascript">
8
+ Event.observe(window, 'load', function() {
9
+ myJsProgressBarHandler = new JS_BRAMUS.jsProgressBar($('translate_progress'), 0);
10
+ }, false);
11
+ </script>
12
+
13
+ <p>
14
+ Asset Translation progress: <span class="progressBar" id="translate_progress"><%= @percentage_translated %></span>
15
+ </p>
16
+
17
+ <h2><%= @translation_option.description %>: <%= @locale.name %></h2>
18
+
19
+ <% if @asset_translations.empty? -%>
20
+ <h3>No records for this criteria.</h3>
21
+ <% end -%>
22
+
23
+ <% for asset in @asset_translations %>
24
+ <p>
25
+ <%= image_tag(asset, :size => "50x50") %>
26
+ <%= link_to(asset, image_path(asset)) %>
27
+ <% if !@locale.default_locale? && @translation_option == TranslationOption.translated -%>
28
+ <%= image_tag(I18n.ta(asset, :locale => @locale.code), :size => "50x50") %>
29
+ <%= link_to(asset, image_path(I18n.ta(asset, :locale => @locale.code))) %>
30
+ <% end -%>
31
+ </p>
32
+ <% end %>
@@ -0,0 +1,35 @@
1
+ <h1>Editing translation for <%= @locale.code %></h1>
2
+
3
+ <p>
4
+ <b>Key:</b>
5
+ <%=h @translation.key %>
6
+ </p>
7
+
8
+ <p>
9
+ <b>Raw Key:</b>
10
+ <%=h @translation.raw_key %>
11
+ </p>
12
+
13
+ <p>
14
+ <b>Pluralization Index:</b>
15
+ <%=h @translation.pluralization_index %>
16
+ </p>
17
+
18
+ <p>
19
+ <b>Default Locale Value:</b>
20
+ <%=h @translation.default_locale_value %>
21
+ </p>
22
+
23
+ <% form_for([@locale, @translation]) do |f| %>
24
+ <%= f.error_messages %>
25
+
26
+ <%= f.label :value %><br />
27
+ <%= f.text_field :value %>
28
+ </p>
29
+ <p>
30
+ <%= f.submit "Update" %>
31
+ </p>
32
+ <% end %>
33
+
34
+ <%= link_to 'Show', locale_translation_path(@locale, @translation) %> |
35
+ <%= link_to 'Back', locale_translations_path %>
@@ -0,0 +1,27 @@
1
+ <h1>Listing translations for <%= @locale.code %></h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th>Key</th>
6
+ <th>Raw Key</th>
7
+ <th>Value</th>
8
+ <th>Default Locale Value</th>
9
+ </tr>
10
+
11
+ <% for translation in @translations %>
12
+ <tr>
13
+ <td><%=h translation.key %></td>
14
+ <td><%=h translation.raw_key %></td>
15
+ <td><%=h translation.value %></td>
16
+ <td><%=h translation.default_locale_value %></td>
17
+ <td><%= link_to 'Show', locale_translation_path(@locale, translation) %></td>
18
+ <td><%= link_to 'Edit', edit_locale_translation_path(@locale, translation) %></td>
19
+ <td><%= link_to 'Destroy', locale_translation_path(@locale, translation), :confirm => 'Are you sure?', :method => :delete %></td>
20
+ </tr>
21
+ <% end %>
22
+ </table>
23
+
24
+ <br />
25
+
26
+ <%= link_to 'New translation', new_locale_translation_path %> |
27
+ <%= link_to 'Back', locales_path %>
@@ -0,0 +1,19 @@
1
+ <h1>New translation</h1>
2
+
3
+ <% form_for([@locale, @translation]) do |f| %>
4
+ <%= f.error_messages %>
5
+
6
+ <p>
7
+ <%= f.label :key %><br />
8
+ <%= f.text_field :key %>
9
+ </p>
10
+ <p>
11
+ <%= f.label :value %><br />
12
+ <%= f.text_field :value %>
13
+ </p>
14
+ <p>
15
+ <%= f.submit "Create" %>
16
+ </p>
17
+ <% end %>
18
+
19
+ <%= link_to 'Back', locale_translations_path %>
@@ -0,0 +1,32 @@
1
+ <p>
2
+ <b>Locale:</b>
3
+ <%=h @locale.code %>
4
+ </p>
5
+
6
+ <p>
7
+ <b>Key:</b>
8
+ <%=h @translation.key %>
9
+ </p>
10
+
11
+ <p>
12
+ <b>Raw Key:</b>
13
+ <%=h @translation.raw_key %>
14
+ </p>
15
+
16
+ <p>
17
+ <b>Pluralization Index:</b>
18
+ <%=h @translation.pluralization_index %>
19
+ </p>
20
+
21
+ <p>
22
+ <b>Default Locale Value:</b>
23
+ <%=h @translation.default_locale_value %>
24
+ </p>
25
+
26
+ <p>
27
+ <b>Value:</b>
28
+ <%=h @translation.value %>
29
+ </p>
30
+
31
+ <%= link_to 'Edit', edit_locale_translation_path(@locale, @translation) %> |
32
+ <%= link_to 'Back', locale_translations_path %>
@@ -0,0 +1,28 @@
1
+ <% form_tag translations_url do -%>
2
+ <%= select_tag :locale_id, options_from_collection_for_select(Locale.all, :code, :name, @locale.code) -%>
3
+ <%= select_tag :translation_option, options_from_collection_for_select(TranslationOption.all, :code, :description, @translation_option.code) -%>
4
+ <%= submit_tag 'Go' %>
5
+ <% end -%>
6
+
7
+ <p>
8
+ Textual Translation progress: <span class="progressBar" id="translate_progress"><%= @locale.percentage_translated %></span>
9
+ </p>
10
+
11
+ <h2><%= @translation_option.description %>: <%= @locale.name %></h2>
12
+
13
+ <% if @translations.empty? -%>
14
+ <h3>No records for this criteria.</h3>
15
+ <% end -%>
16
+
17
+ <% for translation in @translations %>
18
+ <div id="<%= "translation_#{translation.class}_#{translation.id}" %>">
19
+ <% remote_form_for([@locale, translation]) do |f| %>
20
+ <p><%=h translation.default_locale_value || translation.raw_key %></p>
21
+ <p>
22
+ <%= f.text_field :value, :size => 128 %>
23
+ <%= f.submit "Update" %>
24
+ </p>
25
+ <% end %>
26
+ <hr/>
27
+ </div>
28
+ <% end %>
@@ -0,0 +1,7 @@
1
+ # update the percentage and remove the translated record if translated for first time otherwise highlight change
2
+ if @first_time_translating
3
+ page.call 'myJsProgressBarHandler.setPercentage', 'translate_progress', @locale.percentage_translated
4
+ page.remove("translation_#{@translation.class}_#{@translation.id}")
5
+ else
6
+ page.visual_effect :highlight, "translation_#{@translation.class}_#{@translation.id}"
7
+ end
data/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ map.resources :locales, :has_many => :translations
2
+ map.translations '/translations', :controller => 'translations', :action => 'translations'
3
+ map.asset_translations '/asset_translations', :controller => 'translations', :action => 'asset_translations'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ <div class="promo-ad"><%= link_to (translated_image_tag(I18n.ta("promo/sfc08_140x400_3.gif")), "http://streak.espn.go.com/") %></div>
@@ -0,0 +1,30 @@
1
+ <h1>Listing people</h1>
2
+ <%= image_tag(I18n.ta('image1.gif')) %>
3
+ <%= image_tag(I18n.ta('image2.gif')) %>
4
+ <%= image_tag(I18n.ta('image2.gif')) %> <!-- test duplicate tag ... image should only appear once -->
5
+ <%= image_tag(I18n.ta('icons/icon1.gif')) %>
6
+ <%= image_tag(I18n.ta('icons/icon2.gif')) %>
7
+ <%= image_tag(I18n.ta('/favicons/favicon1.gif')) image_tag(I18n.ta('/favicons/favicon2.gif')) %> <!-- should find tags on same line -->
8
+
9
+ <table>
10
+ <tr>
11
+ <th>Name</th>
12
+ <th>City</th>
13
+ <th>Age</th>
14
+ </tr>
15
+
16
+ <% for person in @people %>
17
+ <tr>
18
+ <td><%=h person.name %></td>
19
+ <td><%=h person.city %></td>
20
+ <td><%=h person.age %></td>
21
+ <td><%= link_to 'Show', person %></td>
22
+ <td><%= link_to 'Edit', edit_person_path(person) %></td>
23
+ <td><%= link_to 'Destroy', person, :confirm => 'Are you sure?', :method => :delete %></td>
24
+ </tr>
25
+ <% end %>
26
+ </table>
27
+
28
+ <br />
29
+
30
+ <%= link_to 'New person', new_person_path(:locale => I18n.locale) %>
@@ -0,0 +1,87 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe I18n::Backend::Database do
4
+ before(:each) do
5
+ @backend = I18n::Backend::Database.new
6
+ I18n.backend = @backend
7
+ end
8
+
9
+ after(:each) do
10
+ @backend.cache_store.clear
11
+ end
12
+
13
+ describe "with default locale en" do
14
+ before(:each) do
15
+ I18n.default_locale = "en"
16
+ @english_locale = Locale.create!(:code => "en")
17
+ end
18
+
19
+ describe "and locale en" do
20
+ before(:each) do
21
+ I18n.locale = "en"
22
+ end
23
+
24
+ it "should cache translations" do
25
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
26
+
27
+ options = {:attribute=>"Locale", :value=>nil,
28
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
29
+
30
+ @backend.translate("en", :"models.translation.attributes.locale.blank", options).should == "is blank moron!"
31
+
32
+ @backend.cache_store.exist?("en:#{Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")}").should be_true
33
+ @backend.cache_store.exist?("en:#{Translation.hk("activerecord.errors.models.translation.blank")}").should be_true
34
+ @backend.cache_store.exist?("en:#{Translation.hk("activerecord.errors.messages.blank")}").should be_true
35
+
36
+ @backend.cache_store.read("en:#{Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")}").should == nil
37
+ @backend.cache_store.read("en:#{Translation.hk("activerecord.errors.models.translation.blank")}").should == nil
38
+ @backend.cache_store.read("en:#{Translation.hk("activerecord.errors.messages.blank")}").should == "is blank moron!"
39
+ end
40
+
41
+ it "should update a cache record if the translation record changes" do
42
+ hash_key = Translation.hk("blah")
43
+ @backend.translate("en", "blah")
44
+ @backend.cache_store.read("en:#{hash_key}").should == "blah"
45
+
46
+ translation = @english_locale.translations.find_by_key(Translation.hk("blah"))
47
+ translation.value.should == "blah"
48
+
49
+ translation.update_attribute(:value, "foo")
50
+ translation.value.should == "foo"
51
+ @backend.cache_store.read("en:#{hash_key}").should == "foo"
52
+ end
53
+ end
54
+
55
+ describe "and locale es" do
56
+ before(:each) do
57
+ I18n.locale = "es"
58
+ @spanish_locale = Locale.create!(:code => 'es')
59
+ end
60
+
61
+ it "should cache translations" do
62
+ @english_locale.translations.create!(:key => 'activerecord.errors.messages.blank', :value => 'is blank moron!')
63
+
64
+ options = {:attribute=>"Locale", :value=>nil,
65
+ :scope=>[:activerecord, :errors], :default=>[:"models.translation.blank", :"messages.blank"], :model=>"Translation"}
66
+
67
+ @backend.translate("es", :"models.translation.attributes.locale.blank", options).should == "is blank moron!"
68
+
69
+ @backend.cache_store.exist?("es:#{Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")}").should be_true
70
+ @backend.cache_store.exist?("es:#{Translation.hk("activerecord.errors.models.translation.blank")}").should be_true
71
+ @backend.cache_store.exist?("es:#{Translation.hk("activerecord.errors.messages.blank")}").should be_true
72
+
73
+ @backend.cache_store.read("es:#{Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")}").should == nil
74
+ @backend.cache_store.read("es:#{Translation.hk("activerecord.errors.models.translation.blank")}").should == nil
75
+ @backend.cache_store.read("es:#{Translation.hk("activerecord.errors.messages.blank")}").should == "is blank moron!"
76
+
77
+ @backend.cache_store.exist?("en:#{Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")}").should be_true
78
+ @backend.cache_store.exist?("en:#{Translation.hk("activerecord.errors.models.translation.blank")}").should be_true
79
+ @backend.cache_store.exist?("en:#{Translation.hk("activerecord.errors.messages.blank")}").should be_true
80
+
81
+ @backend.cache_store.read("en:#{Translation.hk("activerecord.errors.models.translation.attributes.locale.blank")}").should == nil
82
+ @backend.cache_store.read("en:#{Translation.hk("activerecord.errors.models.translation.blank")}").should == nil
83
+ @backend.cache_store.read("en:#{Translation.hk("activerecord.errors.messages.blank")}").should == "is blank moron!"
84
+ end
85
+ end
86
+ end
87
+ end