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