Skip to content

Problem with translations for components, defined in a gem (engine) and used in Rails application #2198

@gsmetal

Description

@gsmetal

I am building UI kit (as Rails engine) with a bunch of implemented basic components. And then I use this gem (pushed to private rubygems) in the application. In application, there are app-specific components, so I configured view_component_path to Rails.root.join('app/view_components').

In UI-kit components, I use sidecar translations for some built-in strings. And when I install gem from rubygems, it's component's i18n is loaded incorrectly. This code does not remove absolute path part, and it fills configuration attributes like this:

Component.identifier # => "/home/user/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/mygem-0.5.1/app/view_components/mygem/component.rb"
Component.virtual_path # => "/home/user/.asdf/installs/ruby/3.3.5/lib/ruby/gems/3.3.0/gems/mygem-0.5.1/app/view_components/mygem/component"
Component.i18n_scope # => "home.user..asdf.installs.ruby.3.3.5.lib.ruby.gems.3.3.0.gems.mygem-0.5.1.app.view_components.mygem.component"

Last one is the main problem, this warning renders instead of translation:

translation missing: ru.home.user.asdf.installs.ruby.3.3.5.lib.ruby.gems.3.3.0.gems.mygem-0.5.1.app.view_components.mygem.component.placeholder, locale: ru

It seems to me, that current architecture with one general view_component_path does not allow building UI-kits flawlessly (where components could be defined in gems AND in application). As a workaround now I use this:

      def inherited(child)
        super

        # HACK: removes the first part of the gem's path. It is needed when component tranlslations comes from gem and
        # not from Rails application
        child.virtual_path.gsub!(
          /(.*#{Regexp.quote(Mygem::Engine.root.join('app/view_components').to_s)})|(\.rb)/, ''
        )
      end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions