Class: Transdifflation::Comparer

Inherits:
Object
  • Object
show all
Defined in:
lib/transdifflation.rb

Overview

Implements the core

Constant Summary collapse

NOT_TRANSLATED =

const string added to keys not translated

"**NOT_TRANSLATED** "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComparer

Returns a new instance of Comparer.



20
21
22
# File 'lib/transdifflation.rb', line 20

def initialize()
  @has_changes = false
end

Instance Attribute Details

#has_changesObject (readonly)

Instance variable to get if changes have been detected



18
19
20
# File 'lib/transdifflation.rb', line 18

def has_changes
  @has_changes
end

Instance Method Details

#get_transdifflation_from_file(tag_name, path_to_yaml_relative_from_rails_root, from_locale = :en, to_locale = :es) ⇒ Object

Get Diff from YAML translation locale file from filesystem and generate differences in a file on our host

Parameters:

  • tag_name (String)

    Tag name this file will be installed on host

  • path_to_yaml_relative_from_rails_root (String)

    Path to the file in system, relative from Rails.root

  • from_locale (Symbol) (defaults to: :en)

    Default locale in gem. Used to translate ‘from’

  • to_locale (Symbol) (defaults to: :es)

    Default locale in host. Used to translate ‘to’



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/transdifflation.rb', line 64

def get_transdifflation_from_file(tag_name, path_to_yaml_relative_from_rails_root, from_locale=:en, to_locale=:es )

  #default values in optional params
  from_locale ||= :en
  to_locale ||= :es   #sorry, this gem was developed in Spain :)

  yml_source_content = YAMLReader.read_YAML_from_pathfile(path_to_yaml_relative_from_rails_root)
  puts "Loaded YAML content from file '#{path_to_yaml_relative_from_rails_root}'"

  #build the file name in our host
  filename_in_SRC = File.basename( path_to_yaml_relative_from_rails_root )
  host_target_filename = filename_in_SRC.gsub(/-?#{from_locale}\./) do |match_s|
    match_s.sub("#{from_locale}", "#{to_locale}")
  end
  host_target_file = File.join( Rails.root, "config/locales/#{to_locale}", "#{tag_name}.#{host_target_filename}")

  if(!File.file? host_target_file)
    get_first_time_file(yml_source_content, host_target_file, from_locale, to_locale)
  else
    generate_diff_file(yml_source_content, host_target_file, from_locale, to_locale)
  end

  @has_changes
end

#get_transdifflation_from_gem(gem_name, path_to_yaml_in_gem, from_locale = :en, to_locale = :es) ⇒ Object

Get Diff from YAML translation locale file from a gem and generate differences in a file on our host

Parameters:

  • gem_name (String)

    Installed gem’s name

  • path_to_yaml_in_gem (String)

    Path of the file inside gem’s source code

  • from_locale (Symbol) (defaults to: :en)

    Default locale in gem. Used to translate ‘from’

  • to_locale (Symbol) (defaults to: :es)

    Default locale in host. Used to translate ‘to’



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/transdifflation.rb', line 30

def get_transdifflation_from_gem(gem_name, path_to_yaml_in_gem, from_locale=:en, to_locale=:es )

  #default values in optional params
  from_locale ||= :en
  to_locale ||= :es   #sorry, this gem was developed in Spain :)

  yml_gem_content = YAMLReader.read_YAML_from_gem(gem_name, path_to_yaml_in_gem)
  puts "Loaded YAML content from gem '#{gem_name}', file '#{path_to_yaml_in_gem}'"


  #build the file name in our host
  filename_in_gem_SRC = File.basename( path_to_yaml_in_gem )
  host_target_filename = filename_in_gem_SRC.gsub(/-?#{from_locale}\./) do |match_s|
    match_s.sub("#{from_locale}", "#{to_locale}")
  end
  host_target_file = File.join( Rails.root, "config/locales/#{to_locale}", "#{gem_name}.#{host_target_filename}")

  if(!File.file? host_target_file)
    get_first_time_file(yml_gem_content, host_target_file, from_locale, to_locale)
  else
    generate_diff_file(yml_gem_content, host_target_file, from_locale, to_locale)
  end

  @has_changes
end