Navigation Menu

Skip to content

Commit

Permalink
extract I18n::MissingTranslations from adva-cms2
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Nov 6, 2010
0 parents commit bcdfede
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.bundle/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source :rubygems
gemspec

gem 'i18n', :git => 'git://github.com/svenfuchs/i18n.git', :ref => '0.5.0'
24 changes: 24 additions & 0 deletions Gemfile.lock
@@ -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 added README.textile
Empty file.
10 changes: 10 additions & 0 deletions Rakefile
@@ -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
22 changes: 22 additions & 0 deletions i18n-missing_translations.gemspec
@@ -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
43 changes: 43 additions & 0 deletions lib/i18n/missing_translations.rb
@@ -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
10 changes: 10 additions & 0 deletions lib/i18n/missing_translations/handler.rb
@@ -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
33 changes: 33 additions & 0 deletions lib/i18n/missing_translations/log.rb
@@ -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
5 changes: 5 additions & 0 deletions lib/i18n/missing_translations/version.rb
@@ -0,0 +1,5 @@
module I18n
class MissingTranslations
VERSION = "0.0.1"
end
end
45 changes: 45 additions & 0 deletions test/missing_translations_test.rb
@@ -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


7 changes: 7 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,7 @@
require 'rubygems'
require 'test/unit'
require 'bundler/setup'
require 'test_declarative'

$: << File.expand_path('../../lib', __FILE__)
require 'i18n/missing_translations'

0 comments on commit bcdfede

Please sign in to comment.