Translations
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
require File.expand_path('../boot', __FILE__)
|
||||
|
||||
require 'rails/all'
|
||||
# require 'carmen'
|
||||
# require 'pry'
|
||||
#require 'dotenv'; Dotenv.load ".env.local", ".env.#{Rails.env}"
|
||||
|
||||
# Require the gems listed in Gemfile, including any gems
|
||||
# you've limited to :test, :development, or :production.
|
||||
|
||||
+79
-11
@@ -41,6 +41,9 @@ module I18n
|
||||
def self.note(key, behavior = nil, behavior_size = nil)
|
||||
I18n.backend.needs_translation(key)
|
||||
if behavior
|
||||
if behavior.to_s == 'strict'
|
||||
return nil
|
||||
end
|
||||
return self.lorem_ipsum(behavior, behavior_size)
|
||||
end
|
||||
key.to_s.gsub(/^world\..*\.(.+)\.name$/, '\1').gsub(/^.*\.(.+)?$/, '\1').gsub('_', ' ')
|
||||
@@ -61,6 +64,7 @@ module I18n
|
||||
|
||||
@@translations_file = 'config/locales/.translations.yml'
|
||||
@@translation_cache_file = 'config/locales/.translation-cache.yml'
|
||||
@@pluralization_rules_file = 'config/locales/.pluralization-rules.yml'
|
||||
@@translation_cache
|
||||
|
||||
def needs_translation(key)
|
||||
@@ -68,6 +72,13 @@ module I18n
|
||||
@@needs_translation << key
|
||||
end
|
||||
|
||||
def translate_control(translation)
|
||||
@@translationsOnThisPage = true
|
||||
datakeys = ''
|
||||
translation['vars'].each { |key, value| datakeys += ' data-var-' + key.to_s + '="' + value.to_s.gsub('"', '"') + '"' }
|
||||
('<span class="translate-me ' + (translation['is_translated'] ? '' : 'un') + 'translated lang-' + (translation['lang']) + ' key--' + translation['key'].gsub('.', '--') + '" data-translate-key="' + translation['key'] + '" data-translate-untranslated="' + translation['untranslated'].gsub('"', '"') + (translation['translated'] ? '" data-translate-translated="' + translation['translated'] : '') + '" data-vars="' + (translation['vars'].length ? translation['vars'].to_json.gsub('"', '"') : '') + '" title="' + ('translate.alt_click') + '">' + (translation['html'] || translation['untranslated']) + '</span>').to_s.html_safe
|
||||
end
|
||||
|
||||
def initialized?
|
||||
begin
|
||||
super
|
||||
@@ -85,47 +96,104 @@ module I18n
|
||||
super
|
||||
end
|
||||
|
||||
def get_translation_info()
|
||||
begin
|
||||
YAML.load_file(@@translations_file) || {}
|
||||
rescue Exception => e
|
||||
# sometimes concurrency issues cause an exception during testing
|
||||
puts e
|
||||
sleep(1/2.0)
|
||||
get_translation_info()
|
||||
end
|
||||
end
|
||||
|
||||
def get_pluralization_rules(locale)
|
||||
rules = YAML.load_file(@@pluralization_rules_file)
|
||||
rules[locale.to_sym]
|
||||
end
|
||||
|
||||
def get_language_codes()
|
||||
YAML.load_file(@@pluralization_rules_file).keys
|
||||
end
|
||||
|
||||
def get_language_completion(lang)
|
||||
total = 0
|
||||
complete = 0
|
||||
get_translation_info().each { |k,v|
|
||||
total += 1
|
||||
complete += v['languages'].include?(lang.to_s) ? 1 : 0
|
||||
}
|
||||
total ? complete / total : 0
|
||||
end
|
||||
|
||||
def request_translation(key, vars, options)
|
||||
locale = options[:locale].to_s
|
||||
options[:locale] = :en
|
||||
translation = I18n.translate(key, vars, options)
|
||||
result = JSON.load(open("http://translate.google.com/translate_a/t?client=t&text=#{URI::escape(translation)}&hl=en&sl=en&tl=#{locale}&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1").read().gsub(/,+/, ',').gsub(/\[,+/, '[').gsub(/,+\]/, ']'))
|
||||
while result.is_a?(Array)
|
||||
result = result[0]
|
||||
end
|
||||
return result
|
||||
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 Rails.env.test? && options[:behavior].to_s != 'scrict'
|
||||
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 = get_translation_info()
|
||||
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]['languages'] ||= Array.new
|
||||
translations[key.to_s]['pages'] ||= Array.new
|
||||
if options['behavior']
|
||||
translations[key.to_s]['behavior'] ||= options['behavior']
|
||||
end
|
||||
vars = []
|
||||
options.each { |o,v|
|
||||
if !I18n::RESERVED_KEYS.include?(o.to_sym) && o.to_s != 'behavior' && o.to_s != 'behavior_size'
|
||||
vars << o.to_sym
|
||||
end
|
||||
}
|
||||
if vars.size() > 0
|
||||
translations[key.to_s]['vars'] = vars
|
||||
end
|
||||
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)
|
||||
unless translations[key.to_s]['languages'].include?(t.locale.to_s)
|
||||
translations[key.to_s]['languages'] << t.locale.to_s
|
||||
end
|
||||
}
|
||||
end
|
||||
path = $page_info[:path]
|
||||
unless translations[key.to_s]['pages'].include?(path)
|
||||
translations[key.to_s]['pages'] << path
|
||||
route = nil
|
||||
Rails.application.routes.routes.each { |r|
|
||||
if !route && r.path.match(path)
|
||||
route = r.path.spec.to_s.gsub(/\s*\(\.:\w+\)\s*$/, '')
|
||||
end
|
||||
}
|
||||
unless translations[key.to_s]['pages'].include?(route)
|
||||
translations[key.to_s]['pages'] << route
|
||||
end
|
||||
File.open(@@translations_file, 'w') { |f| f.write translations.to_yaml }
|
||||
end
|
||||
end
|
||||
|
||||
if Rails.env.test?
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
+6
-17
@@ -8,18 +8,8 @@ BikeBike::Application.routes.draw do
|
||||
|
||||
resources :workshop_facilitators
|
||||
|
||||
#resources :conference_registration_responses
|
||||
|
||||
#resources :conference_registrations
|
||||
|
||||
#resources :conference_registraton_form_fields
|
||||
|
||||
resources :registration_form_fields
|
||||
|
||||
#resources :conference_admins
|
||||
|
||||
#resources :conference_host_organizations
|
||||
|
||||
resources :conferences, :param => 'slug' do
|
||||
get :hosts
|
||||
get :registration
|
||||
@@ -37,8 +27,6 @@ BikeBike::Application.routes.draw do
|
||||
post 'registration/form/reorder' => 'conferences#reorder', as: 'registration_reorder'
|
||||
end
|
||||
|
||||
#resources :user_organization_relationships
|
||||
|
||||
resources :organizations, :param => 'slug' do
|
||||
get :members
|
||||
get :identity
|
||||
@@ -48,8 +36,6 @@ BikeBike::Application.routes.draw do
|
||||
resources :users
|
||||
resources :user_sessions
|
||||
|
||||
#resources :organization_statuses
|
||||
|
||||
resources :conference_types
|
||||
|
||||
resources :workshop_streams
|
||||
@@ -60,14 +46,17 @@ BikeBike::Application.routes.draw do
|
||||
|
||||
post '/translate/' => 'pages#translate'
|
||||
post '/location/territories/' => 'pages#location_territories'
|
||||
|
||||
get '/translations/:lang', to: 'pages#translations'
|
||||
get '/translations', to: 'pages#translation_list'
|
||||
|
||||
get 'login' => 'user_sessions#new', :as => :login
|
||||
post 'logout' => 'user_sessions#destroy', :as => :logout
|
||||
get 'register' => 'users#new', :as => 'register'
|
||||
|
||||
post "oauth/callback" => "oauths#callback"
|
||||
get "oauth/callback" => "oauths#callback"
|
||||
get "oauth/:provider" => "oauths#oauth", :as => :auth_at_provider
|
||||
post "oauth/callback" => "oauths#callback"
|
||||
get "oauth/callback" => "oauths#callback"
|
||||
get "oauth/:provider" => "oauths#oauth", :as => :auth_at_provider
|
||||
|
||||
root 'pages#home'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user