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
data/spec/base_spec.rb ADDED
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Base do
6
+ describe "hash value method" do
7
+ it "must return correct values" do
8
+ expect(Tr8n::Base.hash_value({"a" => "b"}, "a")).to eq("b")
9
+ expect(Tr8n::Base.hash_value({:a => "b"}, "a")).to eq("b")
10
+ expect(Tr8n::Base.hash_value({:a => "b"}, :a)).to eq("b")
11
+ expect(Tr8n::Base.hash_value({"a" => "b"}, :a)).to eq("b")
12
+
13
+ expect(Tr8n::Base.hash_value({"a" => {:b => "c"}}, "a.b")).to eq("c")
14
+ expect(Tr8n::Base.hash_value({:a => {:b => "c"}}, "a.b")).to eq("c")
15
+ expect(Tr8n::Base.hash_value({:a => {:b => "c"}}, "a.d")).to be_nil
16
+ expect(Tr8n::Base.hash_value({:a => {:b => {:c => :d}}}, "a.b.c")).to eq(:d)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Config do
6
+ describe "loading defaults" do
7
+ it "should load correct values" do
8
+ expect(Tr8n.config.logger_enabled?).to be_true
9
+ expect(Tr8n.config.enabled?).to be_true
10
+ expect(Tr8n.config.default_locale).to eq("en-US")
11
+ expect(Tr8n.config.cache_enabled?).to be_false
12
+ expect(Tr8n.config.log_path).to eq("./log/tr8n.log")
13
+ expect(Tr8n.config.cache_adapter).to eq("file")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ require 'helper'
2
+
3
+ describe Tr8n::Decorators::Default do
4
+ describe "#initialize" do
5
+ it "sets language attributes" do
6
+ # decorator = Tr8n::Decorators::Default.new(:language => "a")
7
+ # pp decorator
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Decorators::Base do
6
+ describe "creating new decorator" do
7
+ it "must return a decorator specified in the config file" do
8
+ decor = Tr8n::Decorators::Base.decorator
9
+ expect(decor.class.name).to eq("Tr8n::Decorators::Default")
10
+
11
+ expect{Tr8n::Decorators::Base.new.decorate(nil, nil, nil, nil)}.to raise_error(Tr8n::Exception)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Decorators::Default do
6
+ describe "default decorator" do
7
+ it "should return label as is without applying decorations" do
8
+ decor = Tr8n::Decorators::Default.new
9
+ expect(decor.decorate("Hello World", nil, nil, nil)).to eq("Hello World")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'helper'
4
+
5
+ describe Tr8n::Decorators::Html do
6
+ describe "html decorator" do
7
+ it "should decorate the label according to the options" do
8
+ decor = Tr8n::Decorators::Html.new
9
+
10
+ app = init_application
11
+ en = app.language('en-US')
12
+ ru = app.language('ru')
13
+ es = app.language('es')
14
+
15
+ tkey = Tr8n::TranslationKey.new({
16
+ :label => "Hello World",
17
+ :application => app,
18
+ :locale => 'en-US'
19
+ })
20
+
21
+ expect(decor.decorate("Hello World", en, en, tkey, {:skip_decorations => true})).to eq("Hello World")
22
+ Tr8n.config.current_translator = nil
23
+ expect(decor.decorate("Hello World", en, en, tkey)).to eq("Hello World")
24
+
25
+ Tr8n.config.current_translator = Tr8n::Translator.new
26
+ expect(decor.decorate("Hello World", en, en, tkey)).to eq("Hello World")
27
+
28
+ Tr8n.config.current_translator = Tr8n::Translator.new
29
+ Tr8n.config.current_translator.inline = false
30
+ expect(decor.decorate("Hello World", en, en, tkey)).to eq("Hello World")
31
+ Tr8n.config.current_translator.inline = true
32
+ expect(decor.decorate("Hello World", en, en, tkey)).to eq("Hello World")
33
+ expect(decor.decorate("Privet Mir", ru, ru, tkey)).to eq("<span class='tr8n_pending'>Privet Mir</span>")
34
+ expect(decor.decorate("Privet Mir", ru, ru, tkey, {:use_div => true})).to eq("<div class='tr8n_pending'>Privet Mir</div>")
35
+
36
+ tkey.id = 5
37
+ expect(decor.decorate("Hello World", en, ru, tkey)).to eq("<span class='tr8n_translatable tr8n_not_translated' data-translation_key_id='5'>Hello World</span>")
38
+ expect(decor.decorate("Privet Mir", ru, ru, tkey)).to eq("<span class='tr8n_translatable tr8n_translated' data-translation_key_id='5'>Privet Mir</span>")
39
+
40
+ expect(decor.decorate("Hola Mir", es, ru, tkey)).to eq("<span class='tr8n_translatable tr8n_fallback' data-translation_key_id='5'>Hola Mir</span>")
41
+
42
+ tkey.locked = true
43
+ expect(decor.decorate("Privet Mir", ru, ru, tkey)).to eq("Privet Mir")
44
+
45
+ Tr8n.config.current_translator.features = {"show_locked_keys" => true}
46
+ Tr8n.config.current_translator.manager = true
47
+ expect(decor.decorate("Privet Mir", ru, ru, tkey)).to eq("<span class='tr8n_translatable tr8n_locked' data-translation_key_id='5'>Privet Mir</span>")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,112 @@
1
+ {
2
+ "key":"default",
3
+ "name":"Tr8n Translation Service",
4
+ "description":"",
5
+ "default_locale":"en-US",
6
+ "languages":[
7
+ {
8
+ "locale":"da",
9
+ "name":"Danske",
10
+ "english_name":"Danish",
11
+ "native_name":"Danske",
12
+ "flag_url":"http://localhost:3000/media/22/6/63/49/2d1982f07d7e7c2b0.png"
13
+ },
14
+ {
15
+ "locale":"nl",
16
+ "name":"Flemish",
17
+ "english_name":"Dutch",
18
+ "native_name":"Flemish",
19
+ "flag_url":"http://localhost:3000/media/5/2/45/67/0915799fae203af51.png"
20
+ },
21
+ {
22
+ "locale":"en-US",
23
+ "name":"English (US)",
24
+ "english_name":"English (US)",
25
+ "native_name":"English (US)",
26
+ "flag_url":"http://localhost:3000/media/57/23/20/19/cf185dab9566b718e.png"
27
+ },
28
+ {
29
+ "locale":"et",
30
+ "name":"Eesti",
31
+ "english_name":"Estonian",
32
+ "native_name":"Eesti",
33
+ "flag_url":"http://localhost:3000/media/71/5/89/63/5f40bc0a610274b28.png"
34
+ },
35
+ {
36
+ "locale":"fr",
37
+ "name":"Français",
38
+ "english_name":"French",
39
+ "native_name":"Français",
40
+ "flag_url":"http://localhost:3000/media/22/5/6/87/e52809adf86d81215.png"
41
+ },
42
+ {
43
+ "locale":"de",
44
+ "name":"Deutsch",
45
+ "english_name":"German",
46
+ "native_name":"Deutsch",
47
+ "flag_url":"http://localhost:3000/media/5/11/98/10/bc99d7cc527101e6d.png"
48
+ },
49
+ {
50
+ "locale":"he",
51
+ "name":"עברית",
52
+ "english_name":"Hebrew",
53
+ "native_name":"עברית",
54
+ "right_to_left":true,
55
+ "flag_url":"http://localhost:3000/media/42/62/44/29/8720a6f403329ec30.png"
56
+ },
57
+ {
58
+ "locale":"no",
59
+ "name":"Norsk (bokmål)",
60
+ "english_name":"Norwegian",
61
+ "native_name":"Norsk (bokmål)",
62
+ "flag_url":"http://localhost:3000/media/23/60/15/78/a3c7c6e81482ffa63.png"
63
+ },
64
+ {
65
+ "locale":"ru",
66
+ "name":"Русский",
67
+ "english_name":"Russian",
68
+ "native_name":"Русский",
69
+ "flag_url":"http://localhost:3000/media/54/39/11/49/cb768f030bde97f97.png"
70
+ },
71
+ {
72
+ "locale":"es",
73
+ "name":"Español",
74
+ "english_name":"Spanish",
75
+ "native_name":"Español",
76
+ "flag_url":"http://localhost:3000/media/52/47/56/19/6594b760c14053ef4.png"
77
+ },
78
+ {
79
+ "locale":"sv",
80
+ "name":"Svenska",
81
+ "english_name":"Swedish",
82
+ "native_name":"Svenska",
83
+ "flag_url":"http://localhost:3000/media/38/75/53/53/89753a508d8179af1.png"
84
+ }
85
+ ],
86
+ "threshold":1,
87
+ "translator_level":1,
88
+ "features":{
89
+ "inline_translations":true,
90
+ "google_suggestions":true,
91
+ "decorations":true,
92
+ "shortcuts":true,
93
+ "context_rules":true,
94
+ "language_cases":true,
95
+ "javascript_sdk":false,
96
+ "glossary":true,
97
+ "forum":true,
98
+ "awards":true,
99
+ "admin_translations":true,
100
+ "open_registration":false,
101
+ "language_flags":true
102
+ },
103
+ "tokens":{
104
+ "data":{
105
+ "nbsp":"&nbsp;"
106
+ },
107
+ "decoration":{
108
+ "link":"<a href=\"{$href}\">{$0}</a>",
109
+ "strong":"<strong>{$0}</strong>"
110
+ }
111
+ }
112
+ }