module I18nJSON

Constants

MissingConfigError
VERSION

Attributes

started[RW]

Public Class Methods

call(config_file: nil, config: nil) click to toggle source
# File lib/i18n-json.rb, line 21
def self.call(config_file: nil, config: nil)
  if !config_file && !config
    raise MissingConfigError, "you must set either `config_file` or `config`"
  end

  config = Glob::SymbolizeKeys.call(config || YAML.load_file(config_file))
  Schema.validate!(config)

  config[:translations].each do |group|
    export_group(group)
  end
end
compute_changes(paths, changed, added, removed) click to toggle source
# File lib/i18n-json/listen.rb, line 65
def self.compute_changes(paths, changed, added, removed)
  paths = paths.map {|path| relative_path(path) }

  {
    changed: included_on_watched_paths(paths, changed),
    added: included_on_watched_paths(paths, added),
    removed: included_on_watched_paths(paths, removed)
  }.select {|_k, v| v.any? }
end
debug(message) click to toggle source
# File lib/i18n-json/listen.rb, line 38
def self.debug(message)
  logger.tagged("i18n-json") { logger.debug(message) }
end
export_group(group) click to toggle source
# File lib/i18n-json.rb, line 34
def self.export_group(group)
  filtered_translations = Glob.filter(translations, group[:patterns])
  output_file_path = File.expand_path(group[:file])

  if output_file_path.include?(":locale")
    filtered_translations.each_key do |locale|
      locale_file_path = output_file_path.gsub(/:locale/, locale.to_s)
      write_file(locale_file_path, locale => filtered_translations[locale])
    end
  else
    write_file(output_file_path, filtered_translations)
  end
end
included_on_watched_paths(paths, changes) click to toggle source
# File lib/i18n-json/listen.rb, line 75
def self.included_on_watched_paths(paths, changes)
  changes.map {|change| relative_path(change) }.select do |change|
    paths.any? {|path| change.start_with?(path) }
  end
end
listen( config_file: Rails.root.join("config/i18n.yml"), locales_dir: Rails.root.join("config/locales") ) click to toggle source
# File lib/i18n-json/listen.rb, line 12
def self.listen(
  config_file: Rails.root.join("config/i18n.yml"),
  locales_dir: Rails.root.join("config/locales")
)
  return unless Rails.env.development?
  return if started

  self.started = true

  relative_paths =
    [config_file, locales_dir].map {|path| relative_path(path) }

  debug("Watching #{relative_paths.inspect}")

  listener(config_file, locales_dir.to_s).start
  I18nJSON.call(config_file: config_file)
end
listener(config_file, locales_dir) click to toggle source
# File lib/i18n-json/listen.rb, line 46
def self.listener(config_file, locales_dir) # rubocop:disable Metrics/MethodLength
  paths = [File.dirname(config_file), locales_dir]

  Listen.to(*paths) do |changed, added, removed|
    changes = compute_changes(
      [config_file, locales_dir],
      changed,
      added,
      removed
    )

    next unless changes.any?

    debug(changes.map {|key, value| "#{key}=#{value.inspect}" }.join(", "))

    I18nJSON.call(config_file: config_file)
  end
end
logger() click to toggle source
# File lib/i18n-json/listen.rb, line 42
def self.logger
  @logger ||= ActiveSupport::TaggedLogging.new(Rails.logger)
end
relative_path(path) click to toggle source
# File lib/i18n-json/listen.rb, line 30
def self.relative_path(path)
  Pathname.new(path).relative_path_from(Rails.root).to_s
end
relative_path_list(paths) click to toggle source
# File lib/i18n-json/listen.rb, line 34
def self.relative_path_list(paths)
  paths.map {|path| relative_path(path) }
end
translations() click to toggle source
# File lib/i18n-json.rb, line 56
def self.translations
  I18n.backend.instance_eval do
    has_been_initialized_before =
      respond_to?(:initialized?, true) && initialized?
    init_translations unless has_been_initialized_before
    translations
  end
end
write_file(file_path, translations) click to toggle source
# File lib/i18n-json.rb, line 48
def self.write_file(file_path, translations)
  FileUtils.mkdir_p(File.dirname(file_path))

  File.open(file_path, "w") do |file|
    file << ::JSON.pretty_generate(translations)
  end
end