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
+14 -11
View File
@@ -1,7 +1,8 @@
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'pry'
# require 'carmen'
# require 'pry'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
@@ -11,18 +12,20 @@ ENV['JPEGOPTIM_BIN'] = 'jpegoptim'
ENV['OPTIPNG_BIN'] = 'optipng'
module BikeBike
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :en #:de
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :en #:de
config.i18n.enforce_available_locales = false
#config.middleware.swap 'Rack::MethodOverride', 'Rack::MethodOverrideWithParams'
#config.i18n.exception_handler = I18n::MissingTranslationExceptionHandler.new
end
end
+11
View File
@@ -2,3 +2,14 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require'rails/commands/server'
module Rails
class Server
alias:default_options_alias :default_options
def default_options
default_options_alias.merge!(:Port=>80)
end
end
end
+2
View File
@@ -1,5 +1,7 @@
#require 'perftools'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
BikeBike::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
+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"
+78
View File
@@ -0,0 +1,78 @@
---
nola_2013.about:
langauges: {}
pages:
- "/"
home.its_awesome:
langauges: {}
pages:
- "/"
Conferences:
langauges: {}
pages:
- "/"
- "/login"
Organizations:
langauges: {}
pages:
- "/"
- "/login"
Resources:
langauges: {}
pages:
- "/"
- "/login"
Sign_In:
langauges: {}
pages:
- "/"
- "/login"
missing.translation:
langauges: {}
pages:
- "/"
- "/login"
form.Enter_your_email:
langauges: {}
pages:
- "/login"
email:
langauges: {}
pages:
- "/login"
password:
langauges: {}
pages:
- "/login"
sign_in:
langauges: {}
pages:
- "/login"
facebook_sign_in:
langauges: {}
pages:
- "/login"
user.Create_Account:
langauges: {}
pages:
- "/login"
user.why_register:
langauges: {}
pages:
- "/login"
form.Enter_your_username:
langauges: {}
pages:
- "/login"
username:
langauges: {}
pages:
- "/login"
password_confirmation:
langauges: {}
pages:
- "/login"
register:
langauges: {}
pages:
- "/login"