qoobaa-javascript_i18n 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +1 -0
- data/LICENSE +1 -1
- data/README.rdoc +3 -3
- data/VERSION +1 -1
- data/generators/javascript_i18n/javascript_i18n_generator.rb +5 -1
- data/generators/javascript_i18n/templates/initializers/javascript_i18n.rb +6 -0
- data/generators/javascript_i18n/templates/javascripts/{base.js → i18n.js} +0 -0
- data/generators/javascript_i18n/templates/tasks/javascript_i18n.rake +1 -1
- data/lib/javascript_i18n/builder.rb +25 -0
- data/lib/javascript_i18n/configuration.rb +30 -0
- data/lib/javascript_i18n/updater.rb +32 -0
- data/lib/javascript_i18n.rb +3 -13
- metadata +7 -3
data/.document
CHANGED
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -34,12 +34,12 @@ If you often change the translations files, you may generate files on
|
|
34
34
|
every request in development mode, putting the code in your
|
35
35
|
/app/controllers/application_controller.rb:
|
36
36
|
|
37
|
-
before_filter :
|
37
|
+
before_filter :update_javascript_i18n
|
38
38
|
|
39
39
|
protected
|
40
40
|
|
41
|
-
def
|
42
|
-
JavascriptI18n.
|
41
|
+
def update_javascript_i18n
|
42
|
+
JavascriptI18n.update unless Rails.env.production?
|
43
43
|
end
|
44
44
|
|
45
45
|
You can use I18n.t function in your JavaScript files, it's similar to
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -5,7 +5,11 @@ class JavascriptI18nGenerator < Rails::Generator::Base
|
|
5
5
|
record do |m|
|
6
6
|
m.directory "public/javascripts"
|
7
7
|
m.directory "public/javascripts/i18n"
|
8
|
-
m.file "javascripts/
|
8
|
+
m.file "javascripts/i18n.js", "public/javascripts/i18n/i18n.js"
|
9
|
+
|
10
|
+
m.directory "config"
|
11
|
+
m.directory "config/initializers"
|
12
|
+
m.file "initializers/javascript_i18n.rb", "config/initializers/javascript_i18n.rb"
|
9
13
|
|
10
14
|
m.directory "lib/tasks"
|
11
15
|
m.file "tasks/javascript_i18n.rake", "lib/tasks/javascript_i18n.rake"
|
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module JavascriptI18n
|
2
|
+
class Builder
|
3
|
+
attr_accessor :output_path, :lib_path, :output_style
|
4
|
+
|
5
|
+
def initialize(lib_path, output_path, output_style = :merged)
|
6
|
+
self.lib_path = lib_path
|
7
|
+
self.output_path = output_path
|
8
|
+
self.output_style = output_style
|
9
|
+
I18n.backend.send(:init_translations)
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
lib = File.read(File.join(lib_path, "i18n.js")) if output_style == :merged
|
14
|
+
I18n.backend.send(:translations).each do |key, value|
|
15
|
+
File.open(File.join(output_path, "#{key}.js"), "w") do |file|
|
16
|
+
if output_style == :merged
|
17
|
+
file.puts(lib)
|
18
|
+
file.puts("\n")
|
19
|
+
end
|
20
|
+
file.puts("I18n.translations = I18n.translations || #{value.to_json};")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module JavascriptI18n
|
4
|
+
class Configuration
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
ATTRIBUTES = [:root_path, :output_dir, :lib_dir, :output_style]
|
8
|
+
|
9
|
+
attr_accessor *ATTRIBUTES
|
10
|
+
|
11
|
+
def output_path
|
12
|
+
if root_path and output_dir
|
13
|
+
File.join(root_path, output_dir)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def lib_path
|
18
|
+
if root_path and lib_dir
|
19
|
+
File.join(root_path, lib_dir)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.configuration
|
25
|
+
if block_given?
|
26
|
+
yield Configuration.instance
|
27
|
+
end
|
28
|
+
Configuration.instance
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module JavascriptI18n
|
4
|
+
class Updater
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_accessor :last_update_time
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
# force first update
|
11
|
+
self.last_update_time = DateTime.parse("1970-01-01 00:00:00")
|
12
|
+
end
|
13
|
+
|
14
|
+
def most_recent_update_time
|
15
|
+
I18n.load_path.map { |translation_file| File.stat(translation_file).mtime }.max
|
16
|
+
end
|
17
|
+
|
18
|
+
def should_update?
|
19
|
+
most_recent_update_time > last_update_time
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.update
|
24
|
+
update! if Updater.instance.should_update?
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.update!
|
28
|
+
builder = Builder.new(configuration.lib_path, configuration.output_path, configuration.output_style)
|
29
|
+
builder.run
|
30
|
+
Updater.instance.last_update_time = Updater.instance.most_recent_update_time
|
31
|
+
end
|
32
|
+
end
|
data/lib/javascript_i18n.rb
CHANGED
@@ -1,13 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
header = File.read(File.join(path, "base.js"))
|
5
|
-
I18n.backend.send(:init_translations)
|
6
|
-
I18n.backend.send(:translations).each do |key, value|
|
7
|
-
File.open(File.join(path, "#{key}.js"), "w") do |file|
|
8
|
-
file.puts(header)
|
9
|
-
file.puts("\nI18n.translations = I18n.translations || #{value.to_json};")
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
1
|
+
require "javascript_i18n/configuration"
|
2
|
+
require "javascript_i18n/builder"
|
3
|
+
require "javascript_i18n/updater"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qoobaa-javascript_i18n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jakub Ku\xC5\xBAma"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -30,9 +30,13 @@ files:
|
|
30
30
|
- Rakefile
|
31
31
|
- VERSION
|
32
32
|
- generators/javascript_i18n/javascript_i18n_generator.rb
|
33
|
-
- generators/javascript_i18n/templates/
|
33
|
+
- generators/javascript_i18n/templates/initializers/javascript_i18n.rb
|
34
|
+
- generators/javascript_i18n/templates/javascripts/i18n.js
|
34
35
|
- generators/javascript_i18n/templates/tasks/javascript_i18n.rake
|
35
36
|
- lib/javascript_i18n.rb
|
37
|
+
- lib/javascript_i18n/builder.rb
|
38
|
+
- lib/javascript_i18n/configuration.rb
|
39
|
+
- lib/javascript_i18n/updater.rb
|
36
40
|
- test/javascript_i18n_test.rb
|
37
41
|
- test/test_helper.rb
|
38
42
|
has_rdoc: false
|