<commit_before>require "minitest/autorun"
require 'rails'
require 'typus'
require 'yaml'

require "i18n/backend/flatten"
I18n::Backend::Simple.send(:include, I18n::Backend::Flatten)
I18n.enforce_available_locales = false

class LocalesCompletenessTest < Minitest::Test

  REFERENCE_LOCALE = "en"
  LOCALES_TO_TEST  = %w(de)

  def locale_file(locale)
    if (locale == REFERENCE_LOCALE)
      Admin::Engine.root.join("config/locales/typus.#{locale}.yml")
    else
      File.join(File.dirname(__FILE__), "../config/locales/typus.#{locale}.yml")
    end
  end

  def translations(locale)
    file = locale_file(locale)
    data = YAML.load_file(file)[locale]
    I18n.backend.flatten_translations(locale, data, false, false)
  end

  def locale_keys(locale)
    translations(locale).keys
  end

  def all_keys
    locale_keys(REFERENCE_LOCALE)
  end

  LOCALES_TO_TEST.each do |locale|

    define_method("test_#{locale}_is_complete") do
      difference = all_keys - locale_keys(locale)
      msg = %(The locale "#{locale}" is missing translations. Please add translations for the keys listed below)
      assert_equal [], difference, msg
    end

    define_method("test_#{locale}_has_no_obsolete_keys") do
      difference = locale_keys(locale) - all_keys
      msg = %(The locale "#{locale}" has obsolete translations. Please remove the keys listed below)
      assert_equal [], difference, msg
    end

  end

end
<commit_msg>Determine the locales to test dynamically …<commit_after>require "minitest/autorun"
require 'rails'
require 'typus'
require 'yaml'

require "i18n/backend/flatten"
I18n::Backend::Simple.send(:include, I18n::Backend::Flatten)
I18n.enforce_available_locales = false

class LocalesCompletenessTest < Minitest::Test

  REFERENCE_LOCALE = "en"

  def self.locales_to_test
    files = Dir.glob(File.join(File.dirname(__FILE__), '../config/locales/typus.*.yml'))
    files.map { |file| YAML.load_file(file).keys.first.to_s }
  end

  def locale_file(locale)
    if (locale == REFERENCE_LOCALE)
      Admin::Engine.root.join("config/locales/typus.#{locale}.yml")
    else
      File.join(File.dirname(__FILE__), "../config/locales/typus.#{locale}.yml")
    end
  end

  def translations(locale)
    file = locale_file(locale)
    data = YAML.load_file(file)[locale]
    I18n.backend.flatten_translations(locale, data, false, false)
  end

  def locale_keys(locale)
    translations(locale).keys
  end

  def all_keys
    locale_keys(REFERENCE_LOCALE)
  end

  locales_to_test.each do |locale|

    define_method("test_#{locale}_is_complete") do
      difference = all_keys - locale_keys(locale)
      msg = %(The locale "#{locale}" is missing translations. Please add translations for the keys listed below)
      assert_equal [], difference, msg
    end

    define_method("test_#{locale}_has_no_obsolete_keys") do
      difference = locale_keys(locale) - all_keys
      msg = %(The locale "#{locale}" has obsolete translations. Please remove the keys listed below)
      assert_equal [], difference, msg
    end

  end

end

{'fraction_non_alphanumeric': 0.11421069904824417, 'fraction_numerical': 0.006563833278634723, 'mean_word_length': 4.08, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1765313', 'n_tokens_mistral': 1043, 'n_tokens_neox': 1003, 'n_words': 217}