Skip to content

Commit

Permalink
Distribute I18n-js as Rails 3 gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Jul 9, 2010
1 parent e4f7e83 commit b145bf1
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
doc
coverage
coverage
pkg
21 changes: 20 additions & 1 deletion 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
Expand All @@ -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
61 changes: 61 additions & 0 deletions 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

24 changes: 15 additions & 9 deletions 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
Expand All @@ -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
# <tt>SimplesIdeias::I18n::CONFIG_FILE</tt> and <tt>public/i18n.js</tt>.
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.
Expand Down
9 changes: 9 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions 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
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions test/i18n-test.html
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="jsunittest/jsunittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="jsunittest/unittest.css" type="text/css" />

<style type="text/css" media="screen">
#logger p {
background: #ffc;
Expand All @@ -23,7 +23,7 @@
tag.innerHTML += "<p><strong>" + name + ":</strong> " + message + "</p>";
}
</script>
<script src="../lib/i18n.js" type="text/javascript"></script>
<script src="../source/i18n.js" type="text/javascript"></script>
</head>
<body>

Expand All @@ -37,10 +37,10 @@ <h1>JavaScript unit test file</h1>

<!-- Log output (one per Runner, via {testLog: "testlog"} option)-->
<div id="testlog"></div>

<!-- General debugger -->
<div id="logger"></div>

<!-- Put sample/test html here -->
<div id="sample">
</div>
Expand Down
30 changes: 15 additions & 15 deletions test/i18n_js_test.rb
Expand Up @@ -20,16 +20,16 @@ class I18nJSTest < ActiveSupport::TestCase
end

test "copy configuration file" do
assert_equal false, File.file?(SimplesIdeias::I18n::CONFIG_FILE)
assert_equal false, File.file?(SimplesIdeias::I18n.config_file)
SimplesIdeias::I18n.setup!
assert File.file?(SimplesIdeias::I18n::CONFIG_FILE)
assert File.file?(SimplesIdeias::I18n.config_file)
end

test "don't overwrite existing configuration file" do
File.open(SimplesIdeias::I18n::CONFIG_FILE, "w+") {|f| f << "ORIGINAL"}
File.open(SimplesIdeias::I18n.config_file, "w+") {|f| f << "ORIGINAL"}
SimplesIdeias::I18n.setup!

assert_equal "ORIGINAL", File.read(SimplesIdeias::I18n::CONFIG_FILE)
assert_equal "ORIGINAL", File.read(SimplesIdeias::I18n.config_file)
end

test "copy JavaScript library" do
Expand Down Expand Up @@ -150,19 +150,19 @@ class I18nJSTest < ActiveSupport::TestCase

SimplesIdeias::I18n.setup!
SimplesIdeias::I18n.update!
assert_equal "UPDATED", File.read(SimplesIdeias::I18n::JAVASCRIPT_FILE)
assert_equal "UPDATED", File.read(SimplesIdeias::I18n.javascript_file)
end

private
# Set the configuration as the current one
def set_config(path)
config = HashWithIndifferentAccess.new(YAML.load_file(File.dirname(__FILE__) + "/resources/#{path}"))
SimplesIdeias::I18n.expects(:config?).returns(true)
SimplesIdeias::I18n.expects(:config).returns(config)
end
# Set the configuration as the current one
def set_config(path)
config = HashWithIndifferentAccess.new(YAML.load_file(File.dirname(__FILE__) + "/resources/#{path}"))
SimplesIdeias::I18n.expects(:config?).returns(true)
SimplesIdeias::I18n.expects(:config).returns(config)
end

# Shortcut to SimplesIdeias::I18n.translations
def translations
SimplesIdeias::I18n.translations
end
# Shortcut to SimplesIdeias::I18n.translations
def translations
SimplesIdeias::I18n.translations
end
end
3 changes: 2 additions & 1 deletion test/test_helper.rb
Expand Up @@ -8,6 +8,7 @@
require "active_support"
end

require "active_support/version"
require "active_support/test_case"
require "ostruct"
require "pathname"
Expand All @@ -19,6 +20,6 @@

# Stub Rails.root, so we don"t need to load the whole Rails environment.
# Be careful! The specified folder will be removed!
Rails = OpenStruct.new(:root => Pathname.new(File.dirname(__FILE__) + "/tmp"))
Rails = OpenStruct.new(:root => Pathname.new(File.dirname(__FILE__) + "/tmp"), :version => "0")

require File.dirname(__FILE__) + "/../lib/i18n-js"

0 comments on commit b145bf1

Please sign in to comment.