Sha256: c68b825fd2d9952cd91d94952484115596236b63f3ac78c3e4d2aba9e9e08dc8

Contents?: true

Size: 1.42 KB

Versions: 34

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require "rails/generators/abstract_generator"

module Locale
  module Generators
    class ComponentGenerator < ::Rails::Generators::NamedBase
      include ViewComponent::AbstractGenerator

      source_root File.expand_path("templates", __dir__)
      argument :attributes, type: :array, default: [], banner: "attribute"
      class_option :sidecar, type: :boolean, default: false

      def create_locale_file
        if ViewComponent::Base.config.generate.distinct_locale_files
          I18n.available_locales.each do |locale|
            create_file destination(locale), translations_hash([locale]).to_yaml
          end
        else
          create_file destination, translations_hash(I18n.available_locales).to_yaml
        end
      end

      private

      def translations_hash(locales = [:en])
        locales.map { |locale| [locale.to_s, translation_keys] }.to_h
      end

      def translation_keys
        keys = attributes.map(&:name)
        keys = %w[hello] if keys.empty?
        keys.map { |name| [name, name.capitalize] }.to_h
      end

      def destination(locale = nil)
        extention = ".#{locale}" if locale
        if sidecar?
          File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component#{extention}.yml")
        else
          File.join(component_path, class_path, "#{file_name}_component#{extention}.yml")
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/view_component-2.83.0/lib/rails/generators/locale/component_generator.rb
view_component-2.83.0 lib/rails/generators/locale/component_generator.rb
view_component-3.8.0 lib/rails/generators/locale/component_generator.rb
view_component-3.7.0 lib/rails/generators/locale/component_generator.rb
view_component-3.6.0 lib/rails/generators/locale/component_generator.rb
view_component-3.5.0 lib/rails/generators/locale/component_generator.rb
view_component-3.4.0 lib/rails/generators/locale/component_generator.rb
view_component-3.3.0 lib/rails/generators/locale/component_generator.rb
view_component-3.2.0 lib/rails/generators/locale/component_generator.rb
view_component-3.1.0 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0.rc6 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0.rc5 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0.rc4 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0.rc3 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0.rc2 lib/rails/generators/locale/component_generator.rb
view_component-3.0.0.rc1 lib/rails/generators/locale/component_generator.rb
view_component-2.82.0 lib/rails/generators/locale/component_generator.rb
view_component-2.81.0 lib/rails/generators/locale/component_generator.rb
view_component-2.80.0 lib/rails/generators/locale/component_generator.rb