Some updates to translation engine, added dotenv but have not yet set it up.

This commit is contained in:
Godwin
2014-04-29 21:02:02 -06:00
parent 3759910993
commit cc948937bc
35 changed files with 707 additions and 190 deletions
+135
View File
@@ -0,0 +1,135 @@
require 'i18n/backend/active_record'
require 'yaml'
class DevTranslation < Translation
self.table_name = 'translations'
establish_connection :development
end
module I18n
class MissingTranslationExceptionHandler < ExceptionHandler
def self.lorem_ipsum(method, size)
options = {:random => true}
case method.to_s
when 'c', 'char', 'character', 'characters'
if size
return Forgery::LoremIpsum.characters size, options
end
return Forgery::LoremIpsum.character, options
when 'w', 'word', 'words'
if size
return Forgery::LoremIpsum.words size, options
end
#return'LOREM'
return Forgery::LoremIpsum.word options
when 's', 'sentence', 'sentences'
if size
return Forgery::LoremIpsum.sentences size, options
end
return Forgery::LoremIpsum.sentence options
when 'p', 'paragraph', 'paragraphs'
if size
return Forgery::LoremIpsum.paragraphs size, options.merge({:sentences => 10})
end
return Forgery::LoremIpsum.sentences 10, options
when 't', 'title'
return Forgery::LoremIpsum.sentences 1, options
end
return nil
end
def self.note(key, behavior = nil, behavior_size = nil)
I18n.backend.needs_translation(key)
if behavior
return self.lorem_ipsum(behavior, behavior_size)
end
key.to_s.gsub(/^world\..*\.(.+)\.name$/, '\1').gsub(/^.*\.(.+)?$/, '\1').gsub('_', ' ')
end
def call(exception, locale, key, options)
if exception.is_a?(MissingTranslation)
I18n::MissingTranslationExceptionHandler.note(key, options[:behavior] || nil, options[:behavior_size] || nil)
else
super
end
end
end
module Backend
class BikeBike < I18n::Backend::ActiveRecord
@@needs_translation
@@translations_file = 'config/locales/.translations.yml'
@@translation_cache_file = 'config/locales/.translation-cache.yml'
@@translation_cache
def needs_translation(key)
@@needs_translation ||= Array.new
@@needs_translation << key
end
def initialized?
begin
super
rescue
return false
end
end
def initialize
if Rails.env.test?
File.open(@@translations_file, 'w+')
File.open(@@translation_cache_file, 'w+')
end
@@translation_cache = YAML.load(File.read(@@translation_cache_file)) || Hash.new
super
end
protected
def lookup(locale, key, scope = [], options = {})
result = nil
if @@translation_cache && @@translation_cache.has_key?(locale.to_s) && @@translation_cache[locale.to_s].has_key?(key.to_s)
result = @@translation_cache[locale.to_s][key.to_s]
end
if !result
result = super(locale, key, scope, options)
if Rails.env.test?
if result
@@translation_cache[locale.to_s] ||= Hash.new
@@translation_cache[locale.to_s][key.to_s] = result
File.open(@@translation_cache_file, 'w') { |f| f.write @@translation_cache.to_yaml }
end
translations = YAML.load_file(@@translations_file)
translations ||= Hash.new
translations[key.to_s] ||= Hash.new
translations[key.to_s]['langauges'] ||= Hash.new
if result != nil
translations[key.to_s]['langauges'][locale.to_s] = result
end
translations[key.to_s]['pages'] ||= Array.new
unless translations[key.to_s].has_key?('data')
translations[key.to_s]['data'] = Array.new
DevTranslation.where("key = '#{key.to_s}' OR key LIKE '#{key.to_s}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%'").each { |t|
translations[key.to_s]['data'] << t.becomes(Translation)
}
end
path = $page_info[:path]
unless translations[key.to_s]['pages'].include?(path)
translations[key.to_s]['pages'] << path
end
File.open(@@translations_file, 'w') { |f| f.write translations.to_yaml }
end
end
if Rails.env.test?
end
result
end
end
end
end
I18n.exception_handler = I18n::MissingTranslationExceptionHandler.new
+1 -1
View File
@@ -1,4 +1,4 @@
require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::ActiveRecord.new
I18n.backend = I18n::Backend::BikeBike.new
# I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
# I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Flatten)
+15
View File
@@ -0,0 +1,15 @@
module Rack
class MethodOverrideWithParams < Rack::MethodOverride
def call(env)
#puts "\n\nENV: " + env.to_json.to_s + "\n\n"
#puts "\n\nTT: "
#puts I18n::Backend::BikeBike.translations_file + "\n\n"
$request = Rack::Request.new(env)
#Rails.I18n.translations_file ||= 'config/locales/.translations.yml'
#if Rails.env.test?
# File.open(Rails.I18n.translations_file, 'w+')# { |f| f.write {}.to_yaml }
#end
super(env)
end
end
end
+3 -3
View File
@@ -110,9 +110,9 @@ Rails.application.config.sorcery.configure do |config|
# config.twitter.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=twitter"
# config.twitter.user_info_mapping = {:email => "screen_name"}
config.facebook.key = "726202304080642"
config.facebook.secret = "386a7b717d348af4120aeb1bb0ca3516"
config.facebook.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=facebook"
config.facebook.key = "257350517701074"
config.facebook.secret = "2f6ab1fd7eeff9aee73140991fc68314"
config.facebook.callback_url = "http://dev.bikebike.org/oauth/callback?provider=facebook"
config.facebook.user_info_mapping = {:email => "email", :username => "username", :avatar => "picture/data/url"}
config.facebook.scope = "email"
config.facebook.display = "popup"