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

Instance Method Summary collapse

Instance Method Details

#ask_localesObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/generators/trek/install/locales_generator.rb', line 15

def ask_locales
  loop do
    @locales = ENV["TREK_LOCALES"] || ask("What locales do you want to use? (e.g. en, fr)")

    break if valid_locales?(@locales)

    Rails.logger.debug "Invalid locales entered."
  end

  @locales = @locales.split(",").map(&:strip)
end

#create_locales_filesObject



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

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/trek/install/locales_generator.rb', line 27

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