tr8n_core 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +69 -0
  4. data/Rakefile +9 -0
  5. data/config/config.yml +34 -0
  6. data/config/tokens/data.yml +45 -0
  7. data/config/tokens/decorations.yml +37 -0
  8. data/lib/tr8n/application.rb +320 -0
  9. data/lib/tr8n/base.rb +123 -0
  10. data/lib/tr8n/cache.rb +144 -0
  11. data/lib/tr8n/cache_adapters/cdb.rb +74 -0
  12. data/lib/tr8n/cache_adapters/file.rb +70 -0
  13. data/lib/tr8n/cache_adapters/memcache.rb +91 -0
  14. data/lib/tr8n/cache_adapters/redis.rb +94 -0
  15. data/lib/tr8n/component.rb +68 -0
  16. data/lib/tr8n/config.rb +291 -0
  17. data/lib/tr8n/decorators/base.rb +35 -0
  18. data/lib/tr8n/decorators/default.rb +30 -0
  19. data/lib/tr8n/decorators/html.rb +63 -0
  20. data/lib/tr8n/exception.rb +26 -0
  21. data/lib/tr8n/language.rb +250 -0
  22. data/lib/tr8n/language_case.rb +116 -0
  23. data/lib/tr8n/language_case_rule.rb +85 -0
  24. data/lib/tr8n/language_context.rb +115 -0
  25. data/lib/tr8n/language_context_rule.rb +62 -0
  26. data/lib/tr8n/logger.rb +83 -0
  27. data/lib/tr8n/rules_engine/evaluator.rb +156 -0
  28. data/lib/tr8n/rules_engine/parser.rb +83 -0
  29. data/lib/tr8n/source.rb +95 -0
  30. data/lib/tr8n/tokens/data.rb +410 -0
  31. data/lib/tr8n/tokens/data_tokenizer.rb +82 -0
  32. data/lib/tr8n/tokens/decoration_tokenizer.rb +200 -0
  33. data/lib/tr8n/tokens/hidden.rb +48 -0
  34. data/lib/tr8n/tokens/method.rb +52 -0
  35. data/lib/tr8n/tokens/transform.rb +191 -0
  36. data/lib/tr8n/translation.rb +104 -0
  37. data/lib/tr8n/translation_key.rb +205 -0
  38. data/lib/tr8n/translator.rb +62 -0
  39. data/lib/tr8n/utils.rb +124 -0
  40. data/lib/tr8n_core/ext/array.rb +74 -0
  41. data/lib/tr8n_core/ext/date.rb +63 -0
  42. data/lib/tr8n_core/ext/fixnum.rb +39 -0
  43. data/lib/tr8n_core/ext/hash.rb +126 -0
  44. data/lib/tr8n_core/ext/string.rb +44 -0
  45. data/lib/tr8n_core/ext/time.rb +71 -0
  46. data/lib/tr8n_core/generators/cache/base.rb +85 -0
  47. data/lib/tr8n_core/generators/cache/cdb.rb +27 -0
  48. data/lib/tr8n_core/generators/cache/file.rb +69 -0
  49. data/lib/tr8n_core/modules/logger.rb +43 -0
  50. data/lib/tr8n_core/version.rb +27 -0
  51. data/lib/tr8n_core.rb +68 -0
  52. data/spec/application_spec.rb +228 -0
  53. data/spec/base_spec.rb +19 -0
  54. data/spec/config_spec.rb +16 -0
  55. data/spec/decorator_spec.rb +10 -0
  56. data/spec/decorators/base_spec.rb +14 -0
  57. data/spec/decorators/default_spec.rb +12 -0
  58. data/spec/decorators/html_spec.rb +50 -0
  59. data/spec/fixtures/application.json +112 -0
  60. data/spec/fixtures/languages/en-US.json +1424 -0
  61. data/spec/fixtures/languages/es.json +291 -0
  62. data/spec/fixtures/languages/ru.json +550 -0
  63. data/spec/fixtures/translations/ru/basic.json +56 -0
  64. data/spec/fixtures/translations/ru/counters.json +43 -0
  65. data/spec/fixtures/translations/ru/genders.json +171 -0
  66. data/spec/fixtures/translations/ru/last_names.txt +200 -0
  67. data/spec/fixtures/translations/ru/names.json +1 -0
  68. data/spec/fixtures/translations/ru/names.txt +458 -0
  69. data/spec/helper.rb +84 -0
  70. data/spec/language_case_rule_spec.rb +57 -0
  71. data/spec/language_case_spec.rb +58 -0
  72. data/spec/language_context_rule_spec.rb +73 -0
  73. data/spec/language_context_spec.rb +331 -0
  74. data/spec/language_spec.rb +16 -0
  75. data/spec/rules_engine/evaluator_spec.rb +148 -0
  76. data/spec/rules_engine/parser_spec.rb +29 -0
  77. data/spec/tokens/data_spec.rb +160 -0
  78. data/spec/tokens/data_tokenizer_spec.rb +29 -0
  79. data/spec/tokens/decoration_tokenizer_spec.rb +81 -0
  80. data/spec/tokens/hidden_spec.rb +24 -0
  81. data/spec/tokens/method_spec.rb +84 -0
  82. data/spec/tokens/transform_spec.rb +50 -0
  83. data/spec/translation_key_spec.rb +96 -0
  84. data/spec/translation_spec.rb +24 -0
  85. data/spec/utils_spec.rb +64 -0
  86. metadata +176 -0
@@ -0,0 +1,160 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Tokens::Data do
6
+ before do
7
+ @app = init_application
8
+ @english = @app.language('en-US')
9
+ @tkey = Tr8n::TranslationKey.new({
10
+ :label => "Hello {user}",
11
+ :application => @app,
12
+ :locale => 'en-US'
13
+ })
14
+ @token = @tkey.data_tokens.first
15
+ end
16
+
17
+ describe "initialize" do
18
+ it "should parse data token" do
19
+ expect(@token.class.name).to eq("Tr8n::Tokens::Data")
20
+ expect(@token.context_keys).to eq([])
21
+ end
22
+ end
23
+
24
+ describe "substitute" do
25
+ it "should substitute values" do
26
+ user = stub_object({:first_name => "Tom", :last_name => "Anderson", :gender => "Male", :to_s => "Tom Anderson"})
27
+ user.stub(:last_name_with_prefix) {|prefix| "#{prefix} #{user.last_name}"}
28
+
29
+ # tr("Hello {user}", "", {:user => current_user}}
30
+ expect(@token.token_value(user, {}, @english)).to eq(user.to_s)
31
+
32
+ # tr("Hello {user}", "", {:user => [current_user]}}
33
+ expect(@token.token_value([user], {}, @english)).to eq(user.to_s)
34
+
35
+ # tr("Hello {user}", "", {:user => [current_user, current_user.name]}}
36
+ expect(@token.token_value([user, user.first_name], {}, @english)).to eq(user.first_name)
37
+
38
+ # tr("Hello {user}", "", {:user => [current_user, "{$0} {$1}", "param1"]}}
39
+ expect(@token.token_value([user, "{$0} {$1}", "param1"], {}, @english)).to eq(user.to_s + " param1")
40
+ expect(@token.token_value([user, "{$0} {$1} {$2}", "param1", "param2"], {}, @english)).to eq(user.to_s + " param1 param2")
41
+
42
+ # tr("Hello {user}", "", {:user => [current_user, :name]}}
43
+ expect(@token.token_value([user, :first_name], {}, @english)).to eq(user.first_name)
44
+
45
+ # tr("Hello {user}", "", {:user => [current_user, :method_name, "param1"]}}
46
+ expect(@token.token_value([user, :last_name_with_prefix, 'Mr.'], {}, @english)).to eq("Mr. Anderson")
47
+
48
+ # tr("Hello {user}", "", {:user => [current_user, lambda{|user| user.name}]}}
49
+ expect(@token.token_value([user, lambda{|user| user.to_s}], {}, @english)).to eq(user.to_s)
50
+
51
+ # tr("Hello {user}", "", {:user => [current_user, lambda{|user, param1| user.name}, "param1"]}}
52
+ expect(@token.token_value([user, lambda{|user, param1| user.to_s + " " + param1}, "extra_param1"], {}, @english)).to eq(user.to_s + " extra_param1")
53
+
54
+ # tr("Hello {user}", "", {:user => {:object => current_user, :value => current_user.name}]}}
55
+ expect(@token.token_value({:object => user, :value => user.to_s}, {}, @english)).to eq(user.to_s)
56
+
57
+ # tr("Hello {user}", "", {:user => {:object => current_user, :attribute => :first_name}]}}
58
+ expect(@token.token_value({:object => user, :attribute => :first_name}, {}, @english)).to eq(user.first_name)
59
+ expect(@token.token_value({:object => {:first_name => "Michael"}, :attribute => :first_name}, {}, @english)).to eq("Michael")
60
+ end
61
+
62
+ it "should perform complete substitution" do
63
+ user = stub_object({:first_name => "Tom", :last_name => "Anderson", :gender => "Male", :to_s => "Tom Anderson"})
64
+ user.stub(:last_name_with_prefix) {|prefix| "#{prefix} #{user.last_name}"}
65
+
66
+ [
67
+ {:user => user}, "Hello Tom Anderson",
68
+ {:user => [user]}, "Hello Tom Anderson",
69
+ {:user => [user, user.first_name]}, "Hello Tom",
70
+ {:user => [user, "{$0} {$1}", "param1"]}, "Hello Tom Anderson param1",
71
+ {:user => [user, "{$0} {$1} {$2}", "param1", "param2"]}, "Hello Tom Anderson param1 param2",
72
+ {:user => [user, :first_name]}, "Hello Tom",
73
+ {:user => [user, :last_name_with_prefix, 'Mr.']}, "Hello Mr. Anderson",
74
+ {:user => [user, lambda{|user| user.to_s}]}, "Hello Tom Anderson",
75
+ {:user => [user, lambda{|user, param1| user.to_s + " " + param1}, "extra_param1"]}, "Hello Tom Anderson extra_param1",
76
+ {:user => {:object => user, :value => user.to_s}}, "Hello Tom Anderson",
77
+ {:user => {:object => user, :attribute => :first_name}}, "Hello Tom",
78
+ {:user => {:object => {:first_name => "Michael"}, :attribute => :first_name}}, "Hello Michael"
79
+ ].each_slice(2).to_a.each do |pair|
80
+ expect(@token.substitute(@tkey.label, pair.first, @english)).to eq(pair.last)
81
+ end
82
+
83
+ end
84
+
85
+ it "should substitute token with array values" do
86
+ tkey = Tr8n::TranslationKey.new({
87
+ :label => "Hello {users}",
88
+ :application => @app,
89
+ :locale => 'en-US'
90
+ })
91
+ token = tkey.data_tokens.first
92
+
93
+ users = []
94
+ 1.upto(6) do |i|
95
+ users << stub_object({:first_name => "First name #{i}", :last_name => "Last name #{i}", :gender => "Male"})
96
+ end
97
+
98
+ Tr8n.config.with_block_options(:dry => true) do
99
+ expect(token.token_value([users, :first_name], {}, @english)).to match("2 others")
100
+
101
+ expect(token.token_value([users, [:first_name], {
102
+ :translate_items => false,
103
+ :expandable => false,
104
+ :minimizable => true,
105
+ :to_sentence => true,
106
+ :limit => 4,
107
+ :separator => ", ",
108
+ :description => nil,
109
+ :andor => 'and'
110
+ }], {}, @english)).to eq("First name 1, First name 2, First name 3, First name 4 and 2 others")
111
+
112
+ expect(token.token_value([users, [:first_name], {
113
+ :translate_items => false,
114
+ :expandable => false,
115
+ :minimizable => true,
116
+ :to_sentence => false,
117
+ :limit => 4,
118
+ :separator => ", ",
119
+ :description => nil,
120
+ :andor => 'and'
121
+ }], {}, @english)).to eq("First name 1, First name 2, First name 3, First name 4, First name 5, First name 6")
122
+
123
+ expect(token.token_value([users, [:first_name], {
124
+ :translate_items => false,
125
+ :expandable => false,
126
+ :minimizable => true,
127
+ :to_sentence => true,
128
+ :limit => 4,
129
+ :separator => ", ",
130
+ :description => nil,
131
+ :andor => 'or'
132
+ }], {}, @english)).to eq("First name 1, First name 2, First name 3, First name 4 or 2 others")
133
+
134
+ expect(token.token_value([users, [:first_name], {
135
+ :translate_items => false,
136
+ :expandable => false,
137
+ :minimizable => true,
138
+ :to_sentence => true,
139
+ :limit => 2,
140
+ :separator => ", ",
141
+ :description => nil,
142
+ :andor => 'or'
143
+ }], {}, @english)).to eq("First name 1, First name 2 or 4 others")
144
+
145
+ expect(token.token_value([users, [:first_name], {
146
+ :translate_items => false,
147
+ :expandable => false,
148
+ :minimizable => true,
149
+ :to_sentence => true,
150
+ :limit => 2,
151
+ :separator => " & ",
152
+ :description => nil,
153
+ :andor => 'or'
154
+ }], {}, @english)).to eq("First name 1 & First name 2 or 4 others")
155
+
156
+ end
157
+ end
158
+ end
159
+
160
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Tokens::DataTokenizer do
6
+ before do
7
+
8
+ end
9
+
10
+ describe "initialize" do
11
+ it "should parse the text correctly" do
12
+ dt = Tr8n::Tokens::DataTokenizer.new("Hello World")
13
+ expect(dt.tokens).to be_empty
14
+
15
+ dt = Tr8n::Tokens::DataTokenizer.new("Hello {world}")
16
+ expect(dt.tokens.count).to equal(1)
17
+ expect(dt.tokens.first.name).to eq("world")
18
+ expect(dt.tokens.first.name(:parens => true)).to eq("{world}")
19
+
20
+ dt = Tr8n::Tokens::DataTokenizer.new("Dear {user:gender}")
21
+ expect(dt.tokens.count).to equal(1)
22
+ expect(dt.tokens.first.name).to eq("user")
23
+ expect(dt.tokens.first.name(:parens => true)).to eq("{user}")
24
+ expect(dt.tokens.first.context_keys).to eq(['gender'])
25
+ expect(dt.tokens.first.name(:parens => true, :context_keys => true)).to eq("{user:gender}")
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,81 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Tokens::DecorationTokenizer do
6
+
7
+ describe "parse" do
8
+ it "should correctly parse tokens" do
9
+ dt = Tr8n::Tokens::DecorationTokenizer.new("Hello World")
10
+ expect(dt.fragments).to eq(["[tr8n]", "Hello World", "[/tr8n]"])
11
+ expect(dt.parse).to eq(["tr8n", "Hello World"])
12
+
13
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello World]")
14
+ expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello World", "]", "[/tr8n]"])
15
+ expect(dt.parse).to eq(["tr8n", ["bold", "Hello World"]])
16
+
17
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello World")
18
+ expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello World", "[/tr8n]"])
19
+ expect(dt.parse).to eq(["tr8n", ["bold", "Hello World"]])
20
+
21
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello [strong: World]]")
22
+ expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello ", "[strong:", " World", "]", "]", "[/tr8n]"])
23
+ expect(dt.parse).to eq(["tr8n", ["bold", "Hello ", ["strong", "World"]]])
24
+
25
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello [strong: World]")
26
+ expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello ", "[strong:", " World", "]", "[/tr8n]"])
27
+ expect(dt.parse).to eq(["tr8n", ["bold", "Hello ", ["strong", "World"]]])
28
+
29
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold1: Hello [strong22: World]]")
30
+ expect(dt.fragments).to eq(["[tr8n]", "[bold1:", " Hello ", "[strong22:", " World", "]", "]", "[/tr8n]"])
31
+ expect(dt.parse).to eq(["tr8n", ["bold1", "Hello ", ["strong22", "World"]]])
32
+
33
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello, [strong: how] [weak: are] you?]")
34
+ expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello, ", "[strong:", " how", "]", " ", "[weak:", " are", "]", " you?", "]", "[/tr8n]"])
35
+ expect(dt.parse).to eq(["tr8n", ["bold", "Hello, ", ["strong", "how"], " ", ["weak", "are"], " you?"]])
36
+
37
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello, [strong: how [weak: are] you?]")
38
+ expect(dt.fragments).to eq(["[tr8n]", "[bold:", " Hello, ", "[strong:", " how ", "[weak:", " are", "]", " you?", "]", "[/tr8n]"])
39
+ expect(dt.parse).to eq(["tr8n", ["bold", "Hello, ", ["strong", "how ", ["weak", "are"], " you?"]]])
40
+
41
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[link: you have [italic: [bold: {count}] messages] [light: in your mailbox]]")
42
+ expect(dt.fragments).to eq(["[tr8n]", "[link:", " you have ", "[italic:", " ", "[bold:", " {count}", "]", " messages", "]", " ", "[light:", " in your mailbox", "]", "]", "[/tr8n]"])
43
+ expect(dt.parse).to eq(["tr8n", ["link", "you have ", ["italic", "", ["bold", "{count}"], " messages"], " ", ["light", "in your mailbox"]]])
44
+
45
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[link] you have [italic: [bold: {count}] messages] [light: in your mailbox] [/link]")
46
+ expect(dt.fragments).to eq(["[tr8n]", "[link]", " you have ", "[italic:", " ", "[bold:", " {count}", "]", " messages", "]", " ", "[light:", " in your mailbox", "]", " ", "[/link]", "[/tr8n]"])
47
+ expect(dt.parse).to eq( ["tr8n", ["link", " you have ", ["italic", "", ["bold", "{count}"], " messages"], " ", ["light", "in your mailbox"], " "]])
48
+ end
49
+ end
50
+
51
+ describe "substitute" do
52
+ it "should correctly substitute tokens" do
53
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold: Hello World]")
54
+ expect(dt.substitute).to eq("<strong>Hello World</strong>")
55
+
56
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold]Hello World[/bold]")
57
+ expect(dt.substitute).to eq("<strong>Hello World</strong>")
58
+
59
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[bold] Hello World [/bold]")
60
+ expect(dt.substitute).to eq("<strong> Hello World </strong>")
61
+
62
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[p: Hello World]", :p => '<p>{$0}</p>')
63
+ expect(dt.substitute).to eq("<p>Hello World</p>")
64
+
65
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[p: Hello World]", :p => lambda{|text| "<p>#{text}</p>"})
66
+ expect(dt.substitute).to eq("<p>Hello World</p>")
67
+
68
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[p]Hello World[/p]", :p => lambda{|text| "<p>#{text}</p>"})
69
+ expect(dt.substitute).to eq("<p>Hello World</p>")
70
+
71
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[link: you have 5 messages]", "link" => '<a href="http://mail.google.com">{$0}</a>')
72
+ expect(dt.substitute).to eq("<a href=\"http://mail.google.com\">you have 5 messages</a>")
73
+
74
+ dt = Tr8n::Tokens::DecorationTokenizer.new("[link: you have {clount} messages]", "link" => '<a href="http://mail.google.com">{$0}</a>')
75
+ expect(dt.substitute).to eq("<a href=\"http://mail.google.com\">you have {clount} messages</a>")
76
+
77
+ end
78
+ end
79
+
80
+ end
81
+
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Tokens::Hidden do
6
+ before do
7
+ @app = init_application
8
+ @english = @app.language('en-US')
9
+ @tkey = Tr8n::TranslationKey.new({
10
+ :label => "{_he_she}",
11
+ :application => @app,
12
+ :locale => 'en-US'
13
+ })
14
+ @token = @tkey.data_tokens.first
15
+ end
16
+
17
+ describe "initialize" do
18
+ it "should parse data token" do
19
+ expect(@token.class.name).to eq("Tr8n::Tokens::Hidden")
20
+ expect(@token.context_keys).to eq([])
21
+ expect(@token.allowed_in_translation?).to be_false
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Tokens::Method do
6
+ #before do
7
+ # @app = init_application
8
+ # @english = @app.language('en-US')
9
+ # @tkey = Tr8n::TranslationKey.new({
10
+ # :label => "Hello {user.first_name}",
11
+ # :application => @app,
12
+ # :locale => 'en-US'
13
+ # })
14
+ # @tlabel = @tkey.tokenized_label
15
+ #end
16
+ #
17
+ #describe "initialize" do
18
+ # it "should parse token info" do
19
+ # token = @tlabel.tokens.first
20
+ # expect(token.class.name).to eq("Tr8n::Tokens::Method")
21
+ # expect(token.original_label).to eq(@tlabel.label)
22
+ # expect(token.full_name).to eq("{user.first_name}")
23
+ # expect(token.declared_name).to eq("user.first_name")
24
+ # expect(token.name).to eq("user.first_name")
25
+ # expect(token.sanitized_name).to eq("{user.first_name}")
26
+ # expect(token.name_key).to eq(:"user.first_name")
27
+ # expect(token.pipeless_name).to eq("user.first_name")
28
+ # expect(token.case_key).to be_nil
29
+ # expect(token.supports_cases?).to be_true
30
+ # expect(token.has_case_key?).to be_false
31
+ # expect(token.caseless_name).to eq("user.first_name")
32
+ #
33
+ # expect(token.types).to be_nil
34
+ # expect(token.has_types?).to be_false
35
+ # expect(token.associated_rule_types).to eq([:value])
36
+ # expect(token.language_rule_classes).to eq([Tr8n::Rules::Value])
37
+ # expect(token.transformable_language_rule_classes).to eq([])
38
+ # expect(token.decoration?).to be_false
39
+ # end
40
+ #end
41
+ #
42
+ #describe "substitute" do
43
+ # it "should substitute values" do
44
+ # token = @tlabel.tokens.first
45
+ #
46
+ # user = stub_object({:first_name => "Tom", :last_name => "Anderson", :gender => "Male", :to_s => "Tom Anderson"})
47
+ #
48
+ # # tr("Hello {user}", "", {:user => current_user}}
49
+ # expect(token.token_value(user, {}, @english)).to eq(user.to_s)
50
+ #
51
+ # # tr("Hello {user}", "", {:user => [current_user]}}
52
+ # expect(token.token_value([user], {}, @english)).to eq(user.to_s)
53
+ #
54
+ # # tr("Hello {user}", "", {:user => [current_user, current_user.name]}}
55
+ # expect(token.token_value([user, user.first_name], {}, @english)).to eq(user.first_name)
56
+ #
57
+ # # tr("Hello {user}", "", {:user => [current_user, "{$0} {$1}", "param1"]}}
58
+ # expect(token.token_value([user, "{$0} {$1}", "param1"], {}, @english)).to eq(user.to_s + " param1")
59
+ # expect(token.token_value([user, "{$0} {$1} {$2}", "param1", "param2"], {}, @english)).to eq(user.to_s + " param1 param2")
60
+ #
61
+ # # tr("Hello {user}", "", {:user => [current_user, :name]}}
62
+ # expect(token.token_value([user, :first_name], {}, @english)).to eq(user.first_name)
63
+ #
64
+ # # tr("Hello {user}", "", {:user => [current_user, :method_name, "param1"]}}
65
+ # user.stub(:last_name_with_prefix) {|prefix| "#{prefix} #{user.last_name}"}
66
+ # expect(token.token_value([user, :last_name_with_prefix, 'Mr.'], {}, @english)).to eq("Mr. Anderson")
67
+ #
68
+ # # tr("Hello {user}", "", {:user => [current_user, lambda{|user| user.name}]}}
69
+ # expect(token.token_value([user, lambda{|user| user.to_s}], {}, @english)).to eq(user.to_s)
70
+ #
71
+ # # tr("Hello {user}", "", {:user => [current_user, lambda{|user, param1| user.name}, "param1"]}}
72
+ # expect(token.token_value([user, lambda{|user, param1| user.to_s + " " + param1}, "extra_param1"], {}, @english)).to eq(user.to_s + " extra_param1")
73
+ #
74
+ # # tr("Hello {user}", "", {:user => {:object => current_user, :value => current_user.name}]}}
75
+ # expect(token.token_value({:object => user, :value => user.to_s}, {}, @english)).to eq(user.to_s)
76
+ #
77
+ # # tr("Hello {user}", "", {:user => {:object => current_user, :attribute => :first_name}]}}
78
+ # expect(token.token_value({:object => user, :attribute => :first_name}, {}, @english)).to eq(user.first_name)
79
+ # expect(token.token_value({:object => {:first_name => "Michael"}, :attribute => :first_name}, {}, @english)).to eq("Michael")
80
+ # end
81
+ #end
82
+
83
+
84
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Tokens::Transform do
6
+ #before do
7
+ # @app = init_application
8
+ # @english = @app.language('en-US')
9
+ # @tkey = Tr8n::TranslationKey.new({
10
+ # :label => "You have {count||message}",
11
+ # :application => @app,
12
+ # :locale => 'en-US'
13
+ # })
14
+ # @tlabel = @tkey.tokenized_label
15
+ #end
16
+ #
17
+ #describe "initialize" do
18
+ # it "should parse token info" do
19
+ # token = @tlabel.tokens.first
20
+ # expect(token.class.name).to eq("Tr8n::Tokens::Transform")
21
+ # expect(token.original_label).to eq(@tlabel.label)
22
+ # expect(token.full_name).to eq("{count||message}")
23
+ # expect(token.declared_name).to eq("count||message")
24
+ # expect(token.name).to eq("count")
25
+ # expect(token.sanitized_name).to eq("{count}")
26
+ # expect(token.name_key).to eq(:count)
27
+ # expect(token.pipeless_name).to eq("count")
28
+ # expect(token.case_key).to be_nil
29
+ # expect(token.supports_cases?).to be_true
30
+ # expect(token.has_case_key?).to be_false
31
+ # expect(token.caseless_name).to eq("count")
32
+ #
33
+ # expect(token.types).to be_nil
34
+ # expect(token.has_types?).to be_false
35
+ # expect(token.associated_rule_types).to eq([:number, :value])
36
+ # expect(token.language_rule_classes).to eq([Tr8n::Rules::Number, Tr8n::Rules::Value])
37
+ # expect(token.transformable_language_rule_classes).to eq([Tr8n::Rules::Number])
38
+ # expect(token.decoration?).to be_false
39
+ # end
40
+ #end
41
+ #
42
+ #describe "substitute" do
43
+ # it "should substitute values" do
44
+ # token = @tlabel.tokens.first
45
+ #
46
+ # end
47
+ #end
48
+
49
+
50
+ end
@@ -0,0 +1,96 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::TranslationKey do
6
+ describe "#initialize" do
7
+ before do
8
+ @app = init_application
9
+ @english = @app.language('en-US')
10
+ @russian = @app.language('ru')
11
+ end
12
+
13
+ it "sets attributes" do
14
+ expect(Tr8n::TranslationKey.attributes).to eq([:application, :language, :id, :key, :label, :description, :locale, :level, :locked, :translations])
15
+
16
+ tkey = Tr8n::TranslationKey.new({
17
+ :label => "Hello World",
18
+ :application => @app
19
+ })
20
+ expect(tkey.locale).to eq("en-US")
21
+ expect(tkey.language.locale).to eq("en-US")
22
+
23
+ tkey = Tr8n::TranslationKey.new({
24
+ :label => "Hello World",
25
+ :application => @app,
26
+ :locale => 'en-US'
27
+ })
28
+
29
+ expect(tkey.id).to be_nil
30
+ expect(tkey.label).to eq("Hello World")
31
+ expect(tkey.description).to be_nil
32
+ expect(tkey.key).to eq("d541c79af1be6a05b1f16fca8b5730de")
33
+ expect(tkey.locale).to eq("en-US")
34
+ expect(tkey.language.locale).to eq("en-US")
35
+ expect(tkey.has_translations_for_language?(@russian)).to be_false
36
+ expect(tkey.translations_for_language(@russian)).to eq([])
37
+ end
38
+
39
+ it "translates labels correctly into default language" do
40
+ tkey = Tr8n::TranslationKey.new(:label => "Hello World", :application => @app)
41
+ expect(tkey.substitute_tokens("Hello World", {}, @english)).to eq("Hello World")
42
+
43
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user}", :application => @app)
44
+ expect(tkey.substitute_tokens("Hello {user1}", {:user => "Michael"}, @english)).to eq("Hello {user1}")
45
+
46
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user}", :application => @app)
47
+ expect(tkey.substitute_tokens("Hello {user}", {:user => "Michael"}, @english)).to eq("Hello Michael")
48
+
49
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user1} and {user2}", :application => @app)
50
+ expect(tkey.substitute_tokens("Hello {user1} and {user2}", {:user1 => "Michael" , :user2 => "Tom"}, @english)).to eq("Hello Michael and Tom")
51
+
52
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user}", :application => @app)
53
+ expect(tkey.substitute_tokens("Hello {user}", {:user => {:object => {:name => "Michael"}, :value => "Michael"}}, @english)).to eq("Hello Michael")
54
+
55
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user}", :application => @app)
56
+ expect(tkey.substitute_tokens("Hello {user}", {:user => {:object => {:name => "Michael"}, :attribute => "name"}}, @english)).to eq("Hello Michael")
57
+
58
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user}", :application => @app)
59
+ expect(tkey.substitute_tokens("Hello {user}", {:user => {:object => double(:name => "Michael"), :attribute => "name"}}, @english)).to eq("Hello Michael")
60
+
61
+ tkey = Tr8n::TranslationKey.new(:label => "Hello {user1} [bold: and] {user2}", :application => @app)
62
+ expect(tkey.substitute_tokens("Hello {user1} [bold: and] {user2}", {:user1 => "Michael" , :user2 => "Tom"}, @english)).to eq("Hello Michael <strong>and</strong> Tom")
63
+
64
+ tkey = Tr8n::TranslationKey.new(:label => "You have [link: [bold: {count}] messages]", :application => @app)
65
+ expect(tkey.substitute_tokens("You have [link: [bold: {count}] messages]", {:count => 5, :link => {:href => "www.google.com"}}, @english)).to eq("You have <a href='www.google.com'><strong>5</strong> messages</a>")
66
+
67
+ tkey = Tr8n::TranslationKey.new(:label => "You have [link][bold: {count}] messages[/link]", :application => @app)
68
+ expect(tkey.substitute_tokens("You have [link][bold: {count}] messages[/link]", {:count => 5, :link => {:href => "www.google.com"}}, @english)).to eq("You have <a href='www.google.com'><strong>5</strong> messages</a>")
69
+ end
70
+
71
+ context "labels with numeric rules" do
72
+ it "should return correct translations" do
73
+ key = Tr8n::TranslationKey.new(:label => "You have {count||message}.", :application => @app)
74
+ key.set_language_translations(@russian, [
75
+ Tr8n::Translation.new(:label => "U vas est {count} soobshenie.", :context => {"count" => {"number" => "one"}}),
76
+ Tr8n::Translation.new(:label => "U vas est {count} soobsheniya.", :context => {"count" => {"number" => "few"}}),
77
+ Tr8n::Translation.new(:label => "U vas est {count} soobshenii.", :context => {"count" => {"number" => "many"}}),
78
+ ])
79
+
80
+ expect(key.translate(@russian, {:count => 1})).to eq("U vas est 1 soobshenie.")
81
+ expect(key.translate(@russian, {:count => 101})).to eq("U vas est 101 soobshenie.")
82
+ expect(key.translate(@russian, {:count => 11})).to eq("U vas est 11 soobshenii.")
83
+ expect(key.translate(@russian, {:count => 111})).to eq("U vas est 111 soobshenii.")
84
+
85
+ expect(key.translate(@russian, {:count => 5})).to eq("U vas est 5 soobshenii.")
86
+ expect(key.translate(@russian, {:count => 26})).to eq("U vas est 26 soobshenii.")
87
+ expect(key.translate(@russian, {:count => 106})).to eq("U vas est 106 soobshenii.")
88
+
89
+ expect(key.translate(@russian, {:count => 3})).to eq("U vas est 3 soobsheniya.")
90
+ expect(key.translate(@russian, {:count => 13})).to eq("U vas est 13 soobshenii.")
91
+ expect(key.translate(@russian, {:count => 23})).to eq("U vas est 23 soobsheniya.")
92
+ expect(key.translate(@russian, {:count => 103})).to eq("U vas est 103 soobsheniya.")
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Translation do
6
+ describe "initialize" do
7
+ before do
8
+ @app = init_application
9
+ @english = @app.language('en-US')
10
+ @russian = @app.language('ru')
11
+ end
12
+
13
+ it "sets attributes" do
14
+ expect(Tr8n::Translation.attributes).to eq([:translation_key, :language, :locale, :label, :context, :precedence])
15
+
16
+ t = Tr8n::Translation.new(:label => "You have {count||message}", :context => {"count" => {"number" => "one"}}, :language => @russian)
17
+
18
+ [1, 101, 1001].each do |count|
19
+ expect(t.matches_rules?(:count => count)).to be_true
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Utils do
6
+ describe "helper methods" do
7
+ it "should return correct values" do
8
+ #expect(Tr8n::Utils.root).to eq(File.expand_path("#{__dir__}/../"))
9
+ expect(Tr8n::Utils.load_yaml("#{Tr8n.config.root}/config/tokens/data.yml").class).to eq(Hash)
10
+
11
+ expect(
12
+ Tr8n::Utils.normalize_tr_params("Hello {user}", "Sample label", {:user => "Michael"}, {})
13
+ ).to eq(
14
+ {:label=>"Hello {user}", :description=>"Sample label", :tokens=>{:user=>"Michael"}, :options=>{}}
15
+ )
16
+
17
+ expect(
18
+ Tr8n::Utils.normalize_tr_params("Hello {user}", {:user => "Michael"}, nil, nil)
19
+ ).to eq(
20
+ {:label=>"Hello {user}", :description=>nil, :tokens=>{:user=>"Michael"}, :options=>nil}
21
+ )
22
+
23
+ expect(
24
+ Tr8n::Utils.normalize_tr_params("Hello {user}", {:user => "Michael"}, {:skip_decoration => true}, nil)
25
+ ).to eq(
26
+ {:label=>"Hello {user}", :description=>nil, :tokens=>{:user=>"Michael"}, :options=>{:skip_decoration=>true}}
27
+ )
28
+
29
+ expect(
30
+ Tr8n::Utils.normalize_tr_params({:label=>"Hello {user}", :description=>"Sample label", :tokens=>{:user=>"Michael"}, :options=>{}}, nil, nil, nil)
31
+ ).to eq(
32
+ {:label=>"Hello {user}", :description=>"Sample label", :tokens=>{:user=>"Michael"}, :options=>{}}
33
+ )
34
+
35
+ expect(Tr8n::Utils.guid.class).to be(String)
36
+ end
37
+
38
+ it "should correctly split by sentence" do
39
+
40
+ expect(
41
+ Tr8n::Utils.split_by_sentence("Hello World")
42
+ ).to eq(
43
+ ["Hello World"]
44
+ )
45
+
46
+ expect(
47
+ Tr8n::Utils.split_by_sentence("This is the first sentence. Followed by the second one.")
48
+ ).to eq(
49
+ ["This is the first sentence.", "Followed by the second one."]
50
+ )
51
+
52
+ end
53
+
54
+ it "should correctly sign and verify signature" do
55
+ data = {"name" => "Michael"}
56
+ key = "abc"
57
+
58
+ request = Tr8n::Utils.sign_and_encode_params(data, key)
59
+ result = Tr8n::Utils.decode_and_verify_params(request, key)
60
+ expect(result["name"]).to eq(data["name"])
61
+ end
62
+
63
+ end
64
+ end