Module: I18nMultitenant
- Defined in:
- lib/i18n_multitenant.rb,
lib/i18n_multitenant/version.rb
Overview
frozen_string_literal: true
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
'1.0.0'
Class Method Summary collapse
-
.configure(config, enforce_available_locales: false) ⇒ Object
Public: Configures an instance of I18n::Config to ensure fallbacks are setup.
-
.locale_for(locale: I18n.default_locale, tenant: nil) ⇒ Object
Internal: Get the internal locale for a tenant-specific locale.
-
.set(**options) ⇒ Object
Public: Sets the internal locale to consider the current tenant and base locale.
-
.with_locale(**options) ⇒ Object
Public: Executes block using the specified locale configuration, restoring it after the block is executed.
Class Method Details
.configure(config, enforce_available_locales: false) ⇒ Object
Public: Configures an instance of I18n::Config to ensure fallbacks are setup.
47 48 49 50 51 52 53 54 |
# File 'lib/i18n_multitenant.rb', line 47 def self.configure(config, enforce_available_locales: false) config.enforce_available_locales = enforce_available_locales config.backend.class.send(:include, I18n::Backend::Fallbacks) if defined?(Rails.root) config.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] end end |
.locale_for(locale: I18n.default_locale, tenant: nil) ⇒ Object
Internal: Get the internal locale for a tenant-specific locale.
Example:
I18nMultitenant.locale_for(locale: :en, tenant: 'Veridian Dynamics')
=> "en-VERIDIAN_DYNAMICS"
38 39 40 41 42 43 44 |
# File 'lib/i18n_multitenant.rb', line 38 def self.locale_for(locale: I18n.default_locale, tenant: nil) if tenant && !tenant.to_s.empty? "#{ locale }-#{ tenant.to_s.upcase.tr(' .-', '_') }" else locale end end |
.set(**options) ⇒ Object
Public: Sets the internal locale to consider the current tenant and base locale.
locale: The base locale to be set (optional: uses the default locale). tenant: Name of the current tenant (optional: does not scope to the tenant).
Returns the key that should be used for all translations specific to that locale and tenant configuration.
Example:
I18nMultitenant.set(locale: :en, tenant: 'Veridian Dynamics')
=> "en-VERIDIAN_DYNAMICS"
I18nMultitenant.set(locale: 'en-GB', tenant: :'strange.tenant-name')
=> "en-GB-STRANGE_TENANT_NAME"
I18nMultitenant.set(locale: :es)
=> :es
23 24 25 |
# File 'lib/i18n_multitenant.rb', line 23 def self.set(**) I18n.locale = locale_for(**) end |
.with_locale(**options) ⇒ Object
Public: Executes block using the specified locale configuration, restoring it after the block is executed.
29 30 31 |
# File 'lib/i18n_multitenant.rb', line 29 def self.with_locale(**) I18n.with_locale(locale_for(**)) { yield } end |