refinerycms-i18n 0.9.8.12 → 0.9.9
Sign up to get free protection for your applications and to get access to all the features.
- data/features/dashboard.feature +28 -0
- data/lib/gemspec.rb +2 -2
- data/lib/refinery/i18n.rb +27 -6
- data/lib/translate/file.rb +28 -26
- data/lib/translate/keys.rb +102 -100
- data/lib/translate/log.rb +34 -31
- data/lib/translate/storage.rb +17 -15
- metadata +5 -6
@@ -0,0 +1,28 @@
|
|
1
|
+
@refinerycms @i18n
|
2
|
+
Feature: Translation Support using i18n
|
3
|
+
In order to have translation support using i18n
|
4
|
+
As an administrator
|
5
|
+
I want to use the dashboard
|
6
|
+
|
7
|
+
Background:
|
8
|
+
Given I am a logged in refinery user
|
9
|
+
And my locale is en
|
10
|
+
When I go to the Dashboard
|
11
|
+
|
12
|
+
@i18n
|
13
|
+
Scenario: Translation options available
|
14
|
+
Then I should see "English Change language"
|
15
|
+
|
16
|
+
@i18n
|
17
|
+
Scenario: Change Language to Slovenian and back to English
|
18
|
+
When I follow "English Change language"
|
19
|
+
And I follow "Slovenian"
|
20
|
+
Then I should be on the Dashboard
|
21
|
+
And I should see "Slovenian Spremeni Jezik"
|
22
|
+
And I should not see "Switch to your website"
|
23
|
+
# Back to English
|
24
|
+
When I follow "Slovenian Spremeni Jezik"
|
25
|
+
And I follow "English"
|
26
|
+
Then I should be on the Dashboard
|
27
|
+
And I should see "Switch to your website"
|
28
|
+
And I should not see "Spremeni Jezik"
|
data/lib/gemspec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
version = '0.9.
|
2
|
+
version = '0.9.9'
|
3
3
|
raise "Could not get version so gemspec can not be built" if version.nil?
|
4
4
|
files = Dir.glob("**/*").flatten.reject do |file|
|
5
5
|
file =~ /\.gem(spec)?$/
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.require_paths = %w(lib)
|
19
19
|
|
20
20
|
s.add_dependency 'refinerycms', '>= 0.9.8'
|
21
|
-
s.add_dependency 'routing-filter', '~> 0.1
|
21
|
+
s.add_dependency 'routing-filter', '~> 0.1'
|
22
22
|
|
23
23
|
s.files = [
|
24
24
|
'#{files.join("',\n '")}'
|
data/lib/refinery/i18n.rb
CHANGED
@@ -22,12 +22,12 @@ module Refinery
|
|
22
22
|
def find_or_set_locale
|
23
23
|
if ::Refinery::I18n.enabled?
|
24
24
|
if ::Refinery::I18n.has_locale?(locale = params[:locale].try(:to_sym))
|
25
|
-
::I18n.locale = locale
|
25
|
+
Thread.current[:globalize_locale] = ::I18n.locale = locale
|
26
26
|
elsif locale.present? and locale != ::Refinery::I18n.default_frontend_locale
|
27
|
-
params[:locale] = I18n.locale = ::Refinery::I18n.default_frontend_locale
|
27
|
+
Thread.current[:globalize_locale] = params[:locale] = I18n.locale = ::Refinery::I18n.default_frontend_locale
|
28
28
|
redirect_to(params, :notice => "The locale '#{locale.to_s}' is not supported.") and return
|
29
29
|
else
|
30
|
-
::I18n.locale = ::Refinery::I18n.default_frontend_locale
|
30
|
+
Thread.current[:globalize_locale] = ::I18n.locale = ::Refinery::I18n.default_frontend_locale
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
@@ -36,7 +36,8 @@ module Refinery
|
|
36
36
|
end
|
37
37
|
|
38
38
|
::Admin::BaseController.class_eval do
|
39
|
-
|
39
|
+
# globalize! should be prepended first so that it runs after find_or_set_locale
|
40
|
+
prepend_before_filter :globalize!, :find_or_set_locale
|
40
41
|
|
41
42
|
def find_or_set_locale
|
42
43
|
if (params[:set_locale].present? and ::Refinery::I18n.locales.include?(params[:set_locale].to_sym))
|
@@ -47,12 +48,23 @@ module Refinery
|
|
47
48
|
end
|
48
49
|
end
|
49
50
|
|
50
|
-
|
51
|
+
def globalize!
|
52
|
+
Thread.current[:globalize_locale] = (params[:switch_locale] || ::Refinery::I18n.default_frontend_locale)
|
53
|
+
end
|
54
|
+
|
55
|
+
protected :find_or_set_locale, :globalize!
|
51
56
|
end
|
52
57
|
end
|
53
58
|
|
54
59
|
config.after_initialize do
|
55
60
|
::Refinery::I18n.setup! if defined?(RefinerySetting) and RefinerySetting.table_exists?
|
61
|
+
|
62
|
+
Refinery::Plugin.register do |plugin|
|
63
|
+
plugin.name = "refinery_i18n"
|
64
|
+
plugin.version = %q{0.9.9}
|
65
|
+
plugin.hide_from_menu = true
|
66
|
+
plugin.always_allow_access = true
|
67
|
+
end
|
56
68
|
end
|
57
69
|
|
58
70
|
end
|
@@ -112,12 +124,20 @@ module Refinery
|
|
112
124
|
|
113
125
|
def default_frontend_locale
|
114
126
|
(@default_frontend_locale ||= RefinerySetting.find_or_set(:i18n_translation_default_frontend_locale,
|
115
|
-
|
127
|
+
:en, {
|
116
128
|
:scoping => 'refinery',
|
117
129
|
:callback_proc_as_string => %q{::Refinery::I18n.setup!}
|
118
130
|
})).to_sym
|
119
131
|
end
|
120
132
|
|
133
|
+
def frontend_locales
|
134
|
+
@frontend_locales ||= RefinerySetting.find_or_set(:i18n_translation_frontend_locales,
|
135
|
+
[self.default_frontend_locale], {
|
136
|
+
:scoping => 'refinery',
|
137
|
+
:callback_proc_as_string => %q{::Refinery::I18n.setup!}
|
138
|
+
})
|
139
|
+
end
|
140
|
+
|
121
141
|
def locales
|
122
142
|
@locales ||= RefinerySetting.find_or_set(:i18n_translation_locales, self.built_in_locales,
|
123
143
|
{
|
@@ -138,6 +158,7 @@ module Refinery
|
|
138
158
|
@default_locale = nil
|
139
159
|
@default_frontend_locale = nil
|
140
160
|
@current_locale = nil
|
161
|
+
@frontend_locales = nil
|
141
162
|
|
142
163
|
self.load_base_locales!
|
143
164
|
self.load_refinery_locales!
|
data/lib/translate/file.rb
CHANGED
@@ -1,35 +1,37 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module Translate
|
4
|
+
class File
|
5
|
+
attr_accessor :path
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def initialize(path)
|
8
|
+
self.path = path
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def write(keys)
|
12
|
+
FileUtils.mkdir_p File.dirname(path)
|
13
|
+
File.open(path, "w") do |file|
|
14
|
+
file.puts keys_to_yaml(Translate::File.deep_stringify_keys(keys))
|
15
|
+
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
def read
|
19
|
+
File.exists?(path) ? YAML::load(IO.read(path)) : {}
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
# Stringifying keys for prettier YAML
|
23
|
+
def self.deep_stringify_keys(hash)
|
24
|
+
hash.inject({}) { |result, (key, value)|
|
25
|
+
value = deep_stringify_keys(value) if value.is_a? Hash
|
26
|
+
result[(key.to_s rescue key) || key] = value
|
27
|
+
result
|
28
|
+
}
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
private
|
32
|
+
def keys_to_yaml(keys)
|
33
|
+
# Using ya2yaml, if available, for UTF8 support
|
34
|
+
keys.respond_to?(:ya2yaml) ? keys.ya2yaml(:escape_as_utf8 => true) : keys.to_yaml
|
35
|
+
end
|
34
36
|
end
|
35
|
-
end
|
37
|
+
end
|
data/lib/translate/keys.rb
CHANGED
@@ -1,123 +1,125 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module Translate
|
4
|
+
class KeysKeys
|
5
|
+
# Allows keys extracted from lookups in files to be cached
|
6
|
+
def self.files
|
7
|
+
@@files ||= Translate::Keys.new.files
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
# Allows flushing of the files cache
|
11
|
+
def self.files=(files)
|
12
|
+
@@files = files
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def files
|
16
|
+
@files ||= extract_files
|
17
|
+
end
|
18
|
+
alias_method :to_hash, :files
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def keys
|
21
|
+
files.keys
|
22
|
+
end
|
23
|
+
alias_method :to_a, :keys
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def i18n_keys(locale)
|
26
|
+
I18n.backend.send(:init_translations) unless I18n.backend.initialized?
|
27
|
+
extract_i18n_keys(I18n.backend.send(:translations)[locale.to_sym]).sort
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
30
|
+
# Convert something like:
|
31
|
+
#
|
32
|
+
# {
|
33
|
+
# :pressrelease => {
|
34
|
+
# :label => {
|
35
|
+
# :one => "Pressmeddelande"
|
36
|
+
# }
|
37
|
+
# }
|
38
|
+
# }
|
39
|
+
#
|
40
|
+
# to:
|
41
|
+
#
|
42
|
+
# {'pressrelease.label.one' => "Pressmeddelande"}
|
43
|
+
#
|
44
|
+
def self.to_shallow_hash(hash)
|
45
|
+
hash.inject({}) do |shallow_hash, (key, value)|
|
46
|
+
if value.is_a?(Hash)
|
47
|
+
to_shallow_hash(value).each do |sub_key, sub_value|
|
48
|
+
shallow_hash[[key, sub_key].join(".")] = sub_value
|
49
|
+
end
|
50
|
+
else
|
51
|
+
shallow_hash[key.to_s] = value
|
48
52
|
end
|
49
|
-
|
50
|
-
shallow_hash[key.to_s] = value
|
53
|
+
shallow_hash
|
51
54
|
end
|
52
|
-
shallow_hash
|
53
55
|
end
|
54
|
-
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
57
|
+
# Convert something like:
|
58
|
+
#
|
59
|
+
# {'pressrelease.label.one' => "Pressmeddelande"}
|
60
|
+
#
|
61
|
+
# to:
|
62
|
+
#
|
63
|
+
# {
|
64
|
+
# :pressrelease => {
|
65
|
+
# :label => {
|
66
|
+
# :one => "Pressmeddelande"
|
67
|
+
# }
|
68
|
+
# }
|
69
|
+
# }
|
70
|
+
def self.to_deep_hash(hash)
|
71
|
+
hash.inject({}) do |deep_hash, (key, value)|
|
72
|
+
keys = key.to_s.split('.').reverse
|
73
|
+
leaf_key = keys.shift
|
74
|
+
key_hash = keys.inject({leaf_key.to_sym => value}) { |hash, key| {key.to_sym => hash} }
|
75
|
+
deep_merge!(deep_hash, key_hash)
|
76
|
+
deep_hash
|
77
|
+
end
|
76
78
|
end
|
77
|
-
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
80
|
+
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
81
|
+
def self.deep_merge!(hash1, hash2)
|
82
|
+
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
83
|
+
hash1.merge!(hash2, &merger)
|
84
|
+
end
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
86
|
+
private
|
87
|
+
def extract_i18n_keys(hash, parent_keys = [])
|
88
|
+
hash.inject([]) do |keys, (key, value)|
|
89
|
+
full_key = parent_keys + [key]
|
90
|
+
if value.is_a?(Hash)
|
91
|
+
# Nested hash
|
92
|
+
keys += extract_i18n_keys(value, full_key)
|
93
|
+
elsif value.present?
|
94
|
+
# String leaf node
|
95
|
+
keys << full_key.join(".")
|
96
|
+
end
|
97
|
+
keys
|
95
98
|
end
|
96
|
-
keys
|
97
99
|
end
|
98
|
-
end
|
99
100
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
101
|
+
def extract_files
|
102
|
+
files_to_scan.inject(HashWithIndifferentAccess.new) do |files, file|
|
103
|
+
IO.read(file).scan(i18n_lookup_pattern).flatten.map(&:to_sym).each do |key|
|
104
|
+
files[key] ||= []
|
105
|
+
path = Pathname.new(File.expand_path(file)).relative_path_from(Pathname.new(Rails.root)).to_s
|
106
|
+
files[key] << path unless files[key].include?(path)
|
107
|
+
end
|
108
|
+
files
|
106
109
|
end
|
107
|
-
files
|
108
110
|
end
|
109
|
-
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
def i18n_lookup_pattern
|
113
|
+
/\b(?:I18n\.t|I18n\.translate|t)(?:\s|\():?'([a-z0-9_]+.[a-z0-9_.]+)'\)?/
|
114
|
+
end
|
114
115
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
116
|
+
def files_to_scan
|
117
|
+
Dir.glob(File.join(files_root_dir, "{app,config,lib}", "**","*.{rb,erb,rhtml}")) +
|
118
|
+
Dir.glob(File.join(files_root_dir, "public", "javascripts", "**","*.js"))
|
119
|
+
end
|
119
120
|
|
120
|
-
|
121
|
-
|
121
|
+
def files_root_dir
|
122
|
+
Rails.root
|
123
|
+
end
|
122
124
|
end
|
123
|
-
end
|
125
|
+
end
|
data/lib/translate/log.rb
CHANGED
@@ -1,39 +1,42 @@
|
|
1
1
|
require 'fileutils'
|
2
|
-
class Translate::Log
|
3
|
-
attr_accessor :from_locale, :to_locale, :keys
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
self.keys = keys
|
9
|
-
end
|
3
|
+
module Translate
|
4
|
+
class Log
|
5
|
+
attr_accessor :from_locale, :to_locale, :keys
|
10
6
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
def initialize(from_locale, to_locale, keys)
|
8
|
+
self.from_locale = from_locale
|
9
|
+
self.to_locale = to_locale
|
10
|
+
self.keys = keys
|
11
|
+
end
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
|
13
|
+
def write_to_file
|
14
|
+
current_texts = File.exists?(file_path) ? file.read : {}
|
15
|
+
current_texts.merge!(from_texts)
|
16
|
+
file.write(current_texts)
|
17
|
+
end
|
20
18
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
19
|
+
def read
|
20
|
+
file.read
|
21
|
+
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
private
|
24
|
+
def file
|
25
|
+
@file ||= Translate::File.new(file_path)
|
26
|
+
end
|
27
|
+
|
28
|
+
def from_texts
|
29
|
+
Translate::File.deep_stringify_keys(Translate::Keys.to_deep_hash(keys.inject({}) do |hash, key|
|
30
|
+
hash[key] = I18n.backend.send(:lookup, from_locale, key)
|
31
|
+
hash
|
32
|
+
end))
|
33
|
+
end
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
def file_path
|
36
|
+
#make sure diff log dir exists
|
37
|
+
translate_log_dir = File.join(RAILS_ROOT, 'log', 'translate')
|
38
|
+
FileUtils.mkdir_p(translate_log_dir)
|
39
|
+
File.join(translate_log_dir, "from_#{from_locale}_to_#{to_locale}.yml")
|
40
|
+
end
|
38
41
|
end
|
39
|
-
end
|
42
|
+
end
|
data/lib/translate/storage.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Translate
|
2
|
+
class Storage
|
3
|
+
attr_accessor :locale
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(locale)
|
6
|
+
self.locale = locale.to_sym
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def write_to_file
|
10
|
+
Translate::File.new(file_path).write(keys)
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
private
|
14
|
+
def keys
|
15
|
+
{locale => I18n.backend.send(:translations)[locale]}
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
def file_path
|
19
|
+
File.join(Translate.locales_dir, "#{locale}.yml")
|
20
|
+
end
|
19
21
|
end
|
20
|
-
end
|
22
|
+
end
|
metadata
CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.9.8.12
|
8
|
+
- 9
|
9
|
+
version: 0.9.9
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Resolve Digital
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date:
|
17
|
+
date: 2011-01-13 00:00:00 +13:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -44,8 +43,7 @@ dependencies:
|
|
44
43
|
segments:
|
45
44
|
- 0
|
46
45
|
- 1
|
47
|
-
|
48
|
-
version: 0.1.6
|
46
|
+
version: "0.1"
|
49
47
|
type: :runtime
|
50
48
|
version_requirements: *id002
|
51
49
|
description: i18n logic extracted from RefineryCMS, for Refinery CMS.
|
@@ -115,6 +113,7 @@ files:
|
|
115
113
|
- config/locales/vi.yml
|
116
114
|
- config/locales/zh-CN.yml
|
117
115
|
- config/locales/zh-TW.yml
|
116
|
+
- features/dashboard.feature
|
118
117
|
- i18n-js-readme.rdoc
|
119
118
|
- lib/gemspec.rb
|
120
119
|
- lib/refinery/i18n-filter.rb
|