class Rodauth::Rails::TranslationsGenerator

def available_locales

def available_locales
  rodauths.inject([]) do |locales, rodauth|
    locales |= rodauth.allocate.send(:i18n_available_locales) || []
  end
end

def copy_locales

def copy_locales
  say "No locales specified!", :yellow if locales.empty?
  locales.each do |locale|
    translations = retrieve_translations(locale)
    if translations.empty?
      say "No translations for locale: #{locale}", :yellow
      next
    end
    # retain translations the user potentially changed
    translations.merge!(existing_translations(locale))
    # keep only translations for currently enabled features
    translations.slice!(*rodauth_methods)
    create_file "config/locales/rodauth.#{locale}.yml", locale_content(locale, translations)
  end
end

def existing_translations(locale)

def existing_translations(locale)
  destination = File.join(destination_root, "config/locales/rodauth.#{locale}.yml")
  # try to load existing translations first
  if File.exist?(destination)
    YAML.load_file(destination)[locale]["rodauth"]
  else
    {}
  end
end

def locale_content(locale, translations)

def locale_content(locale, translations)
  data = { locale => { "rodauth" => translations } }
  yaml = YAML.dump(data, line_width: 10_000) # disable line wrapping
  yaml.split("\n", 2).last # remove "---" header
end

def locales

def locales
  selected_locales || available_locales.map(&:to_s)
end

def retrieve_translations(locale)

def retrieve_translations(locale)
  files = translation_files(locale)
  files.inject({}) do |translations, file|
    translations.merge YAML.load_file(file)[locale]["rodauth"]
  end
end

def rodauth_methods

def rodauth_methods
  rodauths
    .flat_map { |rodauth| rodauth.instance_methods - Object.instance_methods }
    .map(&:to_s)
    .sort
end

def rodauths

def rodauths
  Rodauth::Rails.app.opts[:rodauths].values
end

def translation_files(locale)

def translation_files(locale)
  # ensure Rodauth configuration ran in autoloaded environment
  Rodauth::Rails.app
  Rodauth::I18n.directories
    .map { |directory| Dir["#{directory}/#{locale}.yml"] }
    .inject(:+)
end