Class: I15R

Inherits:
Object
  • Object
show all
Defined in:
lib/i15r.rb,
lib/i15r/version.rb,
lib/i15r/file_reader.rb,
lib/i15r/file_writer.rb,
lib/i15r/locale_creator.rb,
lib/i15r/console_printer.rb,
lib/i15r/pattern_matcher.rb

Defined Under Namespace

Classes: AppFolderNotFound, Config, ConsolePrinter, FileReader, FileWriter, LocaleCreator, PatternMatcher

Constant Summary collapse

VERSION =
"0.5.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer, printer, config = {}) ⇒ I15R

Returns a new instance of I15R.



35
36
37
38
39
40
41
# File 'lib/i15r.rb', line 35

def initialize(reader, writer, printer, config={})
  @reader = reader
  @writer = writer
  @printer = printer
  @locale_creator = I15R::LocaleCreator.new(config[:create_locale_file])
  @config = I15R::Config.new(config)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



33
34
35
# File 'lib/i15r.rb', line 33

def config
  @config
end

Instance Method Details

#file_path_to_message_prefix(file) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/i15r.rb', line 47

def file_path_to_message_prefix(file)
  segments = File.expand_path(file).split('/').reject { |segment| segment.empty? }
  subdir = %w(views helpers controllers models).find do |app_subdir|
     segments.index(app_subdir)
  end
  if subdir.nil?
    raise AppFolderNotFound, "No app. subfolders were found to determine prefix. Path is #{File.expand_path(file)}"
  end
  first_segment_index = segments.index(subdir) + 1
  file_name_without_extensions = segments.last.split('.').first
  if file_name_without_extensions and file_name_without_extensions[0] == '_'
    file_name_without_extensions = file_name_without_extensions[1..-1]
  end
  path_segments = segments.slice(first_segment_index...-1)
  if path_segments.empty?
    file_name_without_extensions
  else
    "#{path_segments.join('.')}.#{file_name_without_extensions}"
  end
end

#full_prefix(path) ⇒ Object



68
69
70
71
72
# File 'lib/i15r.rb', line 68

def full_prefix(path)
  prefix = [config.prefix]
  prefix << file_path_to_message_prefix(path) if include_path?
  prefix.compact.join('.')
end

#include_path?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/i15r.rb', line 103

def include_path?
  config.prefix_with_path || !config.prefix
end

#internationalize!(path) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/i15r.rb', line 95

def internationalize!(path)
  @printer.println "Running in dry-run mode" if config.dry_run?
  path = "app" if path.nil?
  files = File.directory?(path) ? Dir.glob("#{path}/**/*.{erb,haml}") : [path]
  files.each { |file| internationalize_file(file) }
  @locale_creator.save_file
end

#internationalize_file(path) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/i15r.rb', line 74

def internationalize_file(path)
  text = @reader.read(path)
  template_type = path[/(?:.*)\.(.*)$/, 1]
  @printer.println("#{path}:")
  @printer.println("")
  i18ned_text = sub_plain_strings(text, full_prefix(path), template_type.to_sym)
  @writer.write(path, i18ned_text) unless config.dry_run?
end

#sub_plain_strings(text, prefix, file_type) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/i15r.rb', line 83

def sub_plain_strings(text, prefix, file_type)
  pm = I15R::PatternMatcher.new(prefix,
                                file_type,
                                @locale_creator,
                                :add_default => config.add_default,
                                :override_i18n_method => config.override_i18n_method)
  transformed_text = pm.run(text) do |old_line, new_line|
    @printer.print_diff(old_line, new_line)
  end
  transformed_text + "\n"
end