Extracted localeapp-handlebars_i18n to gem

This commit is contained in:
Randy Morgan 2012-12-04 00:17:58 +09:00
parent 082fac9185
commit 5b0fd89a1f
9 changed files with 21 additions and 123 deletions

View File

@ -1,3 +1,3 @@
---
:polled_at: 1353602900
:updated_at: 1353602900
:polled_at: 1354547351
:updated_at: 1354547351

View File

@ -21,8 +21,10 @@ group :development, :test do
gem 'rake', '~> 0.9.2'
gem 'localeapp'
gem 'handlebars'
gem 'localeapp-handlebars_i18n', path: '~/localeapp-handlebars_i18n'
end
group :development do
gem 'debugger'
gem 'foreman'

View File

@ -15,6 +15,12 @@ GIT
rack
rake-pipeline (~> 0.6)
PATH
remote: ~/localeapp-handlebars_i18n
specs:
localeapp-handlebars_i18n (0.0.1)
localeapp
GEM
remote: http://rubygems.org/
specs:
@ -121,6 +127,7 @@ DEPENDENCIES
guard
handlebars
localeapp
localeapp-handlebars_i18n!
puma
rack-cache
rack-ssl (~> 1.3)

View File

@ -1,15 +1,16 @@
$: << 'lib'
namespace :localeapp do
desc "update all locale files from localeapp"
desc "syncs localeapp, yaml and handlebars"
task :update do
require 'localeapp'
system 'localeapp pull'
require 'localeapp-handlebars_i18n'
Localeapp::HandlebarsI18n.configure($stdout) do |config|
config.hbs_load_path = Dir[File.expand_path '../assets/scripts/app/templates/**/*.hbs', __FILE__]
config.yml_load_path = File.expand_path '../locales/', __FILE__
config.localeapp_api_key = ENV['LOCALEAPP_API_KEY']
end
desc "push changes to en.yml up to localeapp"
task :report do
require 'localeapp'
system 'localeapp push locales/en.yml'
system "localeapp push locales/#{Localeapp::HandlebarsI18n.default_locale}.yml"
Localeapp::HandlebarsI18n.send_missing_translations
system "localeapp pull"
end
end

View File

@ -1,73 +0,0 @@
require 'handlebars'
require 'localeapp'
require 'localeapp/i18n_shim'
require 'localeapp/exception_handler'
module Localeapp
class Reporter
class << self
def hbs_load_path
@@load_path ||= []
end
def hbs_load_path=(dir)
@@load_path = dir
end
end
def initialize(locale_file, *hbs_templates)
configure_localeapp locale_file
backend.load_translations locale_file
hbs_templates = Localeapp::Reporter.hbs_load_path if hbs_templates.empty?
hbs_templates.flatten.each { |file| extract_keys(file) }
end
def send_missing_translations
register_missing_translations
Localeapp::Sender.new.post_missing_translations
end
private
def configure_localeapp(locale_file)
Localeapp.configure do |config|
config.translation_data_directory = Dir.new(File.expand_path('../',locale_file))
end
end
def matcher
@matcher ||= Regexp.new("{{t (.*?)}}")
end
def backend
@backend ||= I18n::Backend::Simple.new
end
def hbs_locale_keys
@hbs_locale_keys ||= []
end
def extract_keys(template_file)
template = IO.read(template_file)
if matches = template.scan(matcher)
@hbs_locale_keys = hbs_locale_keys | matches.flatten
end
end
def register_missing_translations
hbs_locale_keys.map do |locale_key|
begin
backend.translate(:en, locale_key)
nil
rescue Exception => e
Localeapp.missing_translations.add(:en, locale_key, nil, options || {})
end
end
end
end
end

View File

@ -1,34 +0,0 @@
require 'spec_helper'
require 'localeapp-reporter'
require 'localeapp'
describe Localeapp::Reporter do
let(:hbs_load_path) { File.expand_path '../support/**.hbs', __FILE__ }
let(:locale_yaml) { File.expand_path '../support/en.yml', __FILE__ }
let(:reporter) {
Localeapp::Reporter.hbs_load_path = Dir[hbs_load_path]
Localeapp::Reporter.new(locale_yaml)
}
it "configures localeapp" do
reporter
Localeapp.configuration.translation_data_directory.path.should == Dir.new(File.expand_path('../', locale_yaml)).path
end
it "supports hbs_load_path at class level" do
assert_equal Dir[hbs_load_path], reporter.class.hbs_load_path
end
it "adds missing translations to the Localeapp.missing_translations" do
reporter.send(:register_missing_translations)
Localeapp.missing_translations.instance_variable_get('@translations')[:en].keys.should include('missing.key')
end
it "sends missing translations to localeapp" do
# have to stub RestClient here as FakeWeb doesn't support looking at the post body yet
RestClient::Request.should_receive(:execute).with(hash_including(
:payload => { :translations => Localeapp.missing_translations.to_send }.to_json)).and_return(double('response', :code => 200))
reporter.send_missing_translations
end
end

View File

@ -1,3 +0,0 @@
en:
existing:
key: An existing key in locale YAML

View File

@ -1 +0,0 @@
{{t missing.key}}

View File

@ -1 +0,0 @@
{{t existing.key}}{{t missing.key}}