Module: I18nCheckTranslations

Defined in:
lib/i18n_check_translations.rb,
lib/i18n_check_translations_rspec.rb,
lib/i18n_check_translations/version.rb

Defined Under Namespace

Modules: RSpec

Constant Summary collapse

VERSION =
"1.1.1"

Class Method Summary collapse

Class Method Details

.check(basic_locale, check_on_locale, raise_error_if_missing = false) ⇒ Hash

It takes all the translation keys for the basic_locale and tries to translate them to check_on_locale. For those that are missing translations, it either write the error or raises and Exception.

Usage example:

1) result = I18nCheckTranslations.check :en, :nl
    will take all the *.en.yml files from config/locales and will build a
    a Hash with all the translation keys and their values on the :nl locale
    For example:

    {"en.customer.first_name" => {:translation => "Voornaam",
                                  :basic_local_translation => "First Name",
                                  :localization_file => "/config/locales/customers/nl.yml"}}
   If the translation is missing for :nl, then you are going to see on :translation something that
   starts with "translation missing"

2) result = I18nCheckTranslations.check :en, :nl, true

   Same as (1), but it will raise an error if a translation is missing

Parameters:

  • basic_locale (Symbol)

    e.g.: :en, :nl, :gr

  • check_on_locale (Symbol)

    e.g.: :en, :nl, :gr

  • raise_error_if_missing (true|false with default nil) (defaults to: false)

Returns:

  • (Hash)

    The result of processing the locales files for basic_locale and trying to translate all the keys to the check_on_locale. See example below.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/i18n_check_translations.rb', line 36

def self.check(basic_locale, check_on_locale, raise_error_if_missing = false)
  result = {}

  disable_fallbacks # if fallbacks are enabled, then missing translations will not be missing.

  previous_locale = I18n.locale # save the current locale in order to restore it at the end of the process
  I18n.locale = check_on_locale

  Dir.glob(File.join(Rails.root, 'config', 'locales', '**', "*#{basic_locale}.yml")).each do |localization_file|
    hash_input = YAML.load_file localization_file

    paths = hash_traverse hash_input # big stuff of the work is done here to build all the translation keys from a file.

    paths.each do |key, translation|
      key_to_translate = key.gsub /^#{basic_locale.to_s}\./, '' # - remove the "en." from "en.customer.first_name"
      new_key = "#{check_on_locale}.#{key_to_translate}"        # - and make it "nl.customer.first_name" to index the Hash result
      new_translation = I18n.translate key_to_translate         # - do the translation

      result[new_key] = {:translation => new_translation,
                         :basic_locale_translation => translation,
                         :localization_file => dest_localization_file(localization_file, basic_locale, check_on_locale)}

      raise StandardError.new("Missing translation! #{result[new_key].inspect}") if new_translation.start_with?('translation missing') && raise_error_if_missing
    end
  end

  I18n.locale = previous_locale # restore locale

  enable_fallbacks

  result
end

.check_and_dump(filename, basic_locale, check_on_locale, raise_error_if_missing = false) ⇒ Object

Same as I18nCheckTranslations.check but takes as input the filename that will be used to save the results into a csv file.



72
73
74
75
76
77
78
79
80
# File 'lib/i18n_check_translations.rb', line 72

def self.check_and_dump(filename, basic_locale, check_on_locale, raise_error_if_missing = false)
  result = check(basic_locale, check_on_locale, raise_error_if_missing)
  CSV.open filename, 'wb', :force_quotes => true do |csv|
    csv << ['KEY', 'BASIC TRANSLATION', 'TRANSLATION', 'LOCALIZATION FILE']
    result.each do |k, v|
      csv << [k, v[:basic_locale_translation], v[:translation], v[:localization_file]]
    end
  end
end