Skip to content

Commit

Permalink
Add support for inheritable translations
Browse files Browse the repository at this point in the history
Waiting for ViewComponent/view_component#1934
to be released.
  • Loading branch information
elia committed Dec 29, 2023
1 parent c3addd4 commit 014150a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions admin/app/components/solidus_admin/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ def icon_tag(name, **attrs)
render component("ui/icon").new(name: name, **attrs)
end

module InheritableTranslations
def build_i18n_backend
return if compiled?

# We need to load the translations files from the ancestors so a component
# can inherit translations from its parent and is able to overwrite them.
translation_files = ancestors.reverse_each.with_object([]) do |ancestor, files|
if ancestor.is_a?(Class) && ancestor < ViewComponent::Base
files.concat(ancestor.sidecar_files(%w[yml yaml].freeze))
end
end

# In development it will become nil if the translations file is removed
self.i18n_backend = if translation_files.any?
I18nBackend.new(
i18n_scope: i18n_scope,
load_paths: translation_files
)
end
end
end

# Can be removed once https://github.com/ViewComponent/view_component/pull/1934 is released
extend InheritableTranslations unless Gem::Version.new(ViewComponent::VERSION) >= Gem::Version.new("3.9")

def missing_translation(key, options)
keys = I18n.normalize_keys(options[:locale] || I18n.locale, key, options[:scope])

Expand Down

0 comments on commit 014150a

Please sign in to comment.