-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
181 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
require 'fileutils' | ||
|
||
class Translate::File | ||
attr_accessor :path | ||
module Translate | ||
class File | ||
attr_accessor :path | ||
|
||
def initialize(path) | ||
self.path = path | ||
end | ||
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)) | ||
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 | ||
end | ||
|
||
def read | ||
File.exists?(path) ? YAML::load(IO.read(path)) : {} | ||
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 | ||
# 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 | ||
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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,125 @@ | ||
require 'pathname' | ||
|
||
class Translate::Keys | ||
# Allows keys extracted from lookups in files to be cached | ||
def self.files | ||
@@files ||= Translate::Keys.new.files | ||
end | ||
module Translate | ||
class KeysKeys | ||
# 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 | ||
# 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 files | ||
@files ||= extract_files | ||
end | ||
alias_method :to_hash, :files | ||
|
||
def keys | ||
files.keys | ||
end | ||
alias_method :to_a, :keys | ||
def keys | ||
files.keys | ||
end | ||
alias_method :to_a, :keys | ||
|
||
def i18n_keys(locale) | ||
I18n.backend.send(:init_translations) unless I18n.backend.initialized? | ||
extract_i18n_keys(I18n.backend.send(:translations)[locale.to_sym]).sort | ||
end | ||
def i18n_keys(locale) | ||
I18n.backend.send(:init_translations) unless I18n.backend.initialized? | ||
extract_i18n_keys(I18n.backend.send(:translations)[locale.to_sym]).sort | ||
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 | ||
# 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 | ||
else | ||
shallow_hash[key.to_s] = value | ||
shallow_hash | ||
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 | ||
# 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 | ||
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 | ||
# 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_i18n_keys(hash, parent_keys = []) | ||
hash.inject([]) do |keys, (key, value)| | ||
full_key = parent_keys + [key] | ||
if value.is_a?(Hash) | ||
# Nested hash | ||
keys += extract_i18n_keys(value, full_key) | ||
elsif value.present? | ||
# String leaf node | ||
keys << full_key.join(".") | ||
private | ||
def extract_i18n_keys(hash, parent_keys = []) | ||
hash.inject([]) do |keys, (key, value)| | ||
full_key = parent_keys + [key] | ||
if value.is_a?(Hash) | ||
# Nested hash | ||
keys += extract_i18n_keys(value, full_key) | ||
elsif value.present? | ||
# String leaf node | ||
keys << full_key.join(".") | ||
end | ||
keys | ||
end | ||
keys | ||
end | ||
end | ||
|
||
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 unless files[key].include?(path) | ||
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 unless files[key].include?(path) | ||
end | ||
files | ||
end | ||
files | ||
end | ||
end | ||
|
||
def i18n_lookup_pattern | ||
/\b(?:I18n\.t|I18n\.translate|t)(?:\s|\():?'([a-z0-9_]+.[a-z0-9_.]+)'\)?/ | ||
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(files_root_dir, "{app,config,lib}", "**","*.{rb,erb,rhtml}")) + | ||
Dir.glob(File.join(files_root_dir, "public", "javascripts", "**","*.js")) | ||
end | ||
def files_to_scan | ||
Dir.glob(File.join(files_root_dir, "{app,config,lib}", "**","*.{rb,erb,rhtml}")) + | ||
Dir.glob(File.join(files_root_dir, "public", "javascripts", "**","*.js")) | ||
end | ||
|
||
def files_root_dir | ||
Rails.root | ||
def files_root_dir | ||
Rails.root | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,42 @@ | ||
require 'fileutils' | ||
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 | ||
module Translate | ||
class Log | ||
attr_accessor :from_locale, :to_locale, :keys | ||
|
||
def write_to_file | ||
current_texts = File.exists?(file_path) ? file.read : {} | ||
current_texts.merge!(from_texts) | ||
file.write(current_texts) | ||
end | ||
def initialize(from_locale, to_locale, keys) | ||
self.from_locale = from_locale | ||
self.to_locale = to_locale | ||
self.keys = keys | ||
end | ||
|
||
def read | ||
file.read | ||
end | ||
def write_to_file | ||
current_texts = File.exists?(file_path) ? file.read : {} | ||
current_texts.merge!(from_texts) | ||
file.write(current_texts) | ||
end | ||
|
||
private | ||
def file | ||
@file ||= Translate::File.new(file_path) | ||
end | ||
def read | ||
file.read | ||
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 | ||
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 | ||
#make sure diff log dir exists | ||
translate_log_dir = File.join(RAILS_ROOT, 'log', 'translate') | ||
FileUtils.mkdir_p(translate_log_dir) | ||
File.join(translate_log_dir, "from_#{from_locale}_to_#{to_locale}.yml") | ||
def file_path | ||
#make sure diff log dir exists | ||
translate_log_dir = File.join(RAILS_ROOT, 'log', 'translate') | ||
FileUtils.mkdir_p(translate_log_dir) | ||
File.join(translate_log_dir, "from_#{from_locale}_to_#{to_locale}.yml") | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.