Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

extract I18n::MissingTranslations from adva-cms2

  • Loading branch information...
commit bcdfede0a7e528c805eb3c8b92712b7541f75648 0 parents
Sven Fuchs authored
1  .gitignore
... ... @@ -0,0 +1 @@
  1 +.bundle/*
4 Gemfile
... ... @@ -0,0 +1,4 @@
  1 +source :rubygems
  2 +gemspec
  3 +
  4 +gem 'i18n', :git => 'git://github.com/svenfuchs/i18n.git', :ref => '0.5.0'
24 Gemfile.lock
... ... @@ -0,0 +1,24 @@
  1 +GIT
  2 + remote: git://github.com/svenfuchs/i18n.git
  3 + revision: 23c9feffea8f23f686020b64770ca4dae030b545
  4 + ref: 0.5.0
  5 + specs:
  6 + i18n (0.4.2)
  7 +
  8 +PATH
  9 + remote: .
  10 + specs:
  11 + i18n-missing_translations (0.0.1)
  12 +
  13 +GEM
  14 + remote: http://rubygems.org/
  15 + specs:
  16 + test_declarative (0.0.4)
  17 +
  18 +PLATFORMS
  19 + ruby
  20 +
  21 +DEPENDENCIES
  22 + i18n!
  23 + i18n-missing_translations!
  24 + test_declarative
0  README.textile
Source Rendered
No changes.
10 Rakefile
... ... @@ -0,0 +1,10 @@
  1 +require 'rake'
  2 +require 'rake/testtask'
  3 +
  4 +Rake::TestTask.new do |t|
  5 + t.libs << 'lib'
  6 + t.pattern = 'test/**/*_test.rb'
  7 + t.verbose = false
  8 +end
  9 +
  10 +task :default => :test
22 i18n-missing_translations.gemspec
... ... @@ -0,0 +1,22 @@
  1 +# encoding: utf-8
  2 +
  3 +$:.unshift File.expand_path('../lib', __FILE__)
  4 +require 'i18n/missing_translations/version'
  5 +
  6 +Gem::Specification.new do |s|
  7 + s.name = "i18n-missing_translations"
  8 + s.version = I18n::MissingTranslations::VERSION
  9 + s.authors = ["Sven Fuchs"]
  10 + s.email = "svenfuchs@artweb-design.de"
  11 + s.homepage = "http://github.com/svenfuchs/i18n-missing_translations"
  12 + s.summary = "[summary]"
  13 + s.description = "[description]"
  14 +
  15 + s.files = `git ls-files app lib`.split("\n")
  16 + s.platform = Gem::Platform::RUBY
  17 + s.require_path = 'lib'
  18 + s.rubyforge_project = '[none]'
  19 +
  20 + # s.add_dependency 'i18n', '~> 0.5.0'
  21 + s.add_development_dependency 'test_declarative'
  22 +end
43 lib/i18n/missing_translations.rb
... ... @@ -0,0 +1,43 @@
  1 +require 'i18n'
  2 +require 'i18n/exceptions'
  3 +
  4 +module I18n
  5 + class << self
  6 + attr_writer :missing_translations
  7 +
  8 + def missing_translations
  9 + @missing_translations ||= MissingTranslations::Log.new
  10 + end
  11 + end
  12 +
  13 + class MissingTranslations
  14 + autoload :Log, 'i18n/missing_translations/log'
  15 + autoload :Handler, 'i18n/missing_translations/handler'
  16 +
  17 + attr_reader :app, :filename
  18 +
  19 + def initialize(app, filename = nil)
  20 + @app = app
  21 + @filename = filename || guess_filename
  22 + end
  23 +
  24 + def call(*args)
  25 + log.read(filename)
  26 + app.call(*args).tap { log.write(filename) }
  27 + end
  28 +
  29 + def log
  30 + I18n.missing_translations
  31 + end
  32 +
  33 + def guess_filename
  34 + if File.directory?("#{Dir.pwd}/log")
  35 + "#{Dir.pwd}/log/missing_translations.log"
  36 + else
  37 + "/tmp/#{File.dirname(Dir.pwd)}-missing_translations.log"
  38 + end
  39 + end
  40 + end
  41 +
  42 + ExceptionHandler.send(:include, MissingTranslations::Handler)
  43 +end
10 lib/i18n/missing_translations/handler.rb
... ... @@ -0,0 +1,10 @@
  1 +module I18n
  2 + class MissingTranslations
  3 + module Handler
  4 + def call(exception, locale, key, options)
  5 + I18n.missing_translations.log(exception.keys) if MissingTranslationData === exception
  6 + super
  7 + end
  8 + end
  9 + end
  10 +end
33 lib/i18n/missing_translations/log.rb
... ... @@ -0,0 +1,33 @@
  1 +require 'yaml'
  2 +require 'fileutils'
  3 +
  4 +module I18n
  5 + class MissingTranslations
  6 + class Log < Hash
  7 + def log(keys)
  8 + keys = keys.dup
  9 + key = keys.pop.to_s
  10 + log = keys.inject(self) { |log, k| log.key?(k.to_s) ? log[k.to_s] : log[k.to_s] = {} }
  11 + log[key] = key.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
  12 + end
  13 +
  14 + def dump(out = $stdout)
  15 + out.puts(to_yml) unless empty?
  16 + end
  17 +
  18 + def read(filename)
  19 + data = YAML.load_file(filename) rescue nil
  20 + self.replace(data) if data
  21 + end
  22 +
  23 + def write(filename)
  24 + FileUtils.mkdir_p(File.dirname(filename))
  25 + File.open(filename, 'w+') { |f| f.write(to_yml) }
  26 + end
  27 +
  28 + def to_yml
  29 + empty? ? '' : YAML.dump(Hash[*to_a.flatten])
  30 + end
  31 + end
  32 + end
  33 +end
5 lib/i18n/missing_translations/version.rb
... ... @@ -0,0 +1,5 @@
  1 +module I18n
  2 + class MissingTranslations
  3 + VERSION = "0.0.1"
  4 + end
  5 +end
45 test/missing_translations_test.rb
... ... @@ -0,0 +1,45 @@
  1 +require File.expand_path('../test_helper', __FILE__)
  2 +
  3 +module AdvaCoreTests
  4 + class I18nMissingTranslationsLogTest < Test::Unit::TestCase
  5 + attr_reader :filename
  6 +
  7 + def setup
  8 + @filename = '/tmp/test_missing_translations.log'
  9 + FileUtils.mkdir_p(File.dirname(filename))
  10 + end
  11 +
  12 + def teardown
  13 + File.rm(filename) rescue nil
  14 + I18n.missing_translations.clear
  15 + end
  16 +
  17 + test 'logs to a memory hash' do
  18 + log = I18n::MissingTranslations::Log.new
  19 + log.log([:missing_translations, :foo])
  20 + log.log([:missing_translations, :bar, :baz, :boz])
  21 + log.log([:missing_translations, :bar, :baz, :buz])
  22 +
  23 + expected = { 'missing_translations' => { 'foo' => 'Foo', 'bar' => { 'baz' => { 'boz' => 'Boz', 'buz' => 'Buz' } } } }
  24 + assert_equal expected, log
  25 + end
  26 +
  27 + test 'dumps memory log as a yaml hash' do
  28 + log = I18n::MissingTranslations::Log.new
  29 + log.log([:missing_translations, :foo, :bar])
  30 + log.dump(out = StringIO.new)
  31 +
  32 + expected = '--- missing_translations: foo: bar: Bar '
  33 + assert_equal expected, out.string.gsub("\n", ' ')
  34 + end
  35 +
  36 + test 'works as a rack middleware' do
  37 + File.open(filename, 'w+') { |f| f.write(YAML.dump('en' => { 'foo' => 'Foo' })) }
  38 + middleware = I18n::MissingTranslations.new(lambda { |*| I18n.t(:missing) }, filename)
  39 + middleware.call({})
  40 + assert_equal({ 'en' => { 'foo' => 'Foo', 'missing' => 'Missing' }}, YAML.load_file(filename))
  41 + end
  42 + end
  43 +end
  44 +
  45 +
7 test/test_helper.rb
... ... @@ -0,0 +1,7 @@
  1 +require 'rubygems'
  2 +require 'test/unit'
  3 +require 'bundler/setup'
  4 +require 'test_declarative'
  5 +
  6 +$: << File.expand_path('../../lib', __FILE__)
  7 +require 'i18n/missing_translations'

0 comments on commit bcdfede

Please sign in to comment.
Something went wrong with that request. Please try again.