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.
data/lib/translate/log.rb CHANGED
@@ -1,35 +1,35 @@
1
- class Translate::Log
2
- attr_accessor :from_locale, :to_locale, :keys
3
-
4
- def initialize(from_locale, to_locale, keys)
5
- self.from_locale = from_locale
6
- self.to_locale = to_locale
7
- self.keys = keys
8
- end
9
-
10
- def write_to_file
11
- current_texts = File.exists?(file_path) ? file.read : {}
12
- current_texts.merge!(from_texts)
13
- file.write(current_texts)
14
- end
15
-
16
- def read
17
- file.read
18
- end
19
-
20
- private
21
- def file
22
- @file ||= Translate::File.new(file_path)
23
- end
24
-
25
- def from_texts
26
- Translate::File.deep_stringify_keys(Translate::Keys.to_deep_hash(keys.inject({}) do |hash, key|
27
- hash[key] = I18n.backend.send(:lookup, from_locale, key)
28
- hash
29
- end))
30
- end
31
-
32
- def file_path
33
- File.join(Rails.root, "config", "locales", "log", "from_#{from_locale}_to_#{to_locale}.yml")
34
- end
35
- end
1
+ class Translate::Log
2
+ attr_accessor :from_locale, :to_locale, :keys
3
+
4
+ def initialize(from_locale, to_locale, keys)
5
+ self.from_locale = from_locale
6
+ self.to_locale = to_locale
7
+ self.keys = keys
8
+ end
9
+
10
+ def write_to_file
11
+ current_texts = File.exists?(file_path) ? file.read : {}
12
+ current_texts.merge!(from_texts)
13
+ file.write(current_texts)
14
+ end
15
+
16
+ def read
17
+ file.read
18
+ end
19
+
20
+ private
21
+ def file
22
+ @file ||= Translate::File.new(file_path)
23
+ end
24
+
25
+ def from_texts
26
+ Translate::File.deep_stringify_keys(Translate::Keys.to_deep_hash(keys.inject({}) do |hash, key|
27
+ hash[key] = I18n.backend.send(:lookup, from_locale, key)
28
+ hash
29
+ end))
30
+ end
31
+
32
+ def file_path
33
+ File.join(Rails.root, "config", "locales", "log", "from_#{from_locale}_to_#{to_locale}.yml")
34
+ end
35
+ end
@@ -1,11 +1,11 @@
1
- module Translate
2
- class Routes
3
- def self.translation_ui(map)
4
- map.with_options(:controller => 'translate') do |t|
5
- t.translate_list 'translate'
6
- t.translate 'translate/translate', :action => 'translate'
7
- t.translate_reload 'translate/reload', :action => 'reload'
8
- end
9
- end
10
- end
11
- end
1
+ module Translate
2
+ class Routes
3
+ def self.translation_ui(map)
4
+ map.with_options(:controller => 'translate') do |t|
5
+ t.translate_list 'translate'
6
+ t.translate 'translate/translate', :action => 'translate'
7
+ t.translate_reload 'translate/reload', :action => 'reload'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,28 +1,28 @@
1
- class Translate::Storage
2
- attr_accessor :locale
3
-
4
- def initialize(locale)
5
- self.locale = locale.to_sym
6
- end
7
-
8
- def write_to_file
9
- Translate::File.new(file_path).write(keys)
10
- end
11
-
12
- def self.file_paths(locale)
13
- Dir.glob(File.join(root_dir, "config", "locales", "**","#{locale}.yml"))
14
- end
15
-
16
- def self.root_dir
17
- Rails.root
18
- end
19
-
20
- private
21
- def keys
22
- {locale => I18n.backend.send(:translations)[locale]}
23
- end
24
-
25
- def file_path
26
- File.join(Translate::Storage.root_dir, "config", "locales", "#{locale}.yml")
27
- end
28
- end
1
+ class Translate::Storage
2
+ attr_accessor :locale
3
+
4
+ def initialize(locale)
5
+ self.locale = locale.to_sym
6
+ end
7
+
8
+ def write_to_file
9
+ Translate::File.new(file_path).write(keys)
10
+ end
11
+
12
+ def self.file_paths(locale)
13
+ Dir.glob(File.join(root_dir, "config", "locales", "**","#{locale}.yml"))
14
+ end
15
+
16
+ def self.root_dir
17
+ Rails.root
18
+ end
19
+
20
+ private
21
+ def keys
22
+ {locale => I18n.backend.send(:translations)[locale]}
23
+ end
24
+
25
+ def file_path
26
+ File.join(Translate::Storage.root_dir, "config", "locales", "#{locale}.yml")
27
+ end
28
+ end
@@ -1,130 +1,130 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
-
4
- describe TranslateController do
5
- describe "index" do
6
- before(:each) do
7
- controller.stub!(:per_page).and_return(1)
8
- I18n.backend.stub!(:translations).and_return(i18n_translations)
9
- I18n.backend.instance_eval { @initialized = true }
10
- keys = mock(:keys)
11
- keys.stub!(:i18n_keys).and_return(['vendor.foobar'])
12
- Translate::Keys.should_receive(:new).and_return(keys)
13
- Translate::Keys.should_receive(:files).and_return(files)
14
- I18n.stub!(:available_locales).and_return([:en, :sv])
15
- I18n.stub!(:default_locale).and_return(:sv)
16
- end
17
-
18
- it "shows sorted paginated keys from the translate from locale and extracted keys by default" do
19
- get_page :index
20
- assigns(:from_locale).should == :sv
21
- assigns(:to_locale).should == :en
22
- assigns(:files).should == files
23
- assigns(:keys).sort.should == ['articles.new.page_title', 'home.page_title', 'vendor.foobar']
24
- assigns(:paginated_keys).should == ['articles.new.page_title']
25
- end
26
-
27
- it "can be paginated with the page param" do
28
- get_page :index, :page => 2
29
- assigns(:files).should == files
30
- assigns(:paginated_keys).should == ['home.page_title']
31
- end
32
-
33
- it "accepts a key_pattern param with key_type=starts_with" do
34
- get_page :index, :key_pattern => 'articles', :key_type => 'starts_with'
35
- assigns(:files).should == files
36
- assigns(:paginated_keys).should == ['articles.new.page_title']
37
- assigns(:total_entries).should == 1
38
- end
39
-
40
- it "accepts a key_pattern param with key_type=contains" do
41
- get_page :index, :key_pattern => 'page_', :key_type => 'contains'
42
- assigns(:files).should == files
43
- assigns(:total_entries).should == 2
44
- assigns(:paginated_keys).should == ['articles.new.page_title']
45
- end
46
-
47
- it "accepts a filter=untranslated param" do
48
- get_page :index, :filter => 'untranslated'
49
- assigns(:total_entries).should == 2
50
- assigns(:paginated_keys).should == ['articles.new.page_title']
51
- end
52
-
53
- it "accepts a filter=translated param" do
54
- get_page :index, :filter => 'translated'
55
- assigns(:total_entries).should == 1
56
- assigns(:paginated_keys).should == ['vendor.foobar']
57
- end
58
-
59
- it "accepts a filter=changed param" do
60
- log = mock(:log)
61
- old_translations = {:home => {:page_title => "Skapar ny artikel"}}
62
- log.should_receive(:read).and_return(Translate::File.deep_stringify_keys(old_translations))
63
- Translate::Log.should_receive(:new).with(:sv, :en, {}).and_return(log)
64
- get_page :index, :filter => 'changed'
65
- assigns(:total_entries).should == 1
66
- assigns(:keys).should == ["home.page_title"]
67
- end
68
-
69
- def i18n_translations
70
- HashWithIndifferentAccess.new({
71
- :en => {
72
- :vendor => {
73
- :foobar => "Foo Baar"
74
- }
75
- },
76
- :sv => {
77
- :articles => {
78
- :new => {
79
- :page_title => "Skapa ny artikel"
80
- }
81
- },
82
- :home => {
83
- :page_title => "Välkommen till I18n"
84
- },
85
- :vendor => {
86
- :foobar => "Fobar"
87
- }
88
- }
89
- })
90
- end
91
-
92
- def files
93
- HashWithIndifferentAccess.new({
94
- :'home.page_title' => ["app/views/home/index.rhtml"],
95
- :'general.back' => ["app/views/articles/new.rhtml", "app/views/categories/new.rhtml"],
96
- :'articles.new.page_title' => ["app/views/articles/new.rhtml"]
97
- })
98
- end
99
- end
100
-
101
- describe "translate" do
102
- it "should store translations to I18n backend and then write them to a YAML file" do
103
- session[:from_locale] = :sv
104
- session[:to_locale] = :en
105
- translations = {
106
- :articles => {
107
- :new => {
108
- :title => "New Article"
109
- }
110
- },
111
- :category => "Category"
112
- }
113
- key_param = {'articles.new.title' => "New Article", "category" => "Category"}
114
- I18n.backend.should_receive(:store_translations).with(:en, translations)
115
- storage = mock(:storage)
116
- storage.should_receive(:write_to_file)
117
- Translate::Storage.should_receive(:new).with(:en).and_return(storage)
118
- log = mock(:log)
119
- log.should_receive(:write_to_file)
120
- Translate::Log.should_receive(:new).with(:sv, :en, key_param.keys).and_return(log)
121
- post :translate, "key" => key_param
122
- response.should be_redirect
123
- end
124
- end
125
-
126
- def get_page(*args)
127
- get(*args)
128
- response.should be_success
129
- end
130
- end
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe TranslateController do
5
+ describe "index" do
6
+ before(:each) do
7
+ controller.stub!(:per_page).and_return(1)
8
+ I18n.backend.stub!(:translations).and_return(i18n_translations)
9
+ I18n.backend.instance_eval { @initialized = true }
10
+ keys = mock(:keys)
11
+ keys.stub!(:i18n_keys).and_return(['vendor.foobar'])
12
+ Translate::Keys.should_receive(:new).and_return(keys)
13
+ Translate::Keys.should_receive(:files).and_return(files)
14
+ I18n.stub!(:available_locales).and_return([:en, :sv])
15
+ I18n.stub!(:default_locale).and_return(:sv)
16
+ end
17
+
18
+ it "shows sorted paginated keys from the translate from locale and extracted keys by default" do
19
+ get_page :index
20
+ assigns(:from_locale).should == :sv
21
+ assigns(:to_locale).should == :en
22
+ assigns(:files).should == files
23
+ assigns(:keys).sort.should == ['articles.new.page_title', 'home.page_title', 'vendor.foobar']
24
+ assigns(:paginated_keys).should == ['articles.new.page_title']
25
+ end
26
+
27
+ it "can be paginated with the page param" do
28
+ get_page :index, :page => 2
29
+ assigns(:files).should == files
30
+ assigns(:paginated_keys).should == ['home.page_title']
31
+ end
32
+
33
+ it "accepts a key_pattern param with key_type=starts_with" do
34
+ get_page :index, :key_pattern => 'articles', :key_type => 'starts_with'
35
+ assigns(:files).should == files
36
+ assigns(:paginated_keys).should == ['articles.new.page_title']
37
+ assigns(:total_entries).should == 1
38
+ end
39
+
40
+ it "accepts a key_pattern param with key_type=contains" do
41
+ get_page :index, :key_pattern => 'page_', :key_type => 'contains'
42
+ assigns(:files).should == files
43
+ assigns(:total_entries).should == 2
44
+ assigns(:paginated_keys).should == ['articles.new.page_title']
45
+ end
46
+
47
+ it "accepts a filter=untranslated param" do
48
+ get_page :index, :filter => 'untranslated'
49
+ assigns(:total_entries).should == 2
50
+ assigns(:paginated_keys).should == ['articles.new.page_title']
51
+ end
52
+
53
+ it "accepts a filter=translated param" do
54
+ get_page :index, :filter => 'translated'
55
+ assigns(:total_entries).should == 1
56
+ assigns(:paginated_keys).should == ['vendor.foobar']
57
+ end
58
+
59
+ it "accepts a filter=changed param" do
60
+ log = mock(:log)
61
+ old_translations = {:home => {:page_title => "Skapar ny artikel"}}
62
+ log.should_receive(:read).and_return(Translate::File.deep_stringify_keys(old_translations))
63
+ Translate::Log.should_receive(:new).with(:sv, :en, {}).and_return(log)
64
+ get_page :index, :filter => 'changed'
65
+ assigns(:total_entries).should == 1
66
+ assigns(:keys).should == ["home.page_title"]
67
+ end
68
+
69
+ def i18n_translations
70
+ HashWithIndifferentAccess.new({
71
+ :en => {
72
+ :vendor => {
73
+ :foobar => "Foo Baar"
74
+ }
75
+ },
76
+ :sv => {
77
+ :articles => {
78
+ :new => {
79
+ :page_title => "Skapa ny artikel"
80
+ }
81
+ },
82
+ :home => {
83
+ :page_title => "Välkommen till I18n"
84
+ },
85
+ :vendor => {
86
+ :foobar => "Fobar"
87
+ }
88
+ }
89
+ })
90
+ end
91
+
92
+ def files
93
+ HashWithIndifferentAccess.new({
94
+ :'home.page_title' => ["app/views/home/index.rhtml"],
95
+ :'general.back' => ["app/views/articles/new.rhtml", "app/views/categories/new.rhtml"],
96
+ :'articles.new.page_title' => ["app/views/articles/new.rhtml"]
97
+ })
98
+ end
99
+ end
100
+
101
+ describe "translate" do
102
+ it "should store translations to I18n backend and then write them to a YAML file" do
103
+ session[:from_locale] = :sv
104
+ session[:to_locale] = :en
105
+ translations = {
106
+ :articles => {
107
+ :new => {
108
+ :title => "New Article"
109
+ }
110
+ },
111
+ :category => "Category"
112
+ }
113
+ key_param = {'articles.new.title' => "New Article", "category" => "Category"}
114
+ I18n.backend.should_receive(:store_translations).with(:en, translations)
115
+ storage = mock(:storage)
116
+ storage.should_receive(:write_to_file)
117
+ Translate::Storage.should_receive(:new).with(:en).and_return(storage)
118
+ log = mock(:log)
119
+ log.should_receive(:write_to_file)
120
+ Translate::Log.should_receive(:new).with(:sv, :en, key_param.keys).and_return(log)
121
+ post :translate, "key" => key_param
122
+ response.should be_redirect
123
+ end
124
+ end
125
+
126
+ def get_page(*args)
127
+ get(*args)
128
+ response.should be_success
129
+ end
130
+ end
data/spec/file_spec.rb CHANGED
@@ -1,54 +1,54 @@
1
- require 'fileutils'
2
- require File.dirname(__FILE__) + '/spec_helper'
3
-
4
- describe Translate::File do
5
- describe "write" do
6
- before(:each) do
7
- @file = Translate::File.new(file_path)
8
- end
9
-
10
- after(:each) do
11
- FileUtils.rm(file_path)
12
- end
13
-
14
- it "writes all I18n messages for a locale to YAML file" do
15
- @file.write(translations)
16
- @file.read.should == Translate::File.deep_stringify_keys(translations)
17
- end
18
-
19
- def translations
20
- {
21
- :en => {
22
- :article => {
23
- :title => "One Article"
24
- },
25
- :category => "Category"
26
- }
27
- }
28
- end
29
- end
30
-
31
- describe "deep_stringify_keys" do
32
- it "should convert all keys in a hash to strings" do
33
- Translate::File.deep_stringify_keys({
34
- :en => {
35
- :article => {
36
- :title => "One Article"
37
- },
38
- :category => "Category"
39
- }
40
- }).should == {
41
- "en" => {
42
- "article" => {
43
- "title" => "One Article"
44
- },
45
- "category" => "Category"
46
- }
47
- }
48
- end
49
- end
50
-
51
- def file_path
52
- File.join(File.dirname(__FILE__), "files", "en.yml")
53
- end
54
- end
1
+ require 'fileutils'
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+
4
+ describe Translate::File do
5
+ describe "write" do
6
+ before(:each) do
7
+ @file = Translate::File.new(file_path)
8
+ end
9
+
10
+ after(:each) do
11
+ FileUtils.rm(file_path)
12
+ end
13
+
14
+ it "writes all I18n messages for a locale to YAML file" do
15
+ @file.write(translations)
16
+ @file.read.should == Translate::File.deep_stringify_keys(translations)
17
+ end
18
+
19
+ def translations
20
+ {
21
+ :en => {
22
+ :article => {
23
+ :title => "One Article"
24
+ },
25
+ :category => "Category"
26
+ }
27
+ }
28
+ end
29
+ end
30
+
31
+ describe "deep_stringify_keys" do
32
+ it "should convert all keys in a hash to strings" do
33
+ Translate::File.deep_stringify_keys({
34
+ :en => {
35
+ :article => {
36
+ :title => "One Article"
37
+ },
38
+ :category => "Category"
39
+ }
40
+ }).should == {
41
+ "en" => {
42
+ "article" => {
43
+ "title" => "One Article"
44
+ },
45
+ "category" => "Category"
46
+ }
47
+ }
48
+ end
49
+ end
50
+
51
+ def file_path
52
+ File.join(File.dirname(__FILE__), "files", "en.yml")
53
+ end
54
+ end