Class: Trek::Generators::Install::LocalesGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration, Helpers
Defined in:
lib/generators/trek/install/locales_generator.rb

Constant Summary collapse

DEFAULT_LOCALES =
"en, fr".freeze

Instance Method Summary collapse

Instance Method Details

#create_locales_filesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/trek/install/locales_generator.rb', line 48

def create_locales_files
  @locales.each do |locale|
    create_file "config/locales/#{locale}.yml" do
      <<~YAML
        #{locale}:
          application_name: #{application_name.capitalize}
      YAML
    end
  end
end

#create_locales_initializerObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/trek/install/locales_generator.rb', line 36

def create_locales_initializer
  create_file "config/initializers/locales.rb" do
    <<~RUBY
      # Whitelist locales available for the application
      I18n.available_locales = [:#{@locales.join(", :")}]

      # Set default locale to something other than :en
      I18n.default_locale = :#{@locales.first}
    RUBY
  end
end

#set_localesObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/trek/install/locales_generator.rb', line 20

def set_locales
  # `.presence` so a blank option/env (e.g. an empty answer) falls back
  # to the default instead of producing an empty, invalid initializer.
  raw = options[:locales].presence || ENV["TREK_LOCALES"].presence || DEFAULT_LOCALES
  @locales = raw.split(",").map(&:strip).reject(&:blank?)

  invalid = @locales.reject { |locale| valid_locales?(locale) }
  if invalid.any?
    say "Ignoring unavailable locales: #{invalid.join(", ")}", :yellow
    @locales -= invalid
  end

  # Never leave the list empty — that would generate `[:]` / `:`.
  @locales = ["en"] if @locales.empty?
end