Class: I18nJS::EmbedFallbackTranslationsPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/i18n-js/embed_fallback_translations_plugin.rb

Defined Under Namespace

Modules: Utils

Instance Attribute Summary

Attributes inherited from Plugin

#main_config, #schema

Instance Method Summary collapse

Methods inherited from Plugin

#after_export, #config, #config_key, #enabled?, #initialize

Constructor Details

This class inherits a constructor from I18nJS::Plugin

Instance Method Details

#setupObject



38
39
40
# File 'lib/i18n-js/embed_fallback_translations_plugin.rb', line 38

def setup
  I18nJS::Schema.root_keys << config_key
end

#transform(translations:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/i18n-js/embed_fallback_translations_plugin.rb', line 49

def transform(translations:)
  return translations unless enabled?

  fallback_locale = I18n.default_locale.to_sym
  locales_to_fallback = translations.keys - [fallback_locale]

  translations_with_fallback = {}
  translations_with_fallback[fallback_locale] =
    translations[fallback_locale]

  locales_to_fallback.each do |locale|
    translations_with_fallback[locale] = Utils.deep_merge(
      translations[fallback_locale], translations[locale]
    )
  end

  translations_with_fallback
end

#validate_schemaObject



42
43
44
45
46
47
# File 'lib/i18n-js/embed_fallback_translations_plugin.rb', line 42

def validate_schema
  valid_keys = %i[enabled]

  schema.expect_required_keys(keys: valid_keys, path: [config_key])
  schema.reject_extraneous_keys(keys: valid_keys, path: [config_key])
end