lolita-i18n 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +29 -0
  2. data/Gemfile +11 -25
  3. data/History.rdoc +7 -0
  4. data/README.md +1 -1
  5. data/Rakefile +4 -46
  6. data/app/assets/javascripts/lolita/i18n/application.js +1 -1
  7. data/app/assets/javascripts/lolita/i18n/i18n.js +87 -0
  8. data/app/assets/stylesheets/lolita/i18n/application.scss +8 -7
  9. data/app/controllers/lolita/i18n_controller.rb +27 -9
  10. data/app/helpers/lolita/i18n_helper.rb +40 -7
  11. data/app/views/lolita/i18n/index.html.haml +23 -14
  12. data/config/locales/en.yml +7 -2
  13. data/config/locales/lv.yml +7 -2
  14. data/config/routes.rb +2 -10
  15. data/lib/lolita-i18n.rb +11 -77
  16. data/lib/lolita-i18n/configuration.rb +56 -0
  17. data/lib/lolita-i18n/exceptions.rb +6 -0
  18. data/lib/lolita-i18n/rails.rb +0 -1
  19. data/lib/lolita-i18n/request.rb +219 -0
  20. data/lib/lolita-i18n/version.rb +11 -0
  21. data/lolita-i18n.gemspec +18 -96
  22. data/spec/controllers/lolita/i18n_controller_spec.rb +64 -30
  23. data/spec/helpers/lolita/i18n_helper_spec.rb +70 -0
  24. data/spec/lolita-i18n/configuration_spec.rb +63 -0
  25. data/spec/lolita-i18n/exceptions_spec.rb +15 -0
  26. data/spec/lolita-i18n/request_spec.rb +238 -0
  27. data/spec/lolita-i18n/version_spec.rb +8 -0
  28. data/spec/lolita_i18n_spec.rb +38 -0
  29. data/spec/rails_spec_helper.rb +11 -0
  30. data/spec/requests/translating_spec.rb +51 -0
  31. data/spec/requests/translation_list_spec.rb +36 -0
  32. data/spec/routing/routes_spec.rb +11 -0
  33. data/spec/spec_helper.rb +26 -18
  34. data/spec/{rails_app → test_app}/app/controllers/application_controller.rb +2 -2
  35. data/spec/test_app/config/application.rb +19 -0
  36. data/spec/test_app/config/boot.rb +11 -0
  37. data/spec/{rails_app → test_app}/config/enviroment.rb +4 -4
  38. data/spec/test_app/config/enviroments/test.rb +44 -0
  39. data/spec/test_app/config/initializers/lolita_i18n.rb +4 -0
  40. data/spec/test_app/config/initializers/token.rb +7 -0
  41. data/spec/test_app/config/locales/en.yml +10 -0
  42. data/spec/test_app/config/locales/lv.yml +2 -0
  43. data/spec/test_app/config/locales/ru.yml +2 -0
  44. data/spec/test_app/config/mongoid.yml +6 -0
  45. data/spec/{rails_app → test_app}/config/routes.rb +2 -2
  46. data/spec/test_app/log/.gitkeep +0 -0
  47. data/spec/test_app/log/development.log +734 -0
  48. metadata +69 -80
  49. data/VERSION +0 -1
  50. data/app/assets/javascripts/lolita/i18n/i18n.js.coffee +0 -135
  51. data/lib/lolita-i18n/backend.rb +0 -87
  52. data/spec/lolita-i18n/backend_spec.rb +0 -33
  53. data/spec/rails_app/config/application.rb +0 -18
  54. data/spec/rails_app/config/initializers/lolita_i18n.rb +0 -16
  55. data/spec/rails_app/config/locales/en.yml +0 -10
  56. data/spec/rails_app/config/locales/lv.yml +0 -2
@@ -0,0 +1,11 @@
1
+ module Lolita
2
+ module I18n
3
+ class Version
4
+ VERSION = "0.4.0"
5
+
6
+ def self.to_s
7
+ VERSION
8
+ end
9
+ end
10
+ end
11
+ end
data/lolita-i18n.gemspec CHANGED
@@ -1,108 +1,30 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "lolita-i18n/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = "lolita-i18n"
8
- s.version = "0.3.2"
6
+ s.name = "lolita-i18n"
7
+ s.version = Lolita::I18n::Version.to_s
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["ITHouse (Latvia)", "Arturs Meisters", "Gatis Tomsons"]
10
+ s.email = "support@ithouse.lv"
11
+ s.homepage = "http://github.com/ithouse/lolita-i18n"
12
+ s.summary = %q{Lolita plugin, that enables .yml management}
13
+ s.description = %q{Lolita plugin, that enables .yml files management from administrative interface. Also faster access to translations, that DB store}
9
14
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["ITHouse (Latvia)", "Arturs Meisters", "Gatis Tomsons"]
12
- s.date = "2012-02-27"
13
- s.description = "Lolita plugin, that enables .yml files management from administrative interface. Also faster access to translations, that DB store"
14
- s.email = "support@ithouse.lv"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.md"
18
18
  ]
19
- s.files = [
20
- ".document",
21
- ".rspec",
22
- "Gemfile",
23
- "History.rdoc",
24
- "LICENSE.txt",
25
- "README.md",
26
- "Rakefile",
27
- "VERSION",
28
- "app/assets/images/lolita/i18n/google_translate_icon.png",
29
- "app/assets/javascripts/lolita/i18n/application.js",
30
- "app/assets/javascripts/lolita/i18n/i18n.js.coffee",
31
- "app/assets/javascripts/lolita/i18n/spin.min.js",
32
- "app/assets/stylesheets/lolita/i18n/application.scss",
33
- "app/controllers/lolita/i18n_controller.rb",
34
- "app/helpers/lolita/i18n_helper.rb",
35
- "app/views/lolita/i18n/index.html.haml",
36
- "config/locales/en.yml",
37
- "config/locales/lv.yml",
38
- "config/routes.rb",
39
- "lib/lolita-i18n.rb",
40
- "lib/lolita-i18n/backend.rb",
41
- "lib/lolita-i18n/exceptions.rb",
42
- "lib/lolita-i18n/module.rb",
43
- "lib/lolita-i18n/rails.rb",
44
- "lolita-i18n.gemspec",
45
- "spec/controllers/lolita/i18n_controller_spec.rb",
46
- "spec/lolita-i18n/backend_spec.rb",
47
- "spec/rails_app/app/controllers/application_controller.rb",
48
- "spec/rails_app/config/application.rb",
49
- "spec/rails_app/config/enviroment.rb",
50
- "spec/rails_app/config/initializers/lolita_i18n.rb",
51
- "spec/rails_app/config/locales/en.yml",
52
- "spec/rails_app/config/locales/lv.yml",
53
- "spec/rails_app/config/routes.rb",
54
- "spec/spec_helper.rb"
55
- ]
56
- s.homepage = "http://github.com/ithouse/lolita-i18n"
57
19
  s.licenses = ["MIT"]
58
- s.require_paths = ["lib"]
59
- s.rubygems_version = "1.8.15"
60
- s.summary = "Lolita plugin, that enables .yml management"
61
- s.test_files = [
62
- "spec/controllers/lolita/i18n_controller_spec.rb",
63
- "spec/lolita-i18n/backend_spec.rb",
64
- "spec/rails_app/app/controllers/application_controller.rb",
65
- "spec/rails_app/config/application.rb",
66
- "spec/rails_app/config/enviroment.rb",
67
- "spec/rails_app/config/initializers/lolita_i18n.rb",
68
- "spec/rails_app/config/routes.rb",
69
- "spec/spec_helper.rb"
70
- ]
71
20
 
72
- if s.respond_to? :specification_version then
73
- s.specification_version = 3
21
+ s.add_runtime_dependency(%q<lolita>, ["~> 3.2.0.rc.9"])
22
+ s.add_runtime_dependency(%q<hiredis>, ["~> 0.3.1"])
23
+ s.add_runtime_dependency(%q<redis>, ["~> 2.2.2"])
24
+ s.add_runtime_dependency(%q<yajl-ruby>,["~> 1.0.0"])
25
+ s.add_runtime_dependency(%q<unicode_utils>,["~> 1.3.0"])
74
26
 
75
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
76
- s.add_runtime_dependency(%q<lolita>, ["~> 3.2.0.rc.6"])
77
- s.add_runtime_dependency(%q<hiredis>, ["~> 0.3.1"])
78
- s.add_runtime_dependency(%q<redis>, ["~> 2.2.2"])
79
- s.add_runtime_dependency(%q<yajl-ruby>, ["~> 1.0.0"])
80
- s.add_runtime_dependency(%q<easy_translate>, ["~> 0.2.1"])
81
- s.add_development_dependency(%q<shoulda>, [">= 0"])
82
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
83
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
84
- s.add_development_dependency(%q<rcov>, [">= 0"])
85
- else
86
- s.add_dependency(%q<lolita>, ["~> 3.2.0.rc.6"])
87
- s.add_dependency(%q<hiredis>, ["~> 0.3.1"])
88
- s.add_dependency(%q<redis>, ["~> 2.2.2"])
89
- s.add_dependency(%q<yajl-ruby>, ["~> 1.0.0"])
90
- s.add_dependency(%q<easy_translate>, ["~> 0.2.1"])
91
- s.add_dependency(%q<shoulda>, [">= 0"])
92
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
93
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
94
- s.add_dependency(%q<rcov>, [">= 0"])
95
- end
96
- else
97
- s.add_dependency(%q<lolita>, ["~> 3.2.0.rc.6"])
98
- s.add_dependency(%q<hiredis>, ["~> 0.3.1"])
99
- s.add_dependency(%q<redis>, ["~> 2.2.2"])
100
- s.add_dependency(%q<yajl-ruby>, ["~> 1.0.0"])
101
- s.add_dependency(%q<easy_translate>, ["~> 0.2.1"])
102
- s.add_dependency(%q<shoulda>, [">= 0"])
103
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
104
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
105
- s.add_dependency(%q<rcov>, [">= 0"])
106
- end
27
+ s.files = `git ls-files`.split("\n")
28
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
29
+ s.require_paths = ["lib"]
107
30
  end
108
-
@@ -1,30 +1,64 @@
1
- # coding: utf-8
2
-
3
- USE_RAILS=true unless defined?(USE_RAILS)
4
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
5
-
6
- include Lolita::Test::Matchers
7
-
8
- describe Lolita::I18nController do
9
- render_views
10
-
11
- it "should have i18n routes" do
12
- {:get=>"/lolita/i18n"}.should be_routable
13
- {:put=>"/lolita/i18n/1"}.should be_routable
14
- {:put=>"/lolita/i18n/translate_untranslated"}.should be_routable
15
- {:get=>"/lolita/i18n"}.should be_routable
16
- end
17
-
18
- it "should show all translations" do
19
- get :index
20
- response.should render_template("index")
21
- response.body.should match(/#{::I18n.t('lolita-i18n.title')}/)
22
- end
23
-
24
- it "should save translation" do
25
- put :update, :id=>"en.Page title",:translation=>"New title", :format => :json
26
- response.body.should == {error: false}.to_json
27
- ::I18n.t("Page title", :locale => :en).should == "New title"
28
- end
29
-
30
- end
1
+ require 'spec_helper'
2
+
3
+ describe Lolita::I18nController, :rails => true do
4
+
5
+ describe "GET index" do
6
+ it "should authorize resource" do
7
+ controller.authorization_proxy.should_receive(:authorize!).with(:read,controller.resource_class)
8
+ get :index
9
+ end
10
+
11
+ it "should assign all translations to variable" do
12
+ get :index
13
+ assigns[:translations].should be_kind_of(Hash)
14
+ end
15
+
16
+ it "should sort translations when params[:sort] is equal to 1" do
17
+ i18n_request = double("i18n_request")
18
+ i18n_request.stub(:translations)
19
+ controller.stub(:i18n_request => i18n_request)
20
+ i18n_request.should_not_receive(:sort_translations)
21
+ get :index
22
+ i18n_request = double("i18n_request2")
23
+ i18n_request.stub(:translations)
24
+ controller.stub(:i18n_request => i18n_request)
25
+ i18n_request.should_receive(:sort_translations)
26
+ get :index, :sort => 1
27
+ end
28
+ end
29
+
30
+ describe "PUT update" do
31
+ it "should authorize resource for update" do
32
+ controller.authorization_proxy.should_receive(:authorize!).with(:update,controller.resource_class)
33
+ put :update, :id => "key", :translation => "translation", :format => :json
34
+ end
35
+
36
+ it "should create notice when save is successful" do
37
+ controller.stub(:i18n_request => stub(:update_key => true))
38
+ controller.should_receive(:notice).with(kind_of(String))
39
+ put :update, :id => Base64.encode64("lv.title"),:translation => "Tulkots virsraksts", :format => :json
40
+ end
41
+
42
+ it "should create error when save is unsuccessful" do
43
+ controller.stub(:i18n_request => stub(:update_key => false))
44
+ controller.should_receive(:error).with(kind_of(String))
45
+ put :update, :id => Base64.encode64("lv.title"),:translation => "Tulkots virsraksts", :format => :json
46
+ end
47
+
48
+ it "should render error when Lolita::I18n::Exceptions::MissingInterpolationArgument is raised" do
49
+ i18n_request = double("request")
50
+ i18n_request.stub(:update_key).and_raise(Lolita::I18n::Exceptions::MissingInterpolationArgument.new(["%{count}"]))
51
+ controller.stub(:i18n_request => i18n_request)
52
+ put :update, :id => Base64.encode64("lv.title"),:translation => "Tulkots virsraksts", :format => :json
53
+ response.body.should eq(%q{{"error":"Translation should contain all these variables %{count}"}})
54
+ end
55
+
56
+ it "should render error when any other excption happens" do
57
+ i18n_request = double("request")
58
+ i18n_request.stub(:update_key).and_raise(ArgumentError.new)
59
+ controller.stub(:i18n_request => i18n_request)
60
+ put :update, :id => Base64.encode64("lv.title"),:translation => "Tulkots virsraksts", :format => :json
61
+ response.body.should eq(%q{{"error":"Key is not saved. Some error accured."}})
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lolita::I18nHelper, :rails => true do
4
+ it "should return locale options" do
5
+ good_locale_options = [
6
+ ["Choose other language",nil],
7
+ ["Latvian",:lv],
8
+ ["Russian",:ru]
9
+ ]
10
+ helper.locale_options.should eq(good_locale_options)
11
+ end
12
+
13
+ it "translation is visible when value is not blank and params does not say to show only untranslated values" do
14
+ helper.translation_visible?("").should be_true
15
+ helper.translation_visible?("text").should be_true
16
+ helper.translation_visible?([]).should be_true
17
+ helper.translation_visible?({}).should be_true
18
+
19
+ helper.params[:show_untranslated] = true
20
+ helper.translation_visible?("").should be_true
21
+ helper.translation_visible?("text").should be_false
22
+ helper.translation_visible?([]).should be_true
23
+ helper.translation_visible?({}).should be_true
24
+ end
25
+
26
+ describe "#any_translation_visible?" do
27
+ it "should detect if any translation from array is visible" do
28
+ helper.any_translation_visible?([2,1]).should be_true
29
+ helper.any_translation_visible?([]).should be_true
30
+
31
+ helper.params[:show_untranslated] = true
32
+ helper.any_translation_visible?([]).should be_true
33
+ helper.any_translation_visible?(["",1]).should be_true
34
+ helper.any_translation_visible?(["a","b"]).should be_false
35
+ end
36
+
37
+
38
+ it "should detect if any translation from hash is visible" do
39
+ helper.any_translation_visible?({:a => "1",:b => "2" }).should be_true
40
+ helper.any_translation_visible?([]).should be_true
41
+
42
+ helper.params[:show_untranslated] = true
43
+ helper.any_translation_visible?({}).should be_true
44
+ helper.any_translation_visible?({:a => "1",:b => "2" }).should be_false
45
+ helper.any_translation_visible?({:a => "1",:b => "" }).should be_true
46
+ end
47
+
48
+ it "should call #translation_visible? for any other value" do
49
+ helper.should_receive(:translation_visible?).with("text")
50
+ helper.any_translation_visible?("text")
51
+ end
52
+ end
53
+
54
+ describe "#sort_link" do
55
+ before(:each) do
56
+ {:controller => "lolita/i18n",:action => "index", :i18n_locale => :en}.each do |k,v|
57
+ helper.params[k] = v
58
+ end
59
+ end
60
+ it "should create link with sort in it when params does not include :sort" do
61
+ helper.sort_link.should match(/sort=1/)
62
+ end
63
+
64
+ it "should create link witho sort in it when params include :sort" do
65
+ helper.params[:sort] = "1"
66
+ helper.sort_link.should_not match(/sort=1/)
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lolita::I18n::Configuration do
4
+ let(:klass){Lolita::I18n::Configuration}
5
+ let(:conf){klass.new}
6
+ let(:redis){double("redis")}
7
+
8
+ it "should load rails if Rails is defined" do
9
+ Lolita.stub(:rails3?).and_return(true)
10
+ conf.should_receive(:require).with('lolita-i18n/rails')
11
+ conf.load_rails!
12
+ end
13
+
14
+ it "should allow to assign store with Hash as connection options" do
15
+ Redis.should_receive(:new).with({:key => "value"}).and_return(redis)
16
+ conf.store={:key => "value"}
17
+ end
18
+
19
+ it "should allow to assign store as store itself" do
20
+ conf.store = redis
21
+ conf.store.should eq(redis)
22
+ end
23
+
24
+ it "should return assigned store" do
25
+ conf.store = redis
26
+ conf.store.should eq(redis)
27
+ end
28
+
29
+ it "should return new Redis connection and warn when no store is assigned" do
30
+ Redis.should_receive(:new).and_return(redis)
31
+ conf.should_receive(:warn).with("Lolita::I18n No store specified. See Lolita::I18n")
32
+ conf.store.should eq(redis)
33
+ end
34
+
35
+ it "should lazy create and return backend" do
36
+ Redis.should_receive(:new).and_return(redis)
37
+ ::I18n::Backend::KeyValue.should_receive(:new).with(redis)
38
+ conf.backend
39
+ end
40
+
41
+ it "should load translations" do
42
+ yaml_backend = double("yaml_backend")
43
+ conf.yaml_backend = yaml_backend
44
+ yaml_backend.should_receive(:load_translations)
45
+ conf.load_translations
46
+ end
47
+
48
+ it "should initialize chain" do
49
+ conf.yaml_backend = double("yaml backend")
50
+ Redis.stub(:new).and_return(redis)
51
+ ::I18n::Backend::Chain.should_receive(:new).with(conf.backend,conf.yaml_backend)
52
+ conf.initialize_chain
53
+ end
54
+
55
+ it "include module in ::I18n::Backend::Simple" do
56
+ conf.include_modules
57
+ ::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Flatten)
58
+ ::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Pluralization)
59
+ ::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::Metadata)
60
+ ::I18n::Backend::Simple.ancestors.should include(::I18n::Backend::InterpolationCompiler)
61
+ end
62
+
63
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lolita::I18n::Exceptions::MissingInterpolationArgument do
4
+ it "create new StandartError with custom message" do
5
+ error = Lolita::I18n::Exceptions::MissingInterpolationArgument.new(["arg1","arg2"])
6
+ error.message.should eq("Translation should contain all these variables arg1, arg2")
7
+ end
8
+ end
9
+
10
+ describe Lolita::I18n::Exceptions::TranslationDoesNotMatch do
11
+ it "should cretae new ArgumentError with custom message" do
12
+ error = Lolita::I18n::Exceptions::TranslationDoesNotMatch.new("translation", "original")
13
+ error.message.should eq("Translation translation does not match original")
14
+ end
15
+ end
@@ -0,0 +1,238 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lolita::I18n::Request do
4
+
5
+ describe Lolita::I18n::Request::Validator do
6
+ let(:validator){Lolita::I18n::Request::Validator.new}
7
+
8
+ it "should validate string and return error when original and given values interpolations doesn't match" do
9
+ ::I18n.stub(:t).and_return("original text")
10
+ expect{validator.validate("text_key","text")}.not_to raise_error
11
+
12
+ ::I18n.stub(:t).and_return("text with %{interpolation}")
13
+ expect{
14
+ validator.validate("text_key","text")
15
+ }.to raise_error(Lolita::I18n::Exceptions::MissingInterpolationArgument)
16
+ end
17
+
18
+ it "should validate array's each value as string" do
19
+ ::I18n.stub(:t).and_return(["a","b","c"])
20
+ expect{validator.validate("array_key",["1","2","3"])}.not_to raise_error
21
+
22
+ ::I18n.stub(:t).and_return(["a","%{b}","c"])
23
+ expect{
24
+ validator.validate("array_key",["1","2","3"])
25
+ }.to raise_error(Lolita::I18n::Exceptions::MissingInterpolationArgument,"Translation should contain all these variables %{b}")
26
+ end
27
+
28
+ it "should validate hash's each value as string" do
29
+ ::I18n.stub(:t).and_return({:a => "1", :b => "2"})
30
+ expect{validator.validate("array_key",{"a" => "a", "b" => "b"})}.not_to raise_error
31
+
32
+ ::I18n.stub(:t).and_return({:a => "1", :b => "2 %{count}"})
33
+ expect{
34
+ validator.validate("array_key",{"a" => "a", "b" => "b"})
35
+ }.to raise_error(Lolita::I18n::Exceptions::MissingInterpolationArgument)
36
+ end
37
+
38
+ it "should raise error when translation is Array but original is not or sizes does not match" do
39
+ ::I18n.stub(:t).and_return("")
40
+ expect{
41
+ validator.validate("key",[1,2])
42
+ }.to raise_error(Lolita::I18n::Exceptions::TranslationDoesNotMatch)
43
+
44
+ ::I18n.stub(:t).and_return([1,2])
45
+ expect{
46
+ validator.validate("key",[2])
47
+ }
48
+ end
49
+
50
+ it "should raise error when translation is Hash but original is not or keys does not match " do
51
+ ::I18n.stub(:t).and_return("")
52
+ expect{
53
+ validator.validate("key",{"a" => 1})
54
+ }.to raise_error(Lolita::I18n::Exceptions::TranslationDoesNotMatch)
55
+ ::I18n.stub(:t).and_return({:a => 2, :b => 3})
56
+ expect{
57
+ validator.validate("key",{"a" => 1})
58
+ }
59
+ end
60
+ end
61
+
62
+ describe Lolita::I18n::Request::Translation do
63
+ let(:klass){Lolita::I18n::Request::Translation}
64
+
65
+ it "should create new with key and translation as Array,Hash or String" do
66
+ expect{
67
+ Lolita::I18n::Request::Translation.new("key","value")
68
+ Lolita::I18n::Request::Translation.new("key", ["value"])
69
+ Lolita::I18n::Request::Translation.new("key", {:hkey => "value"})
70
+ }.not_to raise_error
71
+ end
72
+
73
+ it "should return parsed value" do
74
+ I18n.stub(:t).and_return("original_value")
75
+ t = klass.new("key","value")
76
+ t.value.should eq("value")
77
+ end
78
+
79
+ it "should return locale for key" do
80
+ t = klass.new("lv.key","value")
81
+ t.locale.should eq(:lv)
82
+ end
83
+
84
+ it "should return key" do
85
+ t = klass.new("lv.key","value")
86
+ t.key.should eq("key")
87
+ end
88
+
89
+ it "should return array for store with locale, key => value and configuration hash" do
90
+ t = klass.new("lv.my.key","value")
91
+ t.for_store.should eq([:lv,{"my.key" => "value"}, {:escape => false}])
92
+ end
93
+
94
+ it "should return original translation of given key" do
95
+ ::I18n.stub(:t).with(kind_of(String), kind_of(Hash)).and_return("original")
96
+ t = klass.new("ru.my.key","value")
97
+ t.original.should eq("original")
98
+ end
99
+ end
100
+
101
+
102
+ describe Lolita::I18n::Request::Translations do
103
+ before(:each) do
104
+ ::I18n.locale = :en
105
+ end
106
+
107
+ let(:klass){Lolita::I18n::Request::Translations}
108
+ let(:translations) {
109
+ ::I18n.stub(:t) do |*args|
110
+ if args[1] && args[1][:locale] == :en
111
+ if args[0] == :arr
112
+ [1,2]
113
+ elsif args[0] == :str
114
+ "string"
115
+ elsif args[0] == :inter
116
+ {:one => "one",:other => "other"}
117
+ elsif args[0] == :"hsh.key"
118
+ "value"
119
+ end
120
+ else
121
+ "-no-translation-"
122
+ end
123
+ end
124
+ {:arr => [1,2], :str => "string", :inter => {:one => "one",:other => "other"}, :hsh => {:key => "value"}}
125
+ }
126
+
127
+ it "should create new with hash" do
128
+ expect{
129
+ klass.new({})
130
+ }.not_to raise_error
131
+ end
132
+
133
+ it "should detect if value is final and should't be flattened any more" do
134
+ t = klass.new({})
135
+ t.final_value?(1).should be_true
136
+ t.final_value?([]).should be_true
137
+ t.final_value?({:other => "other", :one => "one"}).should be_true
138
+ t.final_value?({:other => "other"}).should be_false
139
+ t.final_value?({:other => [], :one => 1}).should be_false
140
+ t.final_value?({:other => {}, :one => 1}).should be_false
141
+ end
142
+
143
+ it "should return default translation value for different original values" do
144
+ t = klass.new({})
145
+ ::I18n.stub(:t).and_return([])
146
+ t.translation_value("key",[1,2],"ru").should eq([])
147
+ ::I18n.stub(:t).and_return({})
148
+ t.translation_value("key",{:a=>1},"ru").should eq({})
149
+ ::I18n.stub(:t).and_return("translation")
150
+ t.translation_value("key","value","ru").should eq("translation")
151
+ ::I18n.stub(:t).and_return([1,2])
152
+ t.translation_value("key",[2,3],"ru").should eq([1,2])
153
+ end
154
+
155
+ it "should flatten keys until final value is found" do
156
+ t = klass.new({})
157
+ result = []
158
+ t.flatten_keys(translations, :lv) do |key,translation_value,original_value|
159
+ result << [key,translation_value,original_value]
160
+ end
161
+ valid_results = [
162
+ [:arr, [], [1,2]],
163
+ [:str, "-no-translation-", "string"],
164
+ [:inter, {}, {:one => "one", :other => "other"}],
165
+ [:"hsh.key","-no-translation-", "value"]
166
+ ]
167
+ result.should eq(valid_results)
168
+ end
169
+
170
+ it "should normalize for locale" do
171
+ t = klass.new(translations)
172
+ valid_results = {
173
+ :arr => {:translation => [], :original_translation => [1,2]},
174
+ :str => {:translation => "-no-translation-", :original_translation => "string"},
175
+ :inter => {:translation => {}, :original_translation => {:one => "one", :other => "other"}},
176
+ :"hsh.key" => {:translation => "-no-translation-", :original_translation => "value"}
177
+ }
178
+ t.normalized(:lv).should eq(valid_results)
179
+ end
180
+
181
+ end
182
+
183
+ let(:request_klass){Lolita::I18n::Request}
184
+
185
+ it "should create new request with params" do
186
+ expect{
187
+ request = request_klass.new({:a => 1})
188
+ request.params.should eq({:a => 1})
189
+ }.not_to raise_error
190
+ end
191
+
192
+ it "should return translations" do
193
+ r = request_klass.new({})
194
+ Lolita.i18n.should_receive(:load_translations).and_return(true)
195
+ Lolita.i18n.stub(:yaml_backend).and_return(stub(:translations => {:en => {}}))
196
+ Lolita::I18n::Request::Translations.any_instance.should_receive(:normalized).once
197
+ r.translations(:en)
198
+ end
199
+
200
+ it "should sort translations" do
201
+ r = request_klass.new({})
202
+ unsorted_translations = {
203
+ :"key9" => {:original_translation => "ZZZ"},
204
+ :"key8" => {:original_translation => [1,2]},
205
+ :"key7" => {:original_translation => {:a => 1}},
206
+ :"key6" => {:original_translation => "aaa"},
207
+ :"key5" => {:original_translation => nil},
208
+ :"key4" => {:original_translation => true},
209
+ :"key3" => {:original_translation => false}
210
+ }
211
+ sorted_translations = [
212
+ [:"key7", {:original_translation => {:a => 1}}],
213
+ [:"key8", {:original_translation => [1,2]}],
214
+ [:"key5", {:original_translation => nil}],
215
+ [:"key6", {:original_translation => "aaa"}],
216
+ [:"key3", {:original_translation => false}],
217
+ [:"key4", {:original_translation => true}],
218
+ [:"key9", {:original_translation => "ZZZ"}]
219
+ ]
220
+ r.sort_translations(unsorted_translations).should eq(sorted_translations)
221
+ end
222
+
223
+ it "should update key" do
224
+ r = request_klass.new({:translation => "translation", :id => Base64.encode64("ru.key")})
225
+ ::I18n.stub(:t).and_return("original")
226
+ backend = double("backend")
227
+ Lolita.i18n.stub(:backend => backend)
228
+ backend.should_receive(:store_translations).with(:"ru", { "key" => "translation" }, :escape => false).and_return(true)
229
+ r.update_key
230
+ end
231
+
232
+ it "should have validator" do
233
+ r = request_klass.new({})
234
+ r.validator.should be_a_kind_of(Lolita::I18n::Request::Validator)
235
+ end
236
+
237
+
238
+ end