remote_i18n 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "bundler", "~> 1.0.0"
5
+ gem "jeweler", "~> 1.5.2"
6
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.0.0)
16
+ jeweler (~> 1.5.2)
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Julien Guimont
2
+ Copyright (c) 2009 Darcy Laycock
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Remote I18n #
2
+
3
+ Remote I18n helps developer export translated messages from a Rails server to a Javascript/HTML client application. More and more we need to use templates in javascript to create new HTML segments, but when you are building an I18n application you need to make sure that those segments contains the right language. This is what this project is for. For now it bundle the Rails translations into different javascript file ready for consumption.
4
+
5
+ ## Getting started
6
+ *For Rails 3 only*
7
+
8
+ 1. Add `gem 'remote_i18n' to your Gemfile
9
+ 2. Run `bundle install`
10
+ 3. Create a "remote_i18n" in /config/initializers
11
+
12
+ Add the following
13
+
14
+ RemoteI18n.configure do |config|
15
+ config.translate :name=>:date, :i18n_path=>"date", :only=>["en", "fr-CA"]
16
+ config.translate :name=>:errors, :i18n_path=>"errors", :only=>["en", "fr-CA"]
17
+ end
18
+
19
+ The configuration can't be simpler
20
+ - _name_ is the name of the resulting file in /public/javascripts
21
+ - _i18n_path_ is the path to use in the Rails i18n module
22
+ - _only_ is to specify languages, by default it is all configured ones
23
+
24
+ The output of this configuration would be:
25
+
26
+ - /public/javascripts/date.en.js
27
+ - /public/javascripts/date.fr-CA.js
28
+ - /public/javascripts/errors.en.js
29
+ - /public/javascripts/errors.fr-CA.js
30
+
31
+ In Javascript the _T_ object is created to access the translations:
32
+
33
+ blank_error = T.errors.message.blank;
34
+ january = T.date.month_names[1];
35
+
36
+ ## More Details
37
+
38
+ In development and in test, a rack middleware is loaded and compiles the changes to the translations at each requests. This cannot be done in production, so there is a bundled rake task to generate the javascript files for deployment:
39
+
40
+ rake remote_i18n:compile
41
+
42
+ I use Jammit to organize my javascript files. I create a Jammit package for each language and use the locale to include it on the page:
43
+
44
+ include_javascripts "translations_#{I18n.locale}"
45
+
46
+ ## Copyright ##
47
+ Most of the code is heavily inspired by excellent Barista from Darcy Laycock.
48
+
49
+ Copyright (c) 2010 Julien Guimont. See LICENSE for details.
50
+ Copyright (c) 2010 Darcy Laycock. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
12
+ require "remote_i18n/version"
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "remote_i18n"
18
+ gem.homepage = "http://github.com/juggy/remote_i18n"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{I18n for Javascript with Rails}
21
+ gem.description = %Q{RemoteI18n helps developer export translated messages from a Rails server to a Javascript/HTML client application. More and more we need to use templates in javascript to create new HTML segments, but when you are building an I18n application you need to make sure that those segments contains the right language. This is what this project is for. For now it bundle the Rails translations into different javascript file ready for consumption.}
22
+ gem.version = RemoteI18n::Version::STRING
23
+ gem.email = "julien@porkepic.com"
24
+ gem.authors = ["Julien Guimont"]
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
@@ -0,0 +1,19 @@
1
+ module RemoteI18n
2
+ class Filter
3
+
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ dup._call(env)
10
+ end
11
+
12
+ def _call(env)
13
+ RemoteI18n.debug 'Compiling all translations'
14
+ RemoteI18n.compile_all!
15
+ @app.call env
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module RemoteI18n
2
+ module Integration
3
+ module Rails3
4
+ class Railtie < Rails::Railtie
5
+
6
+ rake_tasks do
7
+ load RemoteI18n.library_root.join('remote_i18n/tasks/remote_i18n.rake').to_s
8
+ end
9
+
10
+ initializer 'remote_i18n.wrap_filter' do
11
+ config.app_middleware.use RemoteI18n::Filter if RemoteI18n.add_filter?
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ module RemoteI18n
2
+ module Integration
3
+
4
+ autoload :Rails3, 'remote_i18n/integration/rails3'
5
+
6
+ def self.setup
7
+ setup_rails if defined?(Rails)
8
+ setup_sinatra if defined?(::Sinatra)
9
+ end
10
+
11
+ def self.setup_rails
12
+ case Rails::VERSION::MAJOR
13
+ when 3
14
+ Rails3
15
+ when 2
16
+ throw "Not supported yet"
17
+ end
18
+ end
19
+
20
+ def self.setup_sinatra
21
+ throw "Not supported yet"
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ namespace :remote_i18n do
2
+
3
+ desc "Compiles translations to javascripts into public/javascripts"
4
+ task :compile => :environment do
5
+ RemoteI18n.compile_all!
6
+ end
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ module RemoteI18n
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = [MAJOR, MINOR, PATCH].join(".")
7
+ end
8
+ end
@@ -0,0 +1,126 @@
1
+ require 'i18n'
2
+
3
+ module RemoteI18n
4
+ autoload :Integration, 'remote_i18n/integration'
5
+ autoload :Filter, 'remote_i18n/filter'
6
+
7
+ class << self
8
+
9
+ def configure
10
+ yield self if block_given?
11
+ end
12
+
13
+ def library_root
14
+ @library_root ||= Pathname(__FILE__).dirname
15
+ end
16
+
17
+ def default_for_add_filter
18
+ local_env?
19
+ end
20
+
21
+ def local_env?
22
+ %w(test development).include? RemoteI18n.env
23
+ end
24
+
25
+ def env
26
+ @env ||= default_for_env
27
+ end
28
+
29
+ def env=(value)
30
+ @env = value.to_s.strip
31
+ @env = nil if @env == ''
32
+ end
33
+
34
+ def default_for_env
35
+ return Rails.env.to_s if defined?(Rails.env)
36
+ ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
37
+ end
38
+
39
+ def add_filter?
40
+ local_env?
41
+ end
42
+
43
+ def default_for_logger
44
+ if defined?(Rails.logger)
45
+ Rails.logger
46
+ else
47
+ require 'logger'
48
+ Logger.new(STDOUT)
49
+ end
50
+ end
51
+
52
+ def logger
53
+ @logger ||= default_for_logger
54
+ end
55
+
56
+ def logger=(value)
57
+ @logger = value
58
+ end
59
+
60
+ def debug(message)
61
+ logger.debug "[RemoteI18n] #{message}"
62
+ end
63
+
64
+ def app_root
65
+ @app_root ||= default_for_app_root
66
+ end
67
+
68
+ def app_root=(value)
69
+ @app_root = value.nil? ? nil : Pathname(value.to_s)
70
+ end
71
+
72
+ def output_root
73
+ @output_root ||= app_root.join("public", "javascripts")
74
+ end
75
+
76
+ def output_root=(value)
77
+ @output_root = value.nil? ? nil : Pathname(value.to_s)
78
+ end
79
+
80
+ def default_for_app_root
81
+ if defined?(Rails.root)
82
+ Rails.root
83
+ else
84
+ Pathname(Dir.pwd)
85
+ end
86
+ end
87
+
88
+ def translate(opts)
89
+ translations << opts
90
+ end
91
+
92
+ def translations
93
+ @translations ||= []
94
+ end
95
+
96
+ def compile_all!
97
+ I18n.backend.send("init_translations")
98
+ languages = I18n.backend.send("translations")
99
+ default_locale = I18n.locale
100
+ languages.each_key do |locale|
101
+ I18n.locale = locale
102
+ translations.each do |t|
103
+ name = t[:name]
104
+ i18n_path = t[:i18n_path]
105
+ only = t[:only]
106
+
107
+ next if !(only.include? locale.to_s)
108
+
109
+ output = I18n.translate(i18n_path)
110
+ path = output_root.join("#{name}.#{locale}.js")
111
+
112
+ compiled_output = "if(this.T === null || this.T == undefined)this.T = {};this.T.#{name} = #{output.to_json}"
113
+
114
+ FileUtils.mkdir_p File.dirname(path)
115
+ File.open(path, "w+") { |f| f.write compiled_output }
116
+ end
117
+ end
118
+ I18n.locale = default_locale
119
+ end
120
+
121
+ end
122
+
123
+ # Setup integration by default.
124
+ Integration.setup
125
+
126
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{remote_i18n}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Julien Guimont"]
12
+ s.date = %q{2011-01-14}
13
+ s.description = %q{RemoteI18n helps developer export translated messages from a Rails server to a Javascript/HTML client application. More and more we need to use templates in javascript to create new HTML segments, but when you are building an I18n application you need to make sure that those segments contains the right language. This is what this project is for. For now it bundle the Rails translations into different javascript file ready for consumption.}
14
+ s.email = %q{julien@porkepic.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.md",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.md",
23
+ "README.md",
24
+ "Rakefile",
25
+ "lib/remote_i18n.rb",
26
+ "lib/remote_i18n/filter.rb",
27
+ "lib/remote_i18n/integration.rb",
28
+ "lib/remote_i18n/integration/rails3.rb",
29
+ "lib/remote_i18n/tasks/remote_i18n.rake",
30
+ "lib/remote_i18n/version.rb",
31
+ "remote_i18n.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/juggy/remote_i18n}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{I18n for Javascript with Rails}
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
46
+ else
47
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ end
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remote_i18n
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Julien Guimont
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-14 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ prerelease: false
24
+ name: bundler
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 23
31
+ segments:
32
+ - 1
33
+ - 0
34
+ - 0
35
+ version: 1.0.0
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ prerelease: false
40
+ name: jeweler
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ hash: 7
47
+ segments:
48
+ - 1
49
+ - 5
50
+ - 2
51
+ version: 1.5.2
52
+ requirement: *id002
53
+ description: RemoteI18n helps developer export translated messages from a Rails server to a Javascript/HTML client application. More and more we need to use templates in javascript to create new HTML segments, but when you are building an I18n application you need to make sure that those segments contains the right language. This is what this project is for. For now it bundle the Rails translations into different javascript file ready for consumption.
54
+ email: julien@porkepic.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - LICENSE.md
61
+ - README.md
62
+ files:
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.md
66
+ - README.md
67
+ - Rakefile
68
+ - lib/remote_i18n.rb
69
+ - lib/remote_i18n/filter.rb
70
+ - lib/remote_i18n/integration.rb
71
+ - lib/remote_i18n/integration/rails3.rb
72
+ - lib/remote_i18n/tasks/remote_i18n.rake
73
+ - lib/remote_i18n/version.rb
74
+ - remote_i18n.gemspec
75
+ has_rdoc: true
76
+ homepage: http://github.com/juggy/remote_i18n
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: I18n for Javascript with Rails
109
+ test_files: []
110
+