Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added middleware that automatically exports translations with caching…

… support.
  • Loading branch information...
commit 06b1b1981331ddd062bc69455aee3150bad4ffa0 1 parent 265041e
@fnando authored
View
8 README.rdoc
@@ -44,7 +44,13 @@ Examples:
- file: 'public/javascripts/path-to-your-second-file.js'
only: ['*.activerecord', '*.admin.*.title']
-If <tt>only</tt> is omitted all the translations will be saved
+If <tt>only</tt> is omitted all the translations will be saved. Also, make sure you add that initial <tt>*</tt>; it specifies that all languages will be exported. If you want to export only one language, you can do something like this:
+
+ translations:
+ - file: 'public/javascripts/en.js'
+ only: 'en.*'
+ - file: 'public/javascripts/pt-BR.js'
+ only: 'pt-BR.*'
To find more examples on how to use the configuration file please refer to the tests.
View
3  config/i18n-js.yml
@@ -1,6 +1,3 @@
-# Find more details about this configuration file
-# at http://github.com/fnando/i18n-js
-
# Split context in several files.
# By default only one file with all translations is exported and
# no configuration is required. Your settings for asset pipeline
View
10 lib/i18n-js.rb
@@ -6,6 +6,7 @@ module I18n
require "i18n-js/railtie" if Rails.version >= "3.0"
require "i18n-js/engine" if Rails.version >= "3.1"
+ require "i18n-js/middleware"
# 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 }
@@ -15,7 +16,7 @@ def config_file
end
def export_dir
- if Rails.version >= "3.1" && Rails.application.config.assets.enabled
+ if Rails.version >= "3.1" && Rails.configuration.assets.enabled
"app/assets/javascripts/i18n"
else
"public/javascripts"
@@ -49,17 +50,12 @@ def export!
# custom output directory
def config
if config?
- YAML.load_file(config_file).with_indifferent_access
+ (YAML.load_file(config_file) || {}).with_indifferent_access
else
{}
end
end
- # Check if translations can be automatically exported.
- def auto_export?
- Rails.env.development? || (config? && config[:auto_export])
- end
-
# Check if configuration file exist
def config?
File.file? config_file
View
52 lib/i18n-js/middleware.rb
@@ -0,0 +1,52 @@
+module SimplesIdeias
+ module I18n
+ class Middleware
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ @cache = nil
+ verify_locale_files!
+ @app.call(env)
+ end
+
+ private
+ def cache_path
+ @cache_path ||= Rails.root.join("tmp/cache/i18n-js.yml")
+ end
+
+ def cache
+ @cache ||= begin
+ if cache_path.exist?
+ YAML.load_file(cache_path) || {}
+ else
+ {}
+ end
+ end
+ end
+
+ def verify_locale_files!
+ valid_cache = []
+ changed_files = []
+
+ valid_cache.push cache_path.exist?
+ valid_cache.push ::I18n.load_path.size == cache.size
+
+ ::I18n.load_path.each do |path|
+ change = File.mtime(path).to_i
+ file_has_changed = change != cache[path]
+ valid_cache.push file_has_changed
+ changed_files << path if file_has_changed
+ cache[path] = change
+ end
+
+ File.open(cache_path, "w+") do |file|
+ file << cache.to_yaml
+ end
+
+ SimplesIdeias::I18n.export! unless valid_cache.all?
+ end
+ end
+ end
+end
View
6 lib/i18n-js/railtie.rb
@@ -5,10 +5,8 @@ class Railtie < Rails::Railtie
require "i18n-js/rake"
end
- config.to_prepare do
- SimplesIdeias::I18n.tap do |i18n|
- i18n.export! if i18n.auto_export?
- end
+ initializer "i18n-js.initialize" do |app|
+ app.config.middleware.use(Middleware) if Rails.env.development?
end
end
end
View
33 spec/i18n_spec.rb
@@ -50,6 +50,7 @@
end
it "loads configuration file" do
+ set_config "default.yml"
SimplesIdeias::I18n.setup!
SimplesIdeias::I18n.config?.should be_true
@@ -67,12 +68,6 @@
Rails.root.join(SimplesIdeias::I18n.export_dir, "translations.js").should be_file
end
- it "exports messages using the default configuration file" do
- set_config "default.yml"
- SimplesIdeias::I18n.should_receive(:save).with(translations, "public/javascripts/translations.js")
- SimplesIdeias::I18n.export!
- end
-
it "exports messages using custom output path" do
set_config "custom_path.yml"
SimplesIdeias::I18n.should_receive(:save).with(translations, "public/javascripts/translations/all.js")
@@ -159,34 +154,16 @@
File.read(SimplesIdeias::I18n.javascript_file).should == "UPDATED"
end
- describe "#auto_export?" do
- it "returns true for development mode" do
- Rails.env = stub(:development? => true)
- SimplesIdeias::I18n.should be_auto_export
- end
-
- it "returns false for production mode" do
- Rails.env = stub(:production? => true, :development? => false)
- SimplesIdeias::I18n.should_not be_auto_export
- end
-
- it "returns true for production when configuration is set" do
- Rails.env = stub(:production? => true, :development? => false)
- SimplesIdeias::I18n.stub :config? => true, :config => {:auto_export => true}
- SimplesIdeias::I18n.should be_auto_export
- end
- end
-
describe "#export_dir" do
it "detects Rails 3.1 with asset pipeline enabled" do
Rails.version = "3.1"
- Rails.stub_chain(:application, :config, :assets, :enabled => true)
+ Rails.stub_chain(:configuration, :assets, :enabled => true)
SimplesIdeias::I18n.export_dir == "vendor/assets/javascripts"
end
it "detects Rails 3.1 with asset pipeline disabled" do
Rails.version = "3.1"
- Rails.stub_chain(:application, :config, :assets, :enabled => false)
+ Rails.stub_chain(:configuration, :assets, :enabled => false)
SimplesIdeias::I18n.export_dir == "public/javascripts"
end
@@ -200,8 +177,8 @@
# Set the configuration as the current one
def set_config(path)
config = HashWithIndifferentAccess.new(YAML.load_file(File.dirname(__FILE__) + "/resources/#{path}"))
- SimplesIdeias::I18n.should_receive(:config?).and_return(true)
- SimplesIdeias::I18n.should_receive(:config).and_return(config)
+ SimplesIdeias::I18n.stub(:config? => true)
+ SimplesIdeias::I18n.stub(:config => config)
end
# Shortcut to SimplesIdeias::I18n.translations
Please sign in to comment.
Something went wrong with that request. Please try again.