mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-21 01:30:12 +01:00
forgot to add these
This commit is contained in:
parent
86afd42148
commit
fde64e0b3d
145 changed files with 9044 additions and 0 deletions
35
backup.rails2.3/plugins/translate/lib/translate/file.rb
Normal file
35
backup.rails2.3/plugins/translate/lib/translate/file.rb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
require 'fileutils'
|
||||
|
||||
class Translate::File
|
||||
attr_accessor :path
|
||||
|
||||
def initialize(path)
|
||||
self.path = path
|
||||
end
|
||||
|
||||
def write(keys)
|
||||
FileUtils.mkdir_p File.dirname(path)
|
||||
File.open(path, "w") do |file|
|
||||
file.puts keys_to_yaml(Translate::File.deep_stringify_keys(keys))
|
||||
end
|
||||
end
|
||||
|
||||
def read
|
||||
File.exists?(path) ? YAML::load(IO.read(path)) : {}
|
||||
end
|
||||
|
||||
# Stringifying keys for prettier YAML
|
||||
def self.deep_stringify_keys(hash)
|
||||
hash.inject({}) { |result, (key, value)|
|
||||
value = deep_stringify_keys(value) if value.is_a? Hash
|
||||
result[(key.to_s rescue key) || key] = value
|
||||
result
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
def keys_to_yaml(keys)
|
||||
# Using ya2yaml, if available, for UTF8 support
|
||||
keys.respond_to?(:ya2yaml) ? keys.ya2yaml(:escape_as_utf8 => true) : keys.to_yaml
|
||||
end
|
||||
end
|
||||
152
backup.rails2.3/plugins/translate/lib/translate/keys.rb
Normal file
152
backup.rails2.3/plugins/translate/lib/translate/keys.rb
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
require 'pathname'
|
||||
|
||||
class Translate::Keys
|
||||
# Allows keys extracted from lookups in files to be cached
|
||||
def self.files
|
||||
@@files ||= Translate::Keys.new.files
|
||||
end
|
||||
|
||||
# Allows flushing of the files cache
|
||||
def self.files=(files)
|
||||
@@files = files
|
||||
end
|
||||
|
||||
def files
|
||||
@files ||= extract_files
|
||||
end
|
||||
alias_method :to_hash, :files
|
||||
|
||||
def keys
|
||||
files.keys
|
||||
end
|
||||
alias_method :to_a, :keys
|
||||
|
||||
def i18n_keys(locale)
|
||||
I18n.backend.send(:init_translations) unless I18n.backend.initialized?
|
||||
Translate::Keys.to_shallow_hash(I18n.backend.send(:translations)[locale.to_sym]).keys.sort
|
||||
end
|
||||
|
||||
def untranslated_keys
|
||||
Translate::Keys.translated_locales.inject({}) do |missing, locale|
|
||||
missing[locale] = i18n_keys(I18n.default_locale).map do |key|
|
||||
I18n.backend.send(:lookup, locale, key).nil? ? key : nil
|
||||
end.compact
|
||||
missing
|
||||
end
|
||||
end
|
||||
|
||||
def missing_keys
|
||||
locale = I18n.default_locale; yaml_keys = {}
|
||||
yaml_keys = Translate::Storage.file_paths(locale).inject({}) do |keys, path|
|
||||
keys = keys.deep_merge(Translate::File.new(path).read[locale.to_s])
|
||||
end
|
||||
files.reject { |key, file| self.class.contains_key?(yaml_keys, key) }
|
||||
end
|
||||
|
||||
def self.translated_locales
|
||||
I18n.available_locales.reject { |locale| [:root, I18n.default_locale.to_sym].include?(locale) }
|
||||
end
|
||||
|
||||
# Checks if a nested hash contains the keys in dot separated I18n key.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# hash = {
|
||||
# :foo => {
|
||||
# :bar => {
|
||||
# :baz => 1
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# contains_key?("foo", key) # => true
|
||||
# contains_key?("foo.bar", key) # => true
|
||||
# contains_key?("foo.bar.baz", key) # => true
|
||||
# contains_key?("foo.bar.baz.bla", key) # => false
|
||||
#
|
||||
def self.contains_key?(hash, key)
|
||||
keys = key.to_s.split(".")
|
||||
return false if keys.empty?
|
||||
!keys.inject(HashWithIndifferentAccess.new(hash)) do |memo, key|
|
||||
memo.is_a?(Hash) ? memo.try(:[], key) : nil
|
||||
end.nil?
|
||||
end
|
||||
|
||||
# Convert something like:
|
||||
#
|
||||
# {
|
||||
# :pressrelease => {
|
||||
# :label => {
|
||||
# :one => "Pressmeddelande"
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
#
|
||||
# to:
|
||||
#
|
||||
# {'pressrelease.label.one' => "Pressmeddelande"}
|
||||
#
|
||||
def self.to_shallow_hash(hash)
|
||||
hash.inject({}) do |shallow_hash, (key, value)|
|
||||
if value.is_a?(Hash)
|
||||
to_shallow_hash(value).each do |sub_key, sub_value|
|
||||
shallow_hash[[key, sub_key].join(".")] = sub_value
|
||||
end
|
||||
else
|
||||
shallow_hash[key.to_s] = value
|
||||
end
|
||||
shallow_hash
|
||||
end
|
||||
end
|
||||
|
||||
# Convert something like:
|
||||
#
|
||||
# {'pressrelease.label.one' => "Pressmeddelande"}
|
||||
#
|
||||
# to:
|
||||
#
|
||||
# {
|
||||
# :pressrelease => {
|
||||
# :label => {
|
||||
# :one => "Pressmeddelande"
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
def self.to_deep_hash(hash)
|
||||
hash.inject({}) do |deep_hash, (key, value)|
|
||||
keys = key.to_s.split('.').reverse
|
||||
leaf_key = keys.shift
|
||||
key_hash = keys.inject({leaf_key.to_sym => value}) { |hash, key| {key.to_sym => hash} }
|
||||
deep_merge!(deep_hash, key_hash)
|
||||
deep_hash
|
||||
end
|
||||
end
|
||||
|
||||
# deep_merge by Stefan Rusterholz, see http://www.ruby-forum.com/topic/142809
|
||||
def self.deep_merge!(hash1, hash2)
|
||||
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
||||
hash1.merge!(hash2, &merger)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract_files
|
||||
files_to_scan.inject(HashWithIndifferentAccess.new) do |files, file|
|
||||
IO.read(file).scan(i18n_lookup_pattern).flatten.map(&:to_sym).each do |key|
|
||||
files[key] ||= []
|
||||
path = Pathname.new(File.expand_path(file)).relative_path_from(Pathname.new(Rails.root)).to_s
|
||||
files[key] << path if !files[key].include?(path)
|
||||
end
|
||||
files
|
||||
end
|
||||
end
|
||||
|
||||
def i18n_lookup_pattern
|
||||
/\b(?:I18n\.t|I18n\.translate|t)(?:\s|\():?'([a-z0-9_]+.[a-z0-9_.]+)'\)?/
|
||||
end
|
||||
|
||||
def files_to_scan
|
||||
Dir.glob(File.join(Translate::Storage.root_dir, "{app,config,lib}", "**","*.{rb,erb,rhtml}")) +
|
||||
Dir.glob(File.join(Translate::Storage.root_dir, "public", "javascripts", "**","*.js"))
|
||||
end
|
||||
end
|
||||
35
backup.rails2.3/plugins/translate/lib/translate/log.rb
Normal file
35
backup.rails2.3/plugins/translate/lib/translate/log.rb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
class Translate::Log
|
||||
attr_accessor :from_locale, :to_locale, :keys
|
||||
|
||||
def initialize(from_locale, to_locale, keys)
|
||||
self.from_locale = from_locale
|
||||
self.to_locale = to_locale
|
||||
self.keys = keys
|
||||
end
|
||||
|
||||
def write_to_file
|
||||
current_texts = File.exists?(file_path) ? file.read : {}
|
||||
current_texts.merge!(from_texts)
|
||||
file.write(current_texts)
|
||||
end
|
||||
|
||||
def read
|
||||
file.read
|
||||
end
|
||||
|
||||
private
|
||||
def file
|
||||
@file ||= Translate::File.new(file_path)
|
||||
end
|
||||
|
||||
def from_texts
|
||||
Translate::File.deep_stringify_keys(Translate::Keys.to_deep_hash(keys.inject({}) do |hash, key|
|
||||
hash[key] = I18n.backend.send(:lookup, from_locale, key)
|
||||
hash
|
||||
end))
|
||||
end
|
||||
|
||||
def file_path
|
||||
File.join(Rails.root, "config", "locales", "log", "from_#{from_locale}_to_#{to_locale}.yml")
|
||||
end
|
||||
end
|
||||
11
backup.rails2.3/plugins/translate/lib/translate/routes.rb
Normal file
11
backup.rails2.3/plugins/translate/lib/translate/routes.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module Translate
|
||||
class Routes
|
||||
def self.translation_ui(map)
|
||||
map.with_options(:controller => 'translate') do |t|
|
||||
t.translate_list 'translate'
|
||||
t.translate 'translate/translate', :action => 'translate'
|
||||
t.translate_reload 'translate/reload', :action => 'reload'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
28
backup.rails2.3/plugins/translate/lib/translate/storage.rb
Normal file
28
backup.rails2.3/plugins/translate/lib/translate/storage.rb
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
class Translate::Storage
|
||||
attr_accessor :locale
|
||||
|
||||
def initialize(locale)
|
||||
self.locale = locale.to_sym
|
||||
end
|
||||
|
||||
def write_to_file
|
||||
Translate::File.new(file_path).write(keys)
|
||||
end
|
||||
|
||||
def self.file_paths(locale)
|
||||
Dir.glob(File.join(root_dir, "config", "locales", "**","#{locale}.yml"))
|
||||
end
|
||||
|
||||
def self.root_dir
|
||||
Rails.root
|
||||
end
|
||||
|
||||
private
|
||||
def keys
|
||||
{locale => I18n.backend.send(:translations)[locale]}
|
||||
end
|
||||
|
||||
def file_path
|
||||
File.join(Translate::Storage.root_dir, "config", "locales", "#{locale}.yml")
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue