Sha256: bf9dad9dc733792add72f20a72a518b1dfaad6cdc20739babc09507ed2028ebe

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'rails'
require 'alchemy/i18n'

module AlchemyI18n
  module Generators
    class InstallGenerator < ::Rails::Generators::Base
      desc "Installs Alchemy locales into your App."

      def self.description
        locales = Alchemy::I18n.available_locales.to_sentence
        "Available locales are #{locales}"
      end

      class_option :locales,
        type: :array,
        default: [],
        desc: "Locales to generate files for. #{description}"

      source_root AlchemyI18n::Engine.root

      def append_assets
        additional_locales.each do |locale|
          append_file 'vendor/assets/javascripts/alchemy/admin/all.js', <<~ASSETS
            //= require alchemy_i18n/#{locale}
            //= require select2_locale_#{locale}
          ASSETS
        end
      end

      def append_manifest
        additional_locales.each do |locale|
          append_file 'app/assets/config/manifest.js', <<~MANIFEST
            //= link tinymce/langs/#{locale}.js
          MANIFEST
        end
      end

      def add_rails_i18n
        environment do
          "config.i18n.available_locales = #{locales.inspect}"
        end
      end

      def add_russian_gem
        if locales.include?('ru')
          gem 'russian', '~> 0.6'
        end
      end

      private

      def additional_locales
        @_additional_locales ||= locales.reject { |locale| locale == :en }
      end

      def locales
        @_locales ||= begin
          options[:locales].presence || ask_locales.split(" ")
        end.map(&:to_sym)
      end

      def ask_locales
        ask <<~LOCALES
          Which locales should we generate files for?
          #{self.class.description}
          (seperate multiple locales with space):
        LOCALES
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
alchemy_i18n-4.2.2 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.2.1 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.2.0 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.1.3 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.1.2 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.1.1 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.1.0 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.0.2 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.0.1 lib/generators/alchemy_i18n/install/install_generator.rb
alchemy_i18n-4.0.0 lib/generators/alchemy_i18n/install/install_generator.rb