Class: I18nJS::Plugin
- Inherits:
-
Object
- Object
- I18nJS::Plugin
- Defined in:
- lib/i18n-js/plugin.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
The plugin’s configuration.
-
#main_config ⇒ Object
readonly
The main configuration, which holds translation setup for plugins that supports exporting.
-
#schema ⇒ Object
readonly
The schema validator for the plugin.
Class Method Summary collapse
-
.key ⇒ Object
Infer the config key name out of the class.
Instance Method Summary collapse
-
#after_export(files:) ⇒ Object
This method is called whenever ‘I18nJS.call(**kwargs)` finishes exporting JSON files based on your configuration.
-
#enabled? ⇒ Boolean
Check whether plugin is enabled or not.
-
#initialize(plugin_config:, main_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
#config ⇒ Object (readonly)
The plugin’s configuration.
48 49 50 |
# File 'lib/i18n-js/plugin.rb', line 48 def config @config end |
#main_config ⇒ Object (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 |
#schema ⇒ Object (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
.key ⇒ Object
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`.
76 77 78 |
# File 'lib/i18n-js/plugin.rb', line 76 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.
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_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`.
93 94 |
# File 'lib/i18n-js/plugin.rb', line 93 def validate_schema end |