Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract I18n::MissingTranslations from adva-cms2
- Loading branch information
Sven Fuchs
committed
Nov 6, 2010
0 parents
commit bcdfede
Showing
12 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.bundle/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source :rubygems | ||
gemspec | ||
|
||
gem 'i18n', :git => 'git://github.com/svenfuchs/i18n.git', :ref => '0.5.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
GIT | ||
remote: git://github.com/svenfuchs/i18n.git | ||
revision: 23c9feffea8f23f686020b64770ca4dae030b545 | ||
ref: 0.5.0 | ||
specs: | ||
i18n (0.4.2) | ||
|
||
PATH | ||
remote: . | ||
specs: | ||
i18n-missing_translations (0.0.1) | ||
|
||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
test_declarative (0.0.4) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
i18n! | ||
i18n-missing_translations! | ||
test_declarative |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = false | ||
end | ||
|
||
task :default => :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# encoding: utf-8 | ||
|
||
$:.unshift File.expand_path('../lib', __FILE__) | ||
require 'i18n/missing_translations/version' | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "i18n-missing_translations" | ||
s.version = I18n::MissingTranslations::VERSION | ||
s.authors = ["Sven Fuchs"] | ||
s.email = "svenfuchs@artweb-design.de" | ||
s.homepage = "http://github.com/svenfuchs/i18n-missing_translations" | ||
s.summary = "[summary]" | ||
s.description = "[description]" | ||
|
||
s.files = `git ls-files app lib`.split("\n") | ||
s.platform = Gem::Platform::RUBY | ||
s.require_path = 'lib' | ||
s.rubyforge_project = '[none]' | ||
|
||
# s.add_dependency 'i18n', '~> 0.5.0' | ||
s.add_development_dependency 'test_declarative' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'i18n' | ||
require 'i18n/exceptions' | ||
|
||
module I18n | ||
class << self | ||
attr_writer :missing_translations | ||
|
||
def missing_translations | ||
@missing_translations ||= MissingTranslations::Log.new | ||
end | ||
end | ||
|
||
class MissingTranslations | ||
autoload :Log, 'i18n/missing_translations/log' | ||
autoload :Handler, 'i18n/missing_translations/handler' | ||
|
||
attr_reader :app, :filename | ||
|
||
def initialize(app, filename = nil) | ||
@app = app | ||
@filename = filename || guess_filename | ||
end | ||
|
||
def call(*args) | ||
log.read(filename) | ||
app.call(*args).tap { log.write(filename) } | ||
end | ||
|
||
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 | ||
end | ||
end | ||
|
||
ExceptionHandler.send(:include, MissingTranslations::Handler) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module I18n | ||
class MissingTranslations | ||
module Handler | ||
def call(exception, locale, key, options) | ||
I18n.missing_translations.log(exception.keys) if MissingTranslationData === exception | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'yaml' | ||
require 'fileutils' | ||
|
||
module I18n | ||
class MissingTranslations | ||
class Log < Hash | ||
def log(keys) | ||
keys = keys.dup | ||
key = keys.pop.to_s | ||
log = keys.inject(self) { |log, k| log.key?(k.to_s) ? log[k.to_s] : log[k.to_s] = {} } | ||
log[key] = key.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize } | ||
end | ||
|
||
def dump(out = $stdout) | ||
out.puts(to_yml) unless empty? | ||
end | ||
|
||
def read(filename) | ||
data = YAML.load_file(filename) rescue nil | ||
self.replace(data) if data | ||
end | ||
|
||
def write(filename) | ||
FileUtils.mkdir_p(File.dirname(filename)) | ||
File.open(filename, 'w+') { |f| f.write(to_yml) } | ||
end | ||
|
||
def to_yml | ||
empty? ? '' : YAML.dump(Hash[*to_a.flatten]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module I18n | ||
class MissingTranslations | ||
VERSION = "0.0.1" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
|
||
module AdvaCoreTests | ||
class I18nMissingTranslationsLogTest < Test::Unit::TestCase | ||
attr_reader :filename | ||
|
||
def setup | ||
@filename = '/tmp/test_missing_translations.log' | ||
FileUtils.mkdir_p(File.dirname(filename)) | ||
end | ||
|
||
def teardown | ||
File.rm(filename) rescue nil | ||
I18n.missing_translations.clear | ||
end | ||
|
||
test 'logs to a memory hash' do | ||
log = I18n::MissingTranslations::Log.new | ||
log.log([:missing_translations, :foo]) | ||
log.log([:missing_translations, :bar, :baz, :boz]) | ||
log.log([:missing_translations, :bar, :baz, :buz]) | ||
|
||
expected = { 'missing_translations' => { 'foo' => 'Foo', 'bar' => { 'baz' => { 'boz' => 'Boz', 'buz' => 'Buz' } } } } | ||
assert_equal expected, log | ||
end | ||
|
||
test 'dumps memory log as a yaml hash' do | ||
log = I18n::MissingTranslations::Log.new | ||
log.log([:missing_translations, :foo, :bar]) | ||
log.dump(out = StringIO.new) | ||
|
||
expected = '--- missing_translations: foo: bar: Bar ' | ||
assert_equal expected, out.string.gsub("\n", ' ') | ||
end | ||
|
||
test 'works as a rack middleware' do | ||
File.open(filename, 'w+') { |f| f.write(YAML.dump('en' => { 'foo' => 'Foo' })) } | ||
middleware = I18n::MissingTranslations.new(lambda { |*| I18n.t(:missing) }, filename) | ||
middleware.call({}) | ||
assert_equal({ 'en' => { 'foo' => 'Foo', 'missing' => 'Missing' }}, YAML.load_file(filename)) | ||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rubygems' | ||
require 'test/unit' | ||
require 'bundler/setup' | ||
require 'test_declarative' | ||
|
||
$: << File.expand_path('../../lib', __FILE__) | ||
require 'i18n/missing_translations' |