i18n-js 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +22 -25
- data/config/i18n-js.yml +21 -17
- data/i18n-js.gemspec +1 -0
- data/lib/i18n-js.rb +3 -7
- data/lib/i18n-js/middleware.rb +52 -0
- data/lib/i18n-js/railtie.rb +2 -4
- data/lib/i18n-js/version.rb +2 -2
- data/spec/i18n_spec.rb +7 -29
- metadata +31 -19
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -2,13 +2,6 @@
|
|
2
2
|
|
3
3
|
It's a small library to provide the Rails I18n translations on the Javascript.
|
4
4
|
|
5
|
-
This library has been tested on:
|
6
|
-
|
7
|
-
* Safari 4 (Mac)
|
8
|
-
* Firefox 3.6 (Mac)
|
9
|
-
* Opera 10 (Mac)
|
10
|
-
* IE6+ (Mac Parallels)
|
11
|
-
|
12
5
|
== Usage
|
13
6
|
|
14
7
|
=== Installation
|
@@ -17,28 +10,31 @@ This library has been tested on:
|
|
17
10
|
|
18
11
|
=== Setting up
|
19
12
|
|
13
|
+
You <b>don't</b> need to set up a thing. The default settings will work just okay. But if you want to split translations into several files or specify specific contexts, you can follow the rest of this setting up section.
|
14
|
+
|
20
15
|
==== Rails <= 3.0
|
21
|
-
|
16
|
+
|
17
|
+
Run <tt>rake i18n:js:setup</tt> to copy <tt>i18n.js</tt> to your javascript directory and <tt>i18n-js.yml</tt> to your config folder (if not already present). Then you're ready to go!
|
22
18
|
|
23
19
|
==== Rails >= 3.1
|
24
|
-
|
20
|
+
|
21
|
+
Run <tt>rake i18n:js:setup</tt> to copy <tt>i18n-js.yml</tt> to your config folder (if not already present). You should change the path in <tt>i18n.js.yml</tt> to <tt>app/assets/javascripts/i18n</tt> to make full use of the asset pipeline.
|
22
|
+
|
25
23
|
Next add the following lines to your application.js to make the javascripts and translations available to your app:
|
26
24
|
|
27
25
|
//= require i18n
|
28
26
|
//= require i18n/translations
|
29
27
|
|
30
|
-
If you don't want to use the asset pipeline, just disable it on your
|
28
|
+
If you don't want to use the asset pipeline, just disable it on your Rails application by setting <tt>config.assets.enabled</tt> to +false+ on your <tt>config/application.rb</tt> file.
|
31
29
|
|
32
30
|
==== Exporting translations
|
33
31
|
|
34
|
-
You can export the translations file by running `rake i18n:js:export`.
|
35
|
-
|
32
|
+
You can export the translations file by running `rake i18n:js:export`.
|
33
|
+
Translations will be automatically exported in development mode.
|
36
34
|
|
37
35
|
==== Configuration
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
Messages files can also be customized, you can even get more files generated to different folders and with different translations to best suit your needs.
|
37
|
+
Translation files can be customized. You can even get more files generated to different folders and with different translations to best suit your needs.
|
42
38
|
|
43
39
|
Examples:
|
44
40
|
|
@@ -48,7 +44,13 @@ Examples:
|
|
48
44
|
- file: 'public/javascripts/path-to-your-second-file.js'
|
49
45
|
only: ['*.activerecord', '*.admin.*.title']
|
50
46
|
|
51
|
-
If <tt>only</tt> is omitted all the translations will be saved
|
47
|
+
If <tt>only</tt> is omitted all the translations will be saved. Also, make sure you add that initial <tt>*</tt>; it specifies that all languages will be exported. If you want to export only one language, you can do something like this:
|
48
|
+
|
49
|
+
translations:
|
50
|
+
- file: 'public/javascripts/en.js'
|
51
|
+
only: 'en.*'
|
52
|
+
- file: 'public/javascripts/pt-BR.js'
|
53
|
+
only: 'pt-BR.*'
|
52
54
|
|
53
55
|
To find more examples on how to use the configuration file please refer to the tests.
|
54
56
|
|
@@ -120,7 +122,7 @@ You also provide an array as scope.
|
|
120
122
|
|
121
123
|
==== Number formatting
|
122
124
|
|
123
|
-
Similar to Rails helpers, you have
|
125
|
+
Similar to Rails helpers, you have localized number and currency formatting.
|
124
126
|
|
125
127
|
I18n.l("currency", 1990.99);
|
126
128
|
// $1,990.99
|
@@ -174,7 +176,6 @@ The +toHumanSize+ function accepts the following options:
|
|
174
176
|
I18n.toHumanSize(1234); // 1KB
|
175
177
|
I18n.toHumanSize(1234 * 1024); // 1MB
|
176
178
|
|
177
|
-
|
178
179
|
==== Date formatting
|
179
180
|
|
180
181
|
// accepted formats
|
@@ -230,7 +231,7 @@ The only requirement is that you need to set the +translations+ attribute like f
|
|
230
231
|
message: "Some special message for you"
|
231
232
|
}
|
232
233
|
|
233
|
-
I18n.translations["pt"] = {
|
234
|
+
I18n.translations["pt-BR"] = {
|
234
235
|
message: "Uma mensagem especial para você"
|
235
236
|
}
|
236
237
|
|
@@ -253,13 +254,9 @@ Please respect the indentation rules. And use 2 spaces, not tabs.
|
|
253
254
|
|
254
255
|
=== Running tests
|
255
256
|
|
256
|
-
|
257
|
-
|
258
|
-
gem install rspec
|
259
|
-
|
260
|
-
To run JavaScript tests, you need to install http://github.com/fnando/spec-js
|
257
|
+
First, install all dependencies.
|
261
258
|
|
262
|
-
|
259
|
+
bundle install
|
263
260
|
|
264
261
|
Then just run `rake spec`.
|
265
262
|
|
data/config/i18n-js.yml
CHANGED
@@ -1,18 +1,22 @@
|
|
1
|
-
# Find more details about this configuration file
|
2
|
-
# at http://github.com/fnando/i18n-js
|
3
|
-
|
4
|
-
# Automatically export translations when running in
|
5
|
-
# development mode, or once when your app is booted
|
6
|
-
# in production mode.
|
7
|
-
auto_export: no
|
8
|
-
|
9
1
|
# Split context in several files.
|
10
|
-
# By default only one file is exported
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
|
2
|
+
# By default only one file with all translations is exported and
|
3
|
+
# no configuration is required. Your settings for asset pipeline
|
4
|
+
# are automatically recognized.
|
5
|
+
#
|
6
|
+
# If you want to split translations into several files or specify
|
7
|
+
# locale contexts that will be exported, just use this file to do
|
8
|
+
# so.
|
9
|
+
#
|
10
|
+
# If you're going to use the Rails 3.1 asset pipeline, change
|
11
|
+
# the following configuration to something like this:
|
12
|
+
#
|
13
|
+
# translations:
|
14
|
+
# - file: "app/assets/javascripts/i18n/translations.js"
|
15
|
+
#
|
16
|
+
# If you're running an old version, you can use something
|
17
|
+
# like this:
|
18
|
+
#
|
19
|
+
# translations:
|
20
|
+
# - file: "public/javascripts/translations.js"
|
21
|
+
# only: "*"
|
22
|
+
#
|
data/i18n-js.gemspec
CHANGED
data/lib/i18n-js.rb
CHANGED
@@ -6,6 +6,7 @@ module SimplesIdeias
|
|
6
6
|
|
7
7
|
require "i18n-js/railtie" if Rails.version >= "3.0"
|
8
8
|
require "i18n-js/engine" if Rails.version >= "3.1"
|
9
|
+
require "i18n-js/middleware"
|
9
10
|
|
10
11
|
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
11
12
|
MERGER = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2 }
|
@@ -15,7 +16,7 @@ module SimplesIdeias
|
|
15
16
|
end
|
16
17
|
|
17
18
|
def export_dir
|
18
|
-
if Rails.version >= "3.1" &&
|
19
|
+
if Rails.version >= "3.1" && Rails.configuration.assets.enabled
|
19
20
|
"app/assets/javascripts/i18n"
|
20
21
|
else
|
21
22
|
"public/javascripts"
|
@@ -49,17 +50,12 @@ module SimplesIdeias
|
|
49
50
|
# custom output directory
|
50
51
|
def config
|
51
52
|
if config?
|
52
|
-
YAML.load_file(config_file).with_indifferent_access
|
53
|
+
(YAML.load_file(config_file) || {}).with_indifferent_access
|
53
54
|
else
|
54
55
|
{}
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
58
|
-
# Check if translations can be automatically exported.
|
59
|
-
def auto_export?
|
60
|
-
Rails.env.development? || (config? && config[:auto_export])
|
61
|
-
end
|
62
|
-
|
63
59
|
# Check if configuration file exist
|
64
60
|
def config?
|
65
61
|
File.file? config_file
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module SimplesIdeias
|
2
|
+
module I18n
|
3
|
+
class Middleware
|
4
|
+
def initialize(app)
|
5
|
+
@app = app
|
6
|
+
end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
@cache = nil
|
10
|
+
verify_locale_files!
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def cache_path
|
16
|
+
@cache_path ||= Rails.root.join("tmp/cache/i18n-js.yml")
|
17
|
+
end
|
18
|
+
|
19
|
+
def cache
|
20
|
+
@cache ||= begin
|
21
|
+
if cache_path.exist?
|
22
|
+
YAML.load_file(cache_path) || {}
|
23
|
+
else
|
24
|
+
{}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def verify_locale_files!
|
30
|
+
valid_cache = []
|
31
|
+
changed_files = []
|
32
|
+
|
33
|
+
valid_cache.push cache_path.exist?
|
34
|
+
valid_cache.push ::I18n.load_path.size == cache.size
|
35
|
+
|
36
|
+
::I18n.load_path.each do |path|
|
37
|
+
change = File.mtime(path).to_i
|
38
|
+
file_has_changed = change != cache[path]
|
39
|
+
valid_cache.push file_has_changed
|
40
|
+
changed_files << path if file_has_changed
|
41
|
+
cache[path] = change
|
42
|
+
end
|
43
|
+
|
44
|
+
File.open(cache_path, "w+") do |file|
|
45
|
+
file << cache.to_yaml
|
46
|
+
end
|
47
|
+
|
48
|
+
SimplesIdeias::I18n.export! unless valid_cache.all?
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/i18n-js/railtie.rb
CHANGED
@@ -5,10 +5,8 @@ module SimplesIdeias
|
|
5
5
|
require "i18n-js/rake"
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
i18n.export! if i18n.auto_export?
|
11
|
-
end
|
8
|
+
initializer "i18n-js.initialize" do |app|
|
9
|
+
app.config.middleware.use(Middleware) if Rails.env.development?
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
data/lib/i18n-js/version.rb
CHANGED
data/spec/i18n_spec.rb
CHANGED
@@ -50,6 +50,7 @@ describe SimplesIdeias::I18n do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
it "loads configuration file" do
|
53
|
+
set_config "default.yml"
|
53
54
|
SimplesIdeias::I18n.setup!
|
54
55
|
|
55
56
|
SimplesIdeias::I18n.config?.should be_true
|
@@ -67,12 +68,6 @@ describe SimplesIdeias::I18n do
|
|
67
68
|
Rails.root.join(SimplesIdeias::I18n.export_dir, "translations.js").should be_file
|
68
69
|
end
|
69
70
|
|
70
|
-
it "exports messages using the default configuration file" do
|
71
|
-
set_config "default.yml"
|
72
|
-
SimplesIdeias::I18n.should_receive(:save).with(translations, "public/javascripts/translations.js")
|
73
|
-
SimplesIdeias::I18n.export!
|
74
|
-
end
|
75
|
-
|
76
71
|
it "exports messages using custom output path" do
|
77
72
|
set_config "custom_path.yml"
|
78
73
|
SimplesIdeias::I18n.should_receive(:save).with(translations, "public/javascripts/translations/all.js")
|
@@ -159,33 +154,16 @@ describe SimplesIdeias::I18n do
|
|
159
154
|
File.read(SimplesIdeias::I18n.javascript_file).should == "UPDATED"
|
160
155
|
end
|
161
156
|
|
162
|
-
describe "#auto_export?" do
|
163
|
-
it "returns true for development mode" do
|
164
|
-
Rails.env = stub(:development? => true)
|
165
|
-
SimplesIdeias::I18n.should be_auto_export
|
166
|
-
end
|
167
|
-
|
168
|
-
it "returns false for production mode" do
|
169
|
-
Rails.env = stub(:production? => true, :development? => false)
|
170
|
-
SimplesIdeias::I18n.should_not be_auto_export
|
171
|
-
end
|
172
|
-
|
173
|
-
it "returns true for production when configuration is set" do
|
174
|
-
Rails.env = stub(:production? => true, :development? => false)
|
175
|
-
SimplesIdeias::I18n.stub :config? => true, :config => {:auto_export => true}
|
176
|
-
SimplesIdeias::I18n.should be_auto_export
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
157
|
describe "#export_dir" do
|
181
|
-
it "detects Rails 3.1" do
|
158
|
+
it "detects Rails 3.1 with asset pipeline enabled" do
|
182
159
|
Rails.version = "3.1"
|
160
|
+
Rails.stub_chain(:configuration, :assets, :enabled => true)
|
183
161
|
SimplesIdeias::I18n.export_dir == "vendor/assets/javascripts"
|
184
162
|
end
|
185
163
|
|
186
|
-
it "
|
164
|
+
it "detects Rails 3.1 with asset pipeline disabled" do
|
187
165
|
Rails.version = "3.1"
|
188
|
-
|
166
|
+
Rails.stub_chain(:configuration, :assets, :enabled => false)
|
189
167
|
SimplesIdeias::I18n.export_dir == "public/javascripts"
|
190
168
|
end
|
191
169
|
|
@@ -199,8 +177,8 @@ describe SimplesIdeias::I18n do
|
|
199
177
|
# Set the configuration as the current one
|
200
178
|
def set_config(path)
|
201
179
|
config = HashWithIndifferentAccess.new(YAML.load_file(File.dirname(__FILE__) + "/resources/#{path}"))
|
202
|
-
SimplesIdeias::I18n.
|
203
|
-
SimplesIdeias::I18n.
|
180
|
+
SimplesIdeias::I18n.stub(:config? => true)
|
181
|
+
SimplesIdeias::I18n.stub(:config => config)
|
204
182
|
end
|
205
183
|
|
206
184
|
# Shortcut to SimplesIdeias::I18n.translations
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n-js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n
|
16
|
-
requirement: &
|
16
|
+
requirement: &70302525717000 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70302525717000
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: fakeweb
|
27
|
-
requirement: &
|
27
|
+
requirement: &70302525716240 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70302525716240
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activesupport
|
38
|
-
requirement: &
|
38
|
+
requirement: &70302525715340 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 3.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70302525715340
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70302525708020 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '2.6'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70302525708020
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: ruby-debug19
|
60
|
-
requirement: &
|
60
|
+
requirement: &70302525707340 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70302525707340
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: spec-js
|
71
|
-
requirement: &
|
71
|
+
requirement: &70302525706180 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: 0.1.0.beta.0
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70302525706180
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rake
|
82
|
-
requirement: &
|
82
|
+
requirement: &70302525705440 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - =
|
@@ -87,7 +87,18 @@ dependencies:
|
|
87
87
|
version: 0.8.7
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70302525705440
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: ruby-debug19
|
93
|
+
requirement: &70302525704920 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *70302525704920
|
91
102
|
description: It's a small library to provide the Rails I18n translations on the Javascript.
|
92
103
|
email:
|
93
104
|
- fnando.vieira@gmail.com
|
@@ -105,6 +116,7 @@ files:
|
|
105
116
|
- i18n-js.gemspec
|
106
117
|
- lib/i18n-js.rb
|
107
118
|
- lib/i18n-js/engine.rb
|
119
|
+
- lib/i18n-js/middleware.rb
|
108
120
|
- lib/i18n-js/railtie.rb
|
109
121
|
- lib/i18n-js/rake.rb
|
110
122
|
- lib/i18n-js/version.rb
|
@@ -133,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
145
|
version: '0'
|
134
146
|
segments:
|
135
147
|
- 0
|
136
|
-
hash:
|
148
|
+
hash: 2319593298030607948
|
137
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
150
|
none: false
|
139
151
|
requirements:
|
@@ -142,10 +154,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
154
|
version: '0'
|
143
155
|
segments:
|
144
156
|
- 0
|
145
|
-
hash:
|
157
|
+
hash: 2319593298030607948
|
146
158
|
requirements: []
|
147
159
|
rubyforge_project:
|
148
|
-
rubygems_version: 1.8.
|
160
|
+
rubygems_version: 1.8.10
|
149
161
|
signing_key:
|
150
162
|
specification_version: 3
|
151
163
|
summary: It's a small library to provide the Rails I18n translations on the Javascript.
|