Class: Trek::Generators::Install::LocalesGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Trek::Generators::Install::LocalesGenerator
- 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_files ⇒ Object
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 " \#{locale}:\n application_name: \#{application_name.capitalize}\n YAML\n end\n end\nend\n" |
#create_locales_initializer ⇒ Object
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 " # Whitelist locales available for the application\n I18n.available_locales = [:\#{@locales.join(\", :\")}]\n\n # Set default locale to something other than :en\n I18n.default_locale = :\#{@locales.first}\n RUBY\n end\nend\n" |
#set_locales ⇒ Object
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 = [: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 |