translate-rails3-plus 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,12 @@
1
- class Article < ActiveRecord::Base
2
- def validate
3
- # t('li')
4
- errors.add_to_base([t(:'article.key1') + "#{t('article.key2')}"])
5
- I18n.t 'article.key3'
6
- I18n.t 'article.key3'
7
- I18n.t :'article.key4'
8
- I18n.translate :'article.key5'
9
- 'bla bla t' + "blubba bla" + ' foobar'
10
- 'bla bla t ' + "blubba bla" + ' foobar'
11
- end
12
- end
1
+ class Article < ActiveRecord::Base
2
+ def validate
3
+ # t('li')
4
+ errors.add_to_base([t(:'article.key1') + "#{t('article.key2')}"])
5
+ I18n.t 'article.key3'
6
+ I18n.t 'article.key3'
7
+ I18n.t :'article.key4'
8
+ I18n.translate :'article.key5'
9
+ 'bla bla t' + "blubba bla" + ' foobar'
10
+ 'bla bla t ' + "blubba bla" + ' foobar'
11
+ end
12
+ end
@@ -1 +1 @@
1
- <%= t(:'category_erb.key1') %>
1
+ <%= t(:'category_erb.key1') %>
@@ -1 +1 @@
1
- t(:'category_html.key1')
1
+ t(:'category_html.key1')
@@ -1 +1 @@
1
- <%= t(:'category_html_erb.key1') %>
1
+ <%= t(:'category_html_erb.key1') %>
@@ -1,5 +1,5 @@
1
- <script>
2
- document.createElement('li');
3
- </script>
4
-
5
- <%= t(:'category_rhtml.key1') %>
1
+ <script>
2
+ document.createElement('li');
3
+ </script>
4
+
5
+ <%= t(:'category_rhtml.key1') %>
data/spec/keys_spec.rb CHANGED
@@ -1,179 +1,179 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
- require 'fileutils'
3
-
4
- describe Translate::Keys do
5
- before(:each) do
6
- I18n.stub!(:default_locale).and_return(:en)
7
- @keys = Translate::Keys.new
8
- Translate::Storage.stub!(:root_dir).and_return(i18n_files_dir)
9
- end
10
-
11
- describe "to_a" do
12
- it "extracts keys from I18n lookups in .rb, .html.erb, and .rhtml files" do
13
- @keys.to_a.map(&:to_s).sort.should == ['article.key1', 'article.key2', 'article.key3', 'article.key4', 'article.key5',
14
- 'category_erb.key1', 'category_html_erb.key1', 'category_rhtml.key1', 'js.alert']
15
- end
16
- end
17
-
18
- describe "to_hash" do
19
- it "return a hash with I18n keys and file lists" do
20
- @keys.to_hash[:'article.key3'].should == ["vendor/plugins/translate/spec/files/translate/app/models/article.rb"]
21
- end
22
- end
23
-
24
- describe "i18n_keys" do
25
- before(:each) do
26
- I18n.backend.send(:init_translations) unless I18n.backend.initialized?
27
- end
28
-
29
- it "should return all keys in the I18n backend translations hash" do
30
- I18n.backend.should_receive(:translations).and_return(translations)
31
- @keys.i18n_keys(:en).should == ['articles.new.page_title', 'categories.flash.created', 'empty', 'home.about']
32
- end
33
-
34
- describe "untranslated_keys" do
35
- before(:each) do
36
- I18n.backend.stub!(:translations).and_return(translations)
37
- end
38
-
39
- it "should return a hash with keys with missing translations in each locale" do
40
- @keys.untranslated_keys.should == {
41
- :sv => ['articles.new.page_title', 'categories.flash.created', 'empty']
42
- }
43
- end
44
- end
45
-
46
- describe "missing_keys" do
47
- before(:each) do
48
- @file_path = File.join(i18n_files_dir, "config", "locales", "en.yml")
49
- Translate::File.new(@file_path).write({
50
- :en => {
51
- :home => {
52
- :page_title => false,
53
- :intro => {
54
- :one => "intro one",
55
- :other => "intro other"
56
- }
57
- }
58
- }
59
- })
60
- end
61
-
62
- after(:each) do
63
- FileUtils.rm(@file_path)
64
- end
65
-
66
- it "should return a hash with keys that are not in the locale file" do
67
- @keys.stub!(:files).and_return({
68
- :'home.page_title' => "app/views/home/index.rhtml",
69
- :'home.intro' => 'app/views/home/index.rhtml',
70
- :'home.signup' => "app/views/home/_signup.rhtml",
71
- :'about.index.page_title' => "app/views/about/index.rhtml"
72
- })
73
- @keys.missing_keys.should == {
74
- :'home.signup' => "app/views/home/_signup.rhtml",
75
- :'about.index.page_title' => "app/views/about/index.rhtml"
76
- }
77
- end
78
- end
79
-
80
- describe "contains_key?" do
81
- it "works" do
82
- hash = {
83
- :foo => {
84
- :bar => {
85
- :baz => false
86
- }
87
- }
88
- }
89
- Translate::Keys.contains_key?(hash, "").should be_false
90
- Translate::Keys.contains_key?(hash, "foo").should be_true
91
- Translate::Keys.contains_key?(hash, "foo.bar").should be_true
92
- Translate::Keys.contains_key?(hash, "foo.bar.baz").should be_true
93
- Translate::Keys.contains_key?(hash, :"foo.bar.baz").should be_true
94
- Translate::Keys.contains_key?(hash, "foo.bar.baz.bla").should be_false
95
- end
96
- end
97
-
98
- describe "translated_locales" do
99
- before(:each) do
100
- I18n.stub!(:default_locale).and_return(:en)
101
- I18n.stub!(:available_locales).and_return([:sv, :no, :en, :root])
102
- end
103
-
104
- it "returns all avaiable except :root and the default" do
105
- Translate::Keys.translated_locales.should == [:sv, :no]
106
- end
107
- end
108
-
109
- describe "to_deep_hash" do
110
- it "convert shallow hash with dot separated keys to deep hash" do
111
- Translate::Keys.to_deep_hash(shallow_hash).should == deep_hash
112
- end
113
- end
114
-
115
- describe "to_shallow_hash" do
116
- it "converts a deep hash to a shallow one" do
117
- Translate::Keys.to_shallow_hash(deep_hash).should == shallow_hash
118
- end
119
- end
120
-
121
- ##########################################################################
122
- #
123
- # Helper Methods
124
- #
125
- ##########################################################################
126
-
127
- def translations
128
- {
129
- :en => {
130
- :home => {
131
- :about => "This site is about making money"
132
- },
133
- :articles => {
134
- :new => {
135
- :page_title => "New Article"
136
- }
137
- },
138
- :categories => {
139
- :flash => {
140
- :created => "Category created"
141
- }
142
- },
143
- :empty => nil
144
- },
145
- :sv => {
146
- :home => {
147
- :about => false
148
- }
149
- }
150
- }
151
- end
152
- end
153
-
154
- def shallow_hash
155
- {
156
- 'pressrelease.label.one' => "Pressmeddelande",
157
- 'pressrelease.label.other' => "Pressmeddelanden",
158
- 'article' => "Artikel",
159
- 'category' => ''
160
- }
161
- end
162
-
163
- def deep_hash
164
- {
165
- :pressrelease => {
166
- :label => {
167
- :one => "Pressmeddelande",
168
- :other => "Pressmeddelanden"
169
- }
170
- },
171
- :article => "Artikel",
172
- :category => ''
173
- }
174
- end
175
-
176
- def i18n_files_dir
177
- File.join(ENV['PWD'], "spec", "files", "translate")
178
- end
179
- end
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe Translate::Keys do
5
+ before(:each) do
6
+ I18n.stub!(:default_locale).and_return(:en)
7
+ @keys = Translate::Keys.new
8
+ Translate::Storage.stub!(:root_dir).and_return(i18n_files_dir)
9
+ end
10
+
11
+ describe "to_a" do
12
+ it "extracts keys from I18n lookups in .rb, .html.erb, and .rhtml files" do
13
+ @keys.to_a.map(&:to_s).sort.should == ['article.key1', 'article.key2', 'article.key3', 'article.key4', 'article.key5',
14
+ 'category_erb.key1', 'category_html_erb.key1', 'category_rhtml.key1', 'js.alert']
15
+ end
16
+ end
17
+
18
+ describe "to_hash" do
19
+ it "return a hash with I18n keys and file lists" do
20
+ @keys.to_hash[:'article.key3'].should == ["vendor/plugins/translate/spec/files/translate/app/models/article.rb"]
21
+ end
22
+ end
23
+
24
+ describe "i18n_keys" do
25
+ before(:each) do
26
+ I18n.backend.send(:init_translations) unless I18n.backend.initialized?
27
+ end
28
+
29
+ it "should return all keys in the I18n backend translations hash" do
30
+ I18n.backend.should_receive(:translations).and_return(translations)
31
+ @keys.i18n_keys(:en).should == ['articles.new.page_title', 'categories.flash.created', 'empty', 'home.about']
32
+ end
33
+
34
+ describe "untranslated_keys" do
35
+ before(:each) do
36
+ I18n.backend.stub!(:translations).and_return(translations)
37
+ end
38
+
39
+ it "should return a hash with keys with missing translations in each locale" do
40
+ @keys.untranslated_keys.should == {
41
+ :sv => ['articles.new.page_title', 'categories.flash.created', 'empty']
42
+ }
43
+ end
44
+ end
45
+
46
+ describe "missing_keys" do
47
+ before(:each) do
48
+ @file_path = File.join(i18n_files_dir, "config", "locales", "en.yml")
49
+ Translate::File.new(@file_path).write({
50
+ :en => {
51
+ :home => {
52
+ :page_title => false,
53
+ :intro => {
54
+ :one => "intro one",
55
+ :other => "intro other"
56
+ }
57
+ }
58
+ }
59
+ })
60
+ end
61
+
62
+ after(:each) do
63
+ FileUtils.rm(@file_path)
64
+ end
65
+
66
+ it "should return a hash with keys that are not in the locale file" do
67
+ @keys.stub!(:files).and_return({
68
+ :'home.page_title' => "app/views/home/index.rhtml",
69
+ :'home.intro' => 'app/views/home/index.rhtml',
70
+ :'home.signup' => "app/views/home/_signup.rhtml",
71
+ :'about.index.page_title' => "app/views/about/index.rhtml"
72
+ })
73
+ @keys.missing_keys.should == {
74
+ :'home.signup' => "app/views/home/_signup.rhtml",
75
+ :'about.index.page_title' => "app/views/about/index.rhtml"
76
+ }
77
+ end
78
+ end
79
+
80
+ describe "contains_key?" do
81
+ it "works" do
82
+ hash = {
83
+ :foo => {
84
+ :bar => {
85
+ :baz => false
86
+ }
87
+ }
88
+ }
89
+ Translate::Keys.contains_key?(hash, "").should be_false
90
+ Translate::Keys.contains_key?(hash, "foo").should be_true
91
+ Translate::Keys.contains_key?(hash, "foo.bar").should be_true
92
+ Translate::Keys.contains_key?(hash, "foo.bar.baz").should be_true
93
+ Translate::Keys.contains_key?(hash, :"foo.bar.baz").should be_true
94
+ Translate::Keys.contains_key?(hash, "foo.bar.baz.bla").should be_false
95
+ end
96
+ end
97
+
98
+ describe "translated_locales" do
99
+ before(:each) do
100
+ I18n.stub!(:default_locale).and_return(:en)
101
+ I18n.stub!(:available_locales).and_return([:sv, :no, :en, :root])
102
+ end
103
+
104
+ it "returns all avaiable except :root and the default" do
105
+ Translate::Keys.translated_locales.should == [:sv, :no]
106
+ end
107
+ end
108
+
109
+ describe "to_deep_hash" do
110
+ it "convert shallow hash with dot separated keys to deep hash" do
111
+ Translate::Keys.to_deep_hash(shallow_hash).should == deep_hash
112
+ end
113
+ end
114
+
115
+ describe "to_shallow_hash" do
116
+ it "converts a deep hash to a shallow one" do
117
+ Translate::Keys.to_shallow_hash(deep_hash).should == shallow_hash
118
+ end
119
+ end
120
+
121
+ ##########################################################################
122
+ #
123
+ # Helper Methods
124
+ #
125
+ ##########################################################################
126
+
127
+ def translations
128
+ {
129
+ :en => {
130
+ :home => {
131
+ :about => "This site is about making money"
132
+ },
133
+ :articles => {
134
+ :new => {
135
+ :page_title => "New Article"
136
+ }
137
+ },
138
+ :categories => {
139
+ :flash => {
140
+ :created => "Category created"
141
+ }
142
+ },
143
+ :empty => nil
144
+ },
145
+ :sv => {
146
+ :home => {
147
+ :about => false
148
+ }
149
+ }
150
+ }
151
+ end
152
+ end
153
+
154
+ def shallow_hash
155
+ {
156
+ 'pressrelease.label.one' => "Pressmeddelande",
157
+ 'pressrelease.label.other' => "Pressmeddelanden",
158
+ 'article' => "Artikel",
159
+ 'category' => ''
160
+ }
161
+ end
162
+
163
+ def deep_hash
164
+ {
165
+ :pressrelease => {
166
+ :label => {
167
+ :one => "Pressmeddelande",
168
+ :other => "Pressmeddelanden"
169
+ }
170
+ },
171
+ :article => "Artikel",
172
+ :category => ''
173
+ }
174
+ end
175
+
176
+ def i18n_files_dir
177
+ File.join(ENV['PWD'], "spec", "files", "translate")
178
+ end
179
+ end
data/spec/log_spec.rb CHANGED
@@ -1,47 +1,47 @@
1
- require 'fileutils'
2
- require File.dirname(__FILE__) + '/spec_helper'
3
-
4
- describe Translate::Log do
5
- describe "write_to_file" do
6
- before(:each) do
7
- I18n.locale = :sv
8
- I18n.backend.store_translations(:sv, from_texts)
9
- keys = Translate::Keys.new
10
- @log = Translate::Log.new(:sv, :en, Translate::Keys.to_shallow_hash(from_texts).keys)
11
- @log.stub!(:file_path).and_return(file_path)
12
- FileUtils.rm_f file_path
13
- end
14
-
15
- after(:each) do
16
- FileUtils.rm_f file_path
17
- end
18
-
19
- it "writes new log file with from texts" do
20
- File.exists?(file_path).should be_false
21
- @log.write_to_file
22
- File.exists?(file_path).should be_true
23
- Translate::File.new(file_path).read.should == Translate::File.deep_stringify_keys(from_texts)
24
- end
25
-
26
- it "merges from texts with current texts in log file and re-writes the log file" do
27
- @log.write_to_file
28
- I18n.backend.store_translations(:sv, {:category => "Kategori ny"})
29
- @log.keys = ['category']
30
- @log.write_to_file
31
- Translate::File.new(file_path).read['category'].should == "Kategori ny"
32
- end
33
-
34
- def file_path
35
- File.join(File.dirname(__FILE__), "files", "from_sv_to_en.yml")
36
- end
37
-
38
- def from_texts
39
- {
40
- :article => {
41
- :title => "En artikel"
42
- },
43
- :category => "Kategori"
44
- }
45
- end
46
- end
47
- end
1
+ require 'fileutils'
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Translate::Log do
5
+ describe "write_to_file" do
6
+ before(:each) do
7
+ I18n.locale = :sv
8
+ I18n.backend.store_translations(:sv, from_texts)
9
+ keys = Translate::Keys.new
10
+ @log = Translate::Log.new(:sv, :en, Translate::Keys.to_shallow_hash(from_texts).keys)
11
+ @log.stub!(:file_path).and_return(file_path)
12
+ FileUtils.rm_f file_path
13
+ end
14
+
15
+ after(:each) do
16
+ FileUtils.rm_f file_path
17
+ end
18
+
19
+ it "writes new log file with from texts" do
20
+ File.exists?(file_path).should be_false
21
+ @log.write_to_file
22
+ File.exists?(file_path).should be_true
23
+ Translate::File.new(file_path).read.should == Translate::File.deep_stringify_keys(from_texts)
24
+ end
25
+
26
+ it "merges from texts with current texts in log file and re-writes the log file" do
27
+ @log.write_to_file
28
+ I18n.backend.store_translations(:sv, {:category => "Kategori ny"})
29
+ @log.keys = ['category']
30
+ @log.write_to_file
31
+ Translate::File.new(file_path).read['category'].should == "Kategori ny"
32
+ end
33
+
34
+ def file_path
35
+ File.join(File.dirname(__FILE__), "files", "from_sv_to_en.yml")
36
+ end
37
+
38
+ def from_texts
39
+ {
40
+ :article => {
41
+ :title => "En artikel"
42
+ },
43
+ :category => "Kategori"
44
+ }
45
+ end
46
+ end
47
+ end