Class: I18nJS::Plugin

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Plugin

Returns a new instance of Plugin.



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

def initialize(config:)
  @main_config = config
  @schema = I18nJS::Schema.new(@main_config)
end

Instance Attribute Details

#main_configObject (readonly)

The configuration that’s being used to export translations.



36
37
38
# File 'lib/i18n-js/plugin.rb', line 36

def main_config
  @main_config
end

#schemaObject (readonly)

The ‘I18nJS::Schema` instance that can be used to validate your plugin’s configuration.



40
41
42
# File 'lib/i18n-js/plugin.rb', line 40

def schema
  @schema
end

Instance Method Details

#after_export(files:) ⇒ Object

This method is called whenever ‘I18nJS.call(**kwargs)` finishes exporting JSON files based on your configuration.

You can use it to further process exported files, or generate new files based on the translations that have been exported.



100
101
# File 'lib/i18n-js/plugin.rb', line 100

def after_export(files:)
end

#configObject

Return the plugin configuration



60
61
62
# File 'lib/i18n-js/plugin.rb', line 60

def config
  main_config[config_key] || {}
end

#config_keyObject

Infer the config key name out of the class. If you plugin is called ‘MySamplePlugin`, the key will be `my_sample`.



49
50
51
52
53
54
55
56
57
# File 'lib/i18n-js/plugin.rb', line 49

def config_key
  self.class.name.split("::").last
      .gsub(/Plugin$/, "")
      .gsub(/^([A-Z]+)([A-Z])/) { "#{$1.downcase}#{$2}" }
      .gsub(/^([A-Z]+)/) { $1.downcase }
      .gsub(/([A-Z]+)/m) { "_#{$1.downcase}" }
      .downcase
      .to_sym
end

#enabled?Boolean

Check whether plugin is enabled or not. A plugin is enabled when the plugin configuration has ‘enabled: true`.

Returns:

  • (Boolean)


66
67
68
# File 'lib/i18n-js/plugin.rb', line 66

def enabled?
  config[:enabled]
end

#setupObject

This method must set up the basic plugin configuration, like adding the config’s root key in case your plugin accepts configuration (defined via the config file).

If you don’t add this key, the linter will prevent non-default keys from being added to the configuration file.



92
93
# File 'lib/i18n-js/plugin.rb', line 92

def setup
end

#transform(translations:) ⇒ Object

This method is responsible for transforming the translations. The translations you’ll receive may be already be filtered by other plugins and by the default filtering itself. If you need to access the original translations, use ‘I18nJS.translations`.



74
75
76
# File 'lib/i18n-js/plugin.rb', line 74

def transform(translations:)
  translations
end

#validate_schemaObject

In case your plugin accepts configuration, this is where you must validate the configuration, making sure only valid keys and type is provided. If the configuration contains invalid data, then you must raise an exception using something like ‘raise I18nJS::Schema::InvalidError, error_message`.



83
84
# File 'lib/i18n-js/plugin.rb', line 83

def validate_schema
end