i18n_js 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.textile +36 -0
- data/Rakefile +52 -0
- data/lib/i18n_js.rb +35 -0
- metadata +60 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Aleksandr Koss
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
h1. Usage
|
2
|
+
|
3
|
+
h2. Examples
|
4
|
+
|
5
|
+
h3. Locale file
|
6
|
+
|
7
|
+
<pre><code>en:
|
8
|
+
js:
|
9
|
+
say_hello: "Hello cruel world!"</code></pre>
|
10
|
+
|
11
|
+
h3. environment.rb
|
12
|
+
|
13
|
+
For 'javascript' and 'messages' keys only
|
14
|
+
|
15
|
+
<pre><code>config.middleware.use 'Rack::I18n', :accepted_keys => [:javascript, :messages]</code></pre>
|
16
|
+
|
17
|
+
For all keys
|
18
|
+
|
19
|
+
<pre><code>config.middleware.use 'Rack::I18n', :accepted_keys => :all</code></pre>
|
20
|
+
|
21
|
+
The same as one line above
|
22
|
+
|
23
|
+
<pre><code>config.middleware.use 'Rack::I18nJs'</code></pre>
|
24
|
+
|
25
|
+
h3. Application layout
|
26
|
+
|
27
|
+
<pre><code>= javascript_include_tag "/javascripts/locale_#{I18n.locale}.js"</code></pre>
|
28
|
+
|
29
|
+
h4. ...or from JavaScript
|
30
|
+
|
31
|
+
<pre><code>var i18n_js = null;
|
32
|
+
pre. $.get('/javascripts/locale_en.json', null, function(data) { i18n = data; }, 'json');</code></pre>
|
33
|
+
|
34
|
+
h3. JavaScript
|
35
|
+
|
36
|
+
<pre><code>alert(i18n.say_hello);</code></pre>
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'i18n_js'
|
8
|
+
gem.summary = 'Rails I18n to JavaScript'
|
9
|
+
gem.description = 'Render Ruby on Rails I18n to JavaScript via Rack'
|
10
|
+
gem.email = "kossnocorp@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/kossnocorp/i18n_js"
|
12
|
+
gem.authors = ["Aleksandr Koss"]
|
13
|
+
gem.version = '0.5'
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "i18n_js #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/lib/i18n_js.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Rack
|
2
|
+
class I18nJs
|
3
|
+
def initialize app, options = {}
|
4
|
+
@app = app
|
5
|
+
@options = options
|
6
|
+
end
|
7
|
+
|
8
|
+
def call env
|
9
|
+
# Valid url is /javascripts/i18n-<i18n key>-<locale>.<format> where
|
10
|
+
# i18n key - yaml branch named "locale.key"
|
11
|
+
# locale - locale as is
|
12
|
+
# format - js or json
|
13
|
+
|
14
|
+
if data_array = env['PATH_INFO'].scan( \
|
15
|
+
/^\/javascripts\/locale_(\w{1,3})[.](js[on]*)$/)[0]
|
16
|
+
|
17
|
+
locale, type = data_array
|
18
|
+
|
19
|
+
# Get yaml branch by key
|
20
|
+
|
21
|
+
json = ::YAML::load(
|
22
|
+
::File.open("#{RAILS_ROOT}/config/locales/#{locale}.yml")
|
23
|
+
)[locale].to_json
|
24
|
+
|
25
|
+
content_type, response = type == 'js' ?
|
26
|
+
['application/javascript', "var I18n = #{json};"] :
|
27
|
+
['application/json', json]
|
28
|
+
|
29
|
+
[200, {'Content-Type' => content_type}, [response]]
|
30
|
+
else
|
31
|
+
@app.call env
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n_js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.5"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksandr Koss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-09 00:00:00 +06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Render Ruby on Rails I18n to JavaScript via Rack
|
17
|
+
email: kossnocorp@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.textile
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.textile
|
29
|
+
- Rakefile
|
30
|
+
- lib/i18n_js.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/kossnocorp/i18n_js
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Rails I18n to JavaScript
|
59
|
+
test_files: []
|
60
|
+
|