Class: Idioma::PhraseImporter

Inherits:
Object
  • Object
show all
Defined in:
app/models/idioma/phrase_importer.rb

Class Method Summary collapse

Class Method Details

.ignore_key?(key) ⇒ Boolean

Checks to see if a given key should be ignored based on the ignore_keys array

Parameters:

  • The (String)

    key to check against

Returns:

  • (Boolean)

    Whether to ignore this key or not



42
43
44
45
46
47
# File 'app/models/idioma/phrase_importer.rb', line 42

def self.ignore_key?(key)
  Idioma.configuration.ignore_keys.each do |ignore_key|
    return true if key.to_s.include?(ignore_key)
  end
  false
end

.import(locale, locale_phrases) ⇒ Object

For a given locale, impot its phrases

Parameters:

  • the (Symbol)

    locale, eg. :en

  • a (Hash)

    hash of phrases which are already in dot-notation



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/idioma/phrase_importer.rb', line 18

def self.import(locale, locale_phrases)
  locale_phrases.each do |key, value|
    next if self.ignore_key?(key)

    phrase = Idioma::Phrase.find_by(locale: locale, i18n_key: key)
    if !phrase
      phrase = Idioma::Phrase.new({
        locale: locale,
        i18n_key: key,
        i18n_value: value,
        translated_at: Time.zone.now
      })
    elsif phrase.untranslated?
      phrase.i18n_value = value
      phrase.translated_at = Time.zone.now if value.present?
    end
    phrase.save_and_update_backend
  end
end

.import_from_extractionObject

Import all of the locales and phrases from the extraction The extraction comes from Idioma::PhraseExtractor.extract



6
7
8
9
10
11
12
# File 'app/models/idioma/phrase_importer.rb', line 6

def self.import_from_extraction
  phrases = Idioma::PhraseExtractor.extract
  phrases.each do |locale, locale_phrases|
    next unless Idioma.configuration.locales.include?(locale.to_sym)
    self.import(locale, locale_phrases)
  end
end