Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use [locales_dir]/missing_translations.yml instead of a log file

  • Loading branch information...
commit 350f7f9c4d0e35d23fe839f23ff539917cf26956 1 parent 5235667
@svenfuchs authored
View
19 README.textile
@@ -21,9 +21,22 @@ The following hooks up the I18n::MissingTranslations middleware in development m
<pre>
require 'i18n/missing_translations'
-config.app_middleware.use(I18n::MissingTranslations, 'path/to/missing_translations.log') if Rails.env.development?
+config.app_middleware.use(I18n::MissingTranslations) if Rails.env.development?
</pre>
-This will then log missing tranlations in development mode to the given file. You can also omit the filename and it will default to @"#{Dir.pwd}/log/missing_translations.log"@.
+This will then log missing tranlations in development mode to a file @missing_translations.yml@ in your locales dir, which is @config/locales@ if present (i.e. per Rails convention) or the current directory otherwise. You can also pass a filename as an argument:
-The middleware reads *and* writes per request. That means that on subsequent requests missing translations are *added* to the log file. So if you go ahead and copy translations from the log to your locale file you will also want to clear the log file.
+<pre>
+config.app_middleware.use(I18n::MissingTranslations, 'path/to/locales/missing.yml')
+</pre>
+
+The middleware reads *and* writes per request. That means that on subsequent requests missing translations are *added* to the @missing_translations.yml@ file. So if you go ahead and copy translations from the @missing_translations.yml@ to your actual locale files you will also want to clear or delete @missing_translations.yml@.
+
+NOTE that Rails (3.0.1) does not pick up new locale files between requests. That effectively means that changes to the @missing_translations.yml@ file will be overwritten unless you restart the server. Thus your workflow for finding and moving missing translation keys should look something like this:
+
+* start the server
+* click around/work on stuff
+* check @config/locales/missing_translations.yml@
+* copy any missing translation keys to your actual locale files and correct the translations
+* delete or clear @config/locales/missing_translations.yml@
+* restart the server
View
10 lib/i18n/missing_translations.rb
@@ -18,7 +18,7 @@ class MissingTranslations
def initialize(app, filename = nil)
@app = app
- @filename = filename || guess_filename
+ @filename = filename || "#{locale_dir}/missing_translations.yml"
end
def call(*args)
@@ -30,12 +30,8 @@ def log
I18n.missing_translations
end
- def guess_filename
- if File.directory?("#{Dir.pwd}/log")
- "#{Dir.pwd}/log/missing_translations.log"
- else
- "/tmp/#{File.dirname(Dir.pwd)}-missing_translations.log"
- end
+ def locale_dir
+ File.directory?("#{Dir.pwd}/config/locales") ? "#{Dir.pwd}/config/locales" : Dir.pwd
end
end
View
9 lib/i18n/missing_translations/log.rb
@@ -1,5 +1,4 @@
require 'yaml'
-require 'fileutils'
module I18n
class MissingTranslations
@@ -16,17 +15,15 @@ def dump(out = $stdout)
end
def read(filename)
- data = YAML.load_file(filename) rescue nil
- self.replace(data) if data
+ self.replace(YAML.load_file(filename)) rescue nil
end
def write(filename)
- FileUtils.mkdir_p(File.dirname(filename))
- File.open(filename, 'w+') { |f| f.write(to_yml) }
+ File.open(filename, 'w+') { |f| f.write(to_yml) } unless empty?
end
def to_yml
- empty? ? '' : YAML.dump(Hash[*to_a.flatten])
+ YAML.dump(Hash[*to_a.flatten]).split("\n").map(&:rstrip).join("\n")
end
end
end
View
15 test/missing_translations_test.rb
@@ -5,7 +5,7 @@ class I18nMissingTranslationsLogTest < Test::Unit::TestCase
attr_reader :filename
def setup
- @filename = '/tmp/test_missing_translations.log'
+ @filename = '/tmp/i18n_missing_translations/missing_translations.yml'
FileUtils.mkdir_p(File.dirname(filename))
end
@@ -39,6 +39,19 @@ def teardown
middleware.call({})
assert_equal({ 'en' => { 'foo' => 'Foo', 'missing' => 'Missing' }}, YAML.load_file(filename))
end
+
+ test 'works as a middleware that reads an existing locale file, subsequently adds missing translations and writes them back' do
+ File.open(filename, 'w+') { |f| f.write(YAML.dump('en' => { 'foo' => 'Foo' })) }
+
+ app = lambda { I18n.t(:first_miss) }
+ middleware = I18n::MissingTranslations.new(lambda { |*| app.call }, filename)
+ middleware.call({})
+ assert_equal({ 'en' => { 'foo' => 'Foo', 'first_miss' => 'First Miss' }}, YAML.load_file(filename))
+
+ app = lambda { I18n.t(:second_miss) }
+ middleware.call({})
+ assert_equal({ 'en' => { 'foo' => 'Foo', 'first_miss' => 'First Miss', 'second_miss' => 'Second Miss' }}, YAML.load_file(filename))
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.