Class: I18nJS::Plugin

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_config:, main_config:) ⇒ Plugin

Returns a new instance of Plugin.



68
69
70
71
72
# File 'lib/i18n-js/plugin.rb', line 68

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

Instance Attribute Details

#configObject (readonly)

The plugin’s configuration.



48
49
50
# File 'lib/i18n-js/plugin.rb', line 48

def config
  @config
end

#main_configObject (readonly)

The main configuration, which holds translation setup for plugins that supports exporting.



52
53
54
# File 'lib/i18n-js/plugin.rb', line 52

def main_config
  @main_config
end

#schemaObject (readonly)

The schema validator for the plugin.



55
56
57
# File 'lib/i18n-js/plugin.rb', line 55

def schema
  @schema
end

Class Method Details

.keyObject

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



59
60
61
62
63
64
65
66
# File 'lib/i18n-js/plugin.rb', line 59

def self.key
  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
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.



110
111
# File 'lib/i18n-js/plugin.rb', line 110

def after_export(files:)
end

#enabled?Boolean

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/i18n-js/plugin.rb', line 76

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.



102
103
# File 'lib/i18n-js/plugin.rb', line 102

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`.



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

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`.



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

def validate_schema
end