Class: I18nJS::Plugin
- Inherits:
-
Object
- Object
- I18nJS::Plugin
- Defined in:
- lib/i18n-js/plugin.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#main_config ⇒ Object
readonly
The configuration that’s being used to export translations.
-
#schema ⇒ Object
readonly
The ‘I18nJS::Schema` instance that can be used to validate your plugin’s configuration.
Instance Method Summary collapse
-
#after_export(files:) ⇒ Object
This method is called whenever ‘I18nJS.call(**kwargs)` finishes exporting JSON files based on your configuration.
-
#config ⇒ Object
Return the plugin configuration.
-
#config_key ⇒ Object
Infer the config key name out of the class.
-
#enabled? ⇒ Boolean
Check whether plugin is enabled or not.
-
#initialize(config:) ⇒ Plugin
constructor
A new instance of Plugin.
-
#setup ⇒ Object
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).
-
#transform(translations:) ⇒ Object
This method is responsible for transforming the translations.
-
#validate_schema ⇒ Object
In case your plugin accepts configuration, this is where you must validate the configuration, making sure only valid keys and type is provided.
Constructor Details
Instance Attribute Details
#main_config ⇒ Object (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 |
#schema ⇒ Object (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 |
#config ⇒ Object
Return the plugin configuration
60 61 62 |
# File 'lib/i18n-js/plugin.rb', line 60 def config main_config[config_key] || {} end |
#config_key ⇒ Object
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`.
66 67 68 |
# File 'lib/i18n-js/plugin.rb', line 66 def enabled? config[:enabled] end |
#setup ⇒ Object
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_schema ⇒ Object
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 |