exvo_globalize 0.3.1 → 0.3.2

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.
@@ -8,17 +8,40 @@ class GlobalizeTranslationsController < ApplicationController
8
8
  respond_to :html, :json
9
9
 
10
10
  def show
11
- # returns {"default_locale":"en","pl":{"hello.world":"Witaj \u015bwiecie","hello.earth":"Witaj ziemio"},"en":{"hello.world":"Hello world","hello.earth":"Hello Earth"}}
12
11
  @translations = I18n.backend.available_app_translations.merge({ :default_locale => I18n.default_locale })
13
12
  respond_with @translations
14
13
  end
15
14
 
15
+ def list
16
+ @languages = I18n.backend.available_locales.sort_by(&:to_s)
17
+
18
+ hash = case params[:type]
19
+ when "yaml-app"
20
+ I18n.backend.available_app_translations
21
+ when "yaml"
22
+ I18n.backend.simple.available_translations
23
+ when "db"
24
+ I18n.backend.globalize_store.available_translations
25
+ else
26
+ I18n.backend.available_translations
27
+ end
28
+
29
+ @translations = params[:locale].present? ? { params[:locale].to_sym => hash[params[:locale].to_sym] } : hash
30
+ end
31
+
16
32
  def update
17
- if I18n.backend.store_nested_translations(@globalize_app.fetch_translations)
18
- flash.now[:notice] = 'Translations updated'
33
+ translations = @globalize_app.fetch_translations
34
+
35
+ if translations.present? and !translations.has_key?("errors")
36
+ if I18n.backend.store_nested_translations(translations)
37
+ flash.now[:notice] = 'Translations updated'
38
+ else
39
+ flash.now[:alert] = 'There was a problem while updating translations'
40
+ end
19
41
  else
20
- flash.now[:alert] = 'There was a problem while updating translations'
42
+ flash.now[:alert] = "There was a problem while fetching translations: #{translations["errors"].to_s.downcase}"
21
43
  end
44
+
22
45
  render :show
23
46
  end
24
47
 
@@ -2,9 +2,7 @@ class GlobalizeTranslation < ActiveRecord::Base
2
2
 
3
3
  # based on https://github.com/svenfuchs/i18n-active_record/blob/master/lib/i18n/backend/active_record/translation.rb
4
4
 
5
- def self.locale(locale)
6
- scoped(:conditions => { :locale => locale.to_s })
7
- end
5
+ scope :ordered, :order => :key
8
6
 
9
7
  def self.lookup(keys)
10
8
  column_name = connection.quote_column_name('key')
@@ -0,0 +1,11 @@
1
+ = form_tag list_globalize_translations_path, :class => "search", :method => :get do
2
+ %p
3
+ I18n backend:
4
+ = select_tag "type", options_for_select([ ["Globalize store (database)", "db"], ["YAML (application)", "yaml-app"], ["YAML (application and gems)", "yaml"] ], :selected => params[:type]), :include_blank => "All"
5
+
6
+ Language:
7
+ = select_tag "locale", options_for_select(@languages, :selected => params[:locale]), :include_blank => "All"
8
+
9
+ = submit_tag "show"
10
+
11
+ = raw(HashToHTML(@translations)) if @translations.present?
@@ -4,6 +4,10 @@
4
4
  %title= "Exvo Globalize translations"
5
5
  %meta{ :name => "robots", :content => "noindex, nofollow" }
6
6
  :css
7
+ html, body, fieldset, form, input, select, textarea {
8
+ font: 13px/130% "Lucida Grande", Tahoma, Verdana, Helvetica, Arial, sans-serif;
9
+ }
10
+
7
11
  p.request_host {
8
12
  background: #f9fdf1;
9
13
  border: 1px solid #d2e1b8;
@@ -22,7 +26,43 @@
22
26
  margin: 0.2em auto;
23
27
  }
24
28
 
29
+ form p {
30
+ background: #f9fdf1;
31
+ border: 1px solid #d2e1b8;
32
+ margin: 1em 0;
33
+ padding: 1em 1.2em;
34
+ width: 600px;
35
+ }
36
+
37
+ select {
38
+ margin-right: 12px;
39
+ }
40
+
41
+ ul {
42
+ margin-bottom: 1em;
43
+ padding-left: 1em;
44
+ }
45
+
46
+ li {
47
+ color: #eee;
48
+ list-style: disc;
49
+ }
50
+
51
+ li strong {
52
+ color: #509e43;
53
+ margin-right: 0.2em;
54
+ }
55
+
56
+ li span {
57
+ color: black;
58
+ }
59
+
25
60
  %body
61
+ %p
62
+ = link_to 'Main page', globalize_translations_path
63
+ |
64
+ = link_to 'Translations list', list_globalize_translations_path
65
+
26
66
  %p.request_host
27
67
  Request host:
28
68
  %span= @globalize_app.request_host
data/config/routes.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  Rails.application.routes.draw do
2
2
  scope '/globalize' do
3
3
  resource :translations, :only => [:show, :update], :as => 'globalize_translations', :controller => 'globalize_translations' do
4
+ get :list, :on => :collection
4
5
  end
5
6
  end
6
7
  end
@@ -7,7 +7,7 @@ module I18n
7
7
  # returning a combined hash with translations from all chained backends
8
8
  def available_translations
9
9
  # reverse, so that the translations from the first backend (GlobalizeStore) overwrite/overshadow others
10
- @available_translations ||= backends.map { |backend| backend.available_translations }.reverse.inject(&:merge)
10
+ @available_translations ||= backends.map { |backend| backend.available_translations }.reverse.inject(&:deep_merge)
11
11
  end
12
12
 
13
13
  # Return a hash only with Application translations
@@ -9,9 +9,9 @@ module I18n
9
9
  hash.map do |main_key, main_value|
10
10
  main_key.to_s.split(".").reverse.inject(main_value) do |value, key|
11
11
  if value.is_a?(Hash)
12
- { key.to_s => nest_translations(value) }
12
+ { key.to_sym => nest_translations(value) }
13
13
  else
14
- { key.to_s => value }
14
+ { key.to_sym => value }
15
15
  end
16
16
  end
17
17
  end.inject(&:deep_merge)
@@ -13,15 +13,14 @@ module I18n
13
13
  GlobalizeTranslation.available_locales
14
14
  end
15
15
 
16
- # returns a Hash with all translations (translation keys are dot separated strings, not hashes):
17
- # {:en=>{"hello.world"=>"Hello world", "hello.earth"=>"Hello Earth"}, :pl=>{"hello.world"=>"Witaj świecie", "hello.earth"=>"Witaj ziemio"}}
16
+ # returns a nested Hash with all translations
17
+ # { :en => { :hello => { :world => "Hello world", :earth => "Hello Earth" } } } ...
18
18
  def available_translations(locale = nil)
19
- (locale.present? ? GlobalizeTranslation.locale(locale.to_s) : GlobalizeTranslation.all).
20
- inject({}) do |result, element|
21
- result[element.locale.to_sym] ||= {}
22
- result[element.locale.to_sym][element.key] = element.value
23
- result
24
- end
19
+ translations = locale.present? ? GlobalizeTranslation.where(:locale => locale).ordered : GlobalizeTranslation.ordered
20
+
21
+ translations.inject({}) do |result, element|
22
+ result.deep_merge( { element["locale"].to_sym => nest_translations(element["key"] => element["value"]) } )
23
+ end
25
24
  end
26
25
 
27
26
  def store_translations(locale, data, options = {})
@@ -32,7 +31,7 @@ module I18n
32
31
  end
33
32
 
34
33
  def store_flatten_translation(locale, key, value)
35
- GlobalizeTranslation.locale(locale).lookup(expand_keys(key)).delete_all
34
+ GlobalizeTranslation.where(:locale => locale).lookup(expand_keys(key)).delete_all
36
35
  GlobalizeTranslation.create(:locale => locale.to_s, :key => key.to_s, :value => value)
37
36
  end
38
37
 
@@ -40,7 +39,7 @@ module I18n
40
39
 
41
40
  def lookup(locale, key, scope = [], options = {})
42
41
  key = normalize_flat_keys(locale, key, scope, options[:separator])
43
- result = GlobalizeTranslation.locale(locale).lookup(key).all
42
+ result = GlobalizeTranslation.where(:locale => locale).lookup(key).all
44
43
 
45
44
  if result.empty?
46
45
  nil
@@ -20,5 +20,11 @@ module ExvoGlobalize
20
20
  # GlobalizeStore first (database backed), YAML files second (Simple backend, the default one)
21
21
  I18n.backend = I18n::Backend::Chain.new(I18n::Backend::GlobalizeStore.new, I18n.backend)
22
22
  end
23
+
24
+ initializer "exvo_globalize.configure_helpers" do |app|
25
+ ActiveSupport.on_load(:action_view) do
26
+ ActionView::Base.send :include, ExvoGlobalize::Helpers
27
+ end
28
+ end
23
29
  end
24
30
  end
@@ -24,7 +24,7 @@ class GlobalizeApp
24
24
  private
25
25
 
26
26
  def globalize_host
27
- Rails.env.production? ? "globalize.exvo.com" : "globalize.exvo.co"
27
+ Rails.env.production? or ENV['GLOBALIZE_USE_PRODUCTION_SERVER'] == 'true' ? "globalize.exvo.com" : "globalize.exvo.co"
28
28
  end
29
29
 
30
30
  def translations_uri
@@ -0,0 +1,32 @@
1
+ module ExvoGlobalize
2
+ module Helpers
3
+
4
+ # Prints nested Hash as a nested <ul> and <li> tags
5
+ # - keys are wrapped in <strong> tags
6
+ # - values are wrapped in <span> tags
7
+ def HashToHTML(hash, opts = {})
8
+ return if !hash.is_a?(Hash)
9
+
10
+ indent_level = opts.fetch(:indent_level) { 0 }
11
+
12
+ out = " " * indent_level + "<ul>\n"
13
+
14
+ hash.each do |key, value|
15
+ out += " " * (indent_level + 2) + "<li><strong>#{key}:</strong>"
16
+
17
+ if value.is_a?(Hash)
18
+ out += "\n" + HashToHTML(value, :indent_level => indent_level + 2) + " " * (indent_level + 2) + "</li>\n"
19
+ elsif value.is_a?(Array)
20
+ out += " <span>[ #{value.join(', ')} ]</span></li>\n"
21
+ elsif value.is_a?(Proc)
22
+ out += " <span>" + CGI::escapeHTML(value.to_s) + "</span></li>\n"
23
+ else
24
+ out += " <span>#{value}</span></li>\n"
25
+ end
26
+ end
27
+
28
+ out += " " * indent_level + "</ul>\n"
29
+ end
30
+
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module ExvoGlobalize
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -5,6 +5,7 @@ require 'exvo_globalize/backend/flatten'
5
5
  require "exvo_globalize/backend/globalize_store"
6
6
  require "exvo_globalize/backend/simple"
7
7
  require 'exvo_globalize/globalize_app'
8
+ require 'exvo_globalize/helpers'
8
9
  require 'haml'
9
10
  require 'httparty'
10
11
  require 'awesome_print'
data/lib/tasks/dump.rake CHANGED
@@ -2,16 +2,12 @@ namespace :"globalize:translations:dump" do
2
2
  desc "Dump GlobalizeStore I18n translations as YAML"
3
3
  task :yaml => :environment do
4
4
  puts "Dumping translations as YAML..."
5
- backend = I18n.backend.globalize_store
6
- translations = backend.available_translations
7
- puts backend.nest_translations(translations).ya2yaml
5
+ puts I18n.backend.globalize_store.available_translations.ya2yaml
8
6
  end
9
7
 
10
8
  desc "Dump GlobalizeStore I18n translations as Ruby Hash"
11
9
  task :ruby => :environment do
12
10
  puts "Dumping translations as Ruby Hash..."
13
- backend = I18n.backend.globalize_store
14
- translations = backend.available_translations
15
- ap backend.nest_translations(translations), { :plain => true, :indent => 2, :sorted_hash_keys => true }
11
+ ap I18n.backend.globalize_store.available_translations, { :plain => true, :indent => 2, :sorted_hash_keys => true }
16
12
  end
17
13
  end
@@ -58,11 +58,7 @@ describe GlobalizeTranslationsController do
58
58
  let(:intro) { "Introduction" }
59
59
  let(:translations) { { :en => { :intro => { :title => intro } } } }
60
60
 
61
- before do
62
- controller.stub!(:require_admin).and_return(true)
63
- GlobalizeApp.any_instance.stub(:fetch_translations) { translations }
64
- put :update
65
- end
61
+ before { fetch_translations(translations) }
66
62
 
67
63
  it "updates the translations" do
68
64
  I18n.t(:title, :scope => :intro).should eq(intro)
@@ -77,6 +73,51 @@ describe GlobalizeTranslationsController do
77
73
  end
78
74
  end
79
75
 
76
+ describe "PUT :update with error from Globalize" do
77
+ let(:message) { "Not found" }
78
+ let(:translations) { { "errors" => message } }
79
+
80
+ before { fetch_translations(translations) }
81
+
82
+ it "sets a flash alert" do
83
+ flash[:alert].should_not be_empty
84
+ end
85
+
86
+ it "displays a flash alert" do
87
+ page.should have_selector('p.alert')
88
+ end
89
+ end
90
+
91
+ describe "GET :list" do
92
+ before do
93
+ @title = Factory(:i18n_title)
94
+ controller.stub!(:require_admin).and_return(true)
95
+
96
+ # invalidate #available_translations cache before each run
97
+ I18n.backend.instance_variable_set(:@available_translations, nil)
98
+ end
99
+
100
+ it "displays all available translations" do
101
+ get :list
102
+ page.should have_selector('li span', :text => @title.value)
103
+ page.should have_selector('li span', :text => 'YAML Nested Title')
104
+ end
105
+
106
+ it "displays available translations in the GlobalizeStore backend" do
107
+ get :list, { :type => 'db', :locale => 'en' }
108
+ page.should have_selector('li span', :text => @title.value)
109
+ page.should_not have_selector('li span', :text => 'YAML Nested Title')
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ private
116
+
117
+ def fetch_translations(translations_stub)
118
+ controller.stub!(:require_admin).and_return(true)
119
+ GlobalizeApp.any_instance.stub(:fetch_translations) { translations_stub }
120
+ put :update
80
121
  end
81
122
 
82
123
  end
@@ -5,18 +5,18 @@ describe ExvoGlobalize do
5
5
  let(:backend) { I18n.backend.globalize_store }
6
6
 
7
7
  it "should not change a normal hash" do
8
- hash = { 'hello' => "Hello", 'world' => "World" }
8
+ hash = { :hello => "Hello", :world => "World" }
9
9
  backend.nest_translations(hash).should eq(hash)
10
10
  end
11
11
 
12
12
  it "should not change a nested hash" do
13
- nested_hash = { 'en' => { 'hello' => "Hello World!", 'world' => "World!" } }
13
+ nested_hash = { :en => { :hello => "Hello World!", :world => "World!" } }
14
14
  backend.nest_translations(nested_hash).should eq(nested_hash)
15
15
  end
16
16
 
17
17
  it "should nest a simple flatten hash" do
18
18
  hash = { "hello.world" => "Hello World!" }
19
- nested_hash = { 'hello' => { 'world' => "Hello World!" } }
19
+ nested_hash = { :hello => { :world => "Hello World!" } }
20
20
  backend.nest_translations(hash).should eq(nested_hash)
21
21
  end
22
22
 
@@ -29,10 +29,10 @@ describe ExvoGlobalize do
29
29
  }
30
30
 
31
31
  nested_hash = {
32
- 'en' => {
33
- 'hello' => {
34
- 'world' => "Hello World!",
35
- 'earth' => "Hello Earth!"
32
+ :en => {
33
+ :hello => {
34
+ :world => "Hello World!",
35
+ :earth => "Hello Earth!"
36
36
  }
37
37
  }
38
38
  }
@@ -6,6 +6,9 @@ describe ExvoGlobalize do
6
6
  let(:i18n_nested_header) { Factory(:i18n_nested_header) }
7
7
  let(:i18n_title) { Factory(:i18n_title) }
8
8
 
9
+ # invalidate #available_translations cache before each run
10
+ before { I18n.backend.instance_variable_set(:@available_translations, nil) }
11
+
9
12
  it "respects the default_locale setting" do
10
13
  I18n.default_locale.should eq(:en)
11
14
  end
@@ -57,10 +60,22 @@ describe ExvoGlobalize do
57
60
  I18n.backend.simple.available_translations[:en][:yaml][:title].should eq('YAML Nested Title')
58
61
  end
59
62
 
63
+ it "lists available translations from the GlobalizeStore backend" do
64
+ i18n_nested_header.value.should eq(I18n.backend.globalize_store.available_translations[:en][:nested][:header])
65
+ end
66
+
60
67
  it "lists available app translations" do
61
68
  I18n.backend.available_app_translations[:en][:helpers][:select].has_key?(:prompt).should be_true
62
69
  end
63
70
 
71
+ it "lists all available translations" do
72
+ I18n.backend.available_translations[:en][:yaml][:title].should eql('YAML Nested Title')
73
+ end
74
+
75
+ it "lists all available translations prioritizing the GlobalizeStore backend" do
76
+ i18n_title.value.should eql(I18n.backend.available_translations[:en][:title])
77
+ end
78
+
64
79
  it "excludes fixtures from app_translations and does so without breaking the I18n.load_path" do
65
80
  I18n.backend.available_app_translations[:en].has_key?(:title).should be_false
66
81
  I18n.backend.available_translations[:en].has_key?(:title).should be_true
@@ -14,7 +14,7 @@ describe GlobalizeTranslation do
14
14
  end
15
15
 
16
16
  it "returns a translation for scoped locale" do
17
- i18n_title.value.should eq(GlobalizeTranslation.locale('en').lookup(:title).first.value)
17
+ i18n_title.value.should eq(GlobalizeTranslation.where(:locale => :en).lookup(:title).first.value)
18
18
  end
19
19
 
20
20
  it "lists available locales" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exvo_globalize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Pawe\xC5\x82 Go\xC5\x9Bcicki"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-29 00:00:00 +02:00
18
+ date: 2011-09-06 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -275,6 +275,7 @@ files:
275
275
  - app/controllers/globalize_translations_controller.rb
276
276
  - app/models/globalize_translation.rb
277
277
  - app/views/globalize_translations/_flash_messages.html.haml
278
+ - app/views/globalize_translations/list.html.haml
278
279
  - app/views/globalize_translations/show.html.haml
279
280
  - app/views/layouts/exvo_globalize.html.haml
280
281
  - config/locales/ar.yml
@@ -339,6 +340,7 @@ files:
339
340
  - lib/exvo_globalize/backend/simple.rb
340
341
  - lib/exvo_globalize/engine.rb
341
342
  - lib/exvo_globalize/globalize_app.rb
343
+ - lib/exvo_globalize/helpers.rb
342
344
  - lib/exvo_globalize/version.rb
343
345
  - lib/generators/exvo_globalize/exvo_globalize_generator.rb
344
346
  - lib/generators/exvo_globalize/templates/migration.rb