class TranslationIO::Config

Attributes

api_key[RW]
bound_text_domains[RW]
charset[RW]
disable_gettext[RW]
disable_yaml[RW]
endpoint[RW]
erb_source_formats[RW]
gettext_object_delegate[RW]
haml_source_formats[RW]
ignored_key_prefixes[RW]
ignored_source_files[RW]
ignored_source_paths[RW]
locales_path[RW]
localization_key_prefixes[RW]
metadata_path[RW]
parsed_gems[RW]
pot_msgid_bugs_address[RW]
pot_package_name[RW]
pot_package_version[RW]
slim_source_formats[RW]
source_formats[RW]
source_locale[RW]
target_locales[RW]
test[RW]
text_domain[RW]
verbose[RW]
yaml_line_width[RW]
yaml_locales_path[RW]
yaml_remove_empty_keys[RW]

Public Class Methods

new() click to toggle source
# File lib/translation_io/config.rb, line 45
def initialize

  #######
  # Global options
  #######

  self.api_key        = ''
  self.source_locale  = :en
  self.target_locales = []
  self.endpoint       = 'https://translation.io/api'
  self.metadata_path  = File.join('config', 'locales', '.translation_io')
  self.verbose        = 1
  self.test           = false

  #######
  # YAML options
  #######

  self.disable_yaml = false

  # YAML directory
  self.yaml_locales_path = File.join('config', 'locales')

  # Ignored YAML key prefixes (like 'will_paginate.')
  self.ignored_key_prefixes = []

  # Cf. https://github.com/translation/rails#custom-localization-key-prefixes
  self.localization_key_prefixes = []

  # Define max line width of generated YAML files:
  # Can be nil (default behaviour of Ruby version), -1 (no wrapping) or positive integer (wrapping)
  # Cf. https://github.com/translation/rails/issues/19
  self.yaml_line_width = nil

  # Remove empty keys from translated YAML files
  # Cf. https://github.com/translation/rails/pull/37
  self.yaml_remove_empty_keys = false

  #######
  # GetText options
  #######

  self.disable_gettext = false

  # Cf. https://github.com/translation/rails#gettext-object-class-monkey-patching
  self.gettext_object_delegate = true

  # GetText directory for PO and MO files
  self.locales_path = File.join('config', 'locales', 'gettext')

  # These paths and files will not be parsed for GetText entries
  self.ignored_source_paths = ['vendor/', 'tmp/', 'node_modules/', 'logs/', '.git/', 'public/', 'private/']
  self.ignored_source_files = []

  # These gems will be parsed by GetText (use gem names)
  self.parsed_gems = []

  # Extensions for rb/erb/haml/slim file parsers
  self.source_formats      = ['rb', 'ruby', 'rabl']
  self.erb_source_formats  = ['erb', 'inky']
  self.haml_source_formats = ['haml', 'mjmlhaml']
  self.slim_source_formats = ['slim', 'mjmlslim']

  # 'text_domain' will be synced (name of .po/.mo files)
  # 'bound_text_domains' will be read during execution (in that priority order)
  self.text_domain        = 'app'
  self.bound_text_domains = ['app']
  self.charset            = 'UTF-8'

  # POT header informations
  self.pot_msgid_bugs_address = 'contact@translation.io'
  self.pot_package_name       = File.basename(Dir.pwd)
  self.pot_package_version    = '1.0'
  self.pot_copyright_holder   = File.basename(Dir.pwd)
  self.pot_copyright_year     = Date.today.year
end

Public Instance Methods

erb_source_files() click to toggle source
# File lib/translation_io/config.rb, line 136
def erb_source_files
  source_files_for_formats(erb_source_formats)
end
haml_source_files() click to toggle source
# File lib/translation_io/config.rb, line 140
def haml_source_files
  source_files_for_formats(haml_source_formats)
end
is_ignored_file?(path) click to toggle source
# File lib/translation_io/config.rb, line 182
def is_ignored_file?(path)
  ignored_source_files.any? do |ignored_source_file|
    path == Pathname.new(ignored_source_file).cleanpath
  end
end
is_ignored_path?(path) click to toggle source
# File lib/translation_io/config.rb, line 176
def is_ignored_path?(path)
  ignored_source_paths.any? do |ignored_source_path|
    path == Pathname.new(ignored_source_path).cleanpath
  end
end
pot_path() click to toggle source
# File lib/translation_io/config.rb, line 122
def pot_path
  File.join(locales_path, "#{text_domain}.pot")
end
slim_source_files() click to toggle source
# File lib/translation_io/config.rb, line 144
def slim_source_files
  source_files_for_formats(slim_source_formats)
end
source_files() click to toggle source
# File lib/translation_io/config.rb, line 132
def source_files
  source_files_for_formats(source_formats)
end
source_files_for_formats(formats) click to toggle source
# File lib/translation_io/config.rb, line 148
def source_files_for_formats(formats)
  file_paths = []
  root_paths = ['.']

  # Add gem paths that need to be parsed by GetText ("parsed_gem" option)
  parsed_gems.each do |gem_name|
    if Gem.loaded_specs[gem_name]
      root_paths << Gem.loaded_specs[gem_name].full_gem_path
    end
  end

  root_paths.each do |root_path|
    Pathname.new(root_path).find do |path|
      if path.directory?
        if is_ignored_path?(path)
          Find.prune
        end
      else
        if formats.include?(path.extname[1..-1]) && !is_ignored_file?(path)
          file_paths << path.to_s
        end
      end
    end
  end

  file_paths
end
to_s() click to toggle source
# File lib/translation_io/config.rb, line 188
def to_s
  "API Key: #{api_key} | Languages: #{source_locale} => [#{target_locales.join(', ')}]"
end
yaml_file_paths() click to toggle source
# File lib/translation_io/config.rb, line 126
def yaml_file_paths
  I18n.load_path.collect(&:to_s).uniq.select do |p|
    File.exist?(p) && (File.extname(p) == '.yml' || File.extname(p) == '.yaml')
  end
end