From b145bf1db745893bbae58d8c8625f53151aa0b5a Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Fri, 9 Jul 2010 10:23:19 -0300 Subject: [PATCH] Distribute I18n-js as Rails 3 gem. --- .gitignore | 3 +- Rakefile | 21 ++++++++++++- i18n-js.gemspec | 61 +++++++++++++++++++++++++++++++++++++ lib/i18n-js.rb | 24 +++++++++------ lib/i18n-js/railtie.rb | 9 ++++++ lib/i18n-js/version.rb | 10 ++++++ {lib => source}/i18n-js.yml | 0 {lib => source}/i18n.js | 0 test/i18n-test.html | 8 ++--- test/i18n_js_test.rb | 30 +++++++++--------- test/test_helper.rb | 3 +- 11 files changed, 138 insertions(+), 31 deletions(-) create mode 100644 i18n-js.gemspec create mode 100644 lib/i18n-js/railtie.rb create mode 100644 lib/i18n-js/version.rb rename {lib => source}/i18n-js.yml (100%) rename {lib => source}/i18n.js (100%) diff --git a/.gitignore b/.gitignore index 9d98ae4a..e20ef250 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ doc -coverage \ No newline at end of file +coverage +pkg \ No newline at end of file diff --git a/Rakefile b/Rakefile index 4f0ba15a..24099c7b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,7 @@ require 'rake' require 'rake/testtask' -require 'hanna/rdoctask' +require 'rake/rdoctask' +require 'lib/i18n-js/version' desc 'Default: run unit tests.' task :default => :test @@ -21,3 +22,21 @@ Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('README.rdoc') rdoc.rdoc_files.include('lib/**/*.rb') end + +begin + require 'jeweler' + + JEWEL = Jeweler::Tasks.new do |gem| + gem.name = "i18n-js" + gem.email = "fnando.vieira@gmail.com" + gem.homepage = "http://github.com/fnando/i18n-js" + gem.authors = ["Nando Vieira"] + gem.version = SimplesIdeias::I18n::Version::STRING + gem.summary = "It's a small library to provide the Rails I18n translations on the Javascript." + gem.files = FileList["README.rdoc", "init.rb", "install.rb", "{lib,test,source}/**/*", "Rakefile"] + end + + Jeweler::GemcutterTasks.new +rescue LoadError => e + puts "[JEWELER] You can't build a gem until you install jeweler with `gem install jeweler`" +end diff --git a/i18n-js.gemspec b/i18n-js.gemspec new file mode 100644 index 00000000..e868cc17 --- /dev/null +++ b/i18n-js.gemspec @@ -0,0 +1,61 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{i18n-js} + s.version = "0.1.0" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Nando Vieira"] + s.date = %q{2010-07-09} + s.email = %q{fnando.vieira@gmail.com} + s.extra_rdoc_files = [ + "README.rdoc" + ] + s.files = [ + "README.rdoc", + "Rakefile", + "init.rb", + "install.rb", + "lib/i18n-js.rb", + "lib/i18n-js.yml", + "lib/i18n-js/railtie.rb", + "lib/i18n-js/version.rb", + "lib/i18n.js", + "lib/tasks/i18n-js_tasks.rake", + "test/i18n-test.html", + "test/i18n-test.js", + "test/i18n_js_test.rb", + "test/jsunittest/jsunittest.js", + "test/jsunittest/unittest.css", + "test/resources/custom_path.yml", + "test/resources/default.yml", + "test/resources/locales.yml", + "test/resources/multiple_files.yml", + "test/resources/no_scope.yml", + "test/resources/simple_scope.yml", + "test/test_helper.rb" + ] + s.homepage = %q{http://github.com/fnando/i18n-js} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.6} + s.summary = %q{It's a small library to provide the Rails I18n translations on the Javascript.} + s.test_files = [ + "test/i18n_js_test.rb", + "test/test_helper.rb" + ] + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + else + end + else + end +end + diff --git a/lib/i18n-js.rb b/lib/i18n-js.rb index 0bc29c4d..3c3d2fe4 100644 --- a/lib/i18n-js.rb +++ b/lib/i18n-js.rb @@ -1,15 +1,21 @@ +require "FileUtils" unless defined?(FileUtils) + module SimplesIdeias module I18n extend self + require "i18n-js/railtie" if Rails.version >= "3.0" + # deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809 MERGER = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2 } - # Set configuration file path - CONFIG_FILE = Rails.root.join("config/i18n-js.yml") + def config_file + Rails.root.join("config/i18n-js.yml") + end - # Set i18n.js output path - JAVASCRIPT_FILE = Rails.root.join("public/javascripts/i18n.js") + def javascript_file + Rails.root.join("public/javascripts/i18n.js") + end # Export translations to JavaScript, considering settings # from configuration file @@ -33,26 +39,26 @@ def export! # Load configuration file for partial exporting and # custom output directory def config - HashWithIndifferentAccess.new YAML.load_file(CONFIG_FILE) + HashWithIndifferentAccess.new YAML.load_file(config_file) end # Check if configuration file exist def config? - File.file? CONFIG_FILE + File.file? config_file end # Copy configuration and JavaScript library files to # SimplesIdeias::I18n::CONFIG_FILE and public/i18n.js. def setup! - FileUtils.cp File.dirname(__FILE__) + "/i18n.js", JAVASCRIPT_FILE - FileUtils.cp(File.dirname(__FILE__) + "/i18n-js.yml", CONFIG_FILE) unless config? + FileUtils.cp File.dirname(__FILE__) + "/../source/i18n.js", javascript_file + FileUtils.cp(File.dirname(__FILE__) + "/../source/i18n-js.yml", config_file) unless config? end # Retrieve an updated JavaScript library from Github. def update! require "open-uri" contents = open("http://github.com/fnando/i18n-js/raw/master/lib/i18n.js").read - File.open(JAVASCRIPT_FILE, "w+") {|f| f << contents} + File.open(javascript_file, "w+") {|f| f << contents} end # Convert translations to JSON string and save file. diff --git a/lib/i18n-js/railtie.rb b/lib/i18n-js/railtie.rb new file mode 100644 index 00000000..f26a12e8 --- /dev/null +++ b/lib/i18n-js/railtie.rb @@ -0,0 +1,9 @@ +module SimplesIdeias + module I18n + class Railtie < Rails::Railtie + rake_tasks do + load File.dirname(__FILE__) + "/../tasks/i18n-js_tasks.rake" + end + end + end +end diff --git a/lib/i18n-js/version.rb b/lib/i18n-js/version.rb new file mode 100644 index 00000000..d4cb9341 --- /dev/null +++ b/lib/i18n-js/version.rb @@ -0,0 +1,10 @@ +module SimplesIdeias + module I18n + module Version + MAJOR = 0 + MINOR = 1 + PATCH = 0 + STRING = "#{MAJOR}.#{MINOR}.#{PATCH}" + end + end +end diff --git a/lib/i18n-js.yml b/source/i18n-js.yml similarity index 100% rename from lib/i18n-js.yml rename to source/i18n-js.yml diff --git a/lib/i18n.js b/source/i18n.js similarity index 100% rename from lib/i18n.js rename to source/i18n.js diff --git a/test/i18n-test.html b/test/i18n-test.html index 41127c3e..0dadf6e9 100644 --- a/test/i18n-test.html +++ b/test/i18n-test.html @@ -6,7 +6,7 @@ - +