Class: RailsTemplate18f::Generators::I18nGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Base
Defined in:
lib/generators/rails_template18f/i18n/i18n_generator.rb

Instance Method Summary collapse

Instance Method Details

#configure_i18nObject



48
49
50
51
52
53
54
55
56
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 48

def configure_i18n
  application "config.i18n.fallbacks = [:en]"
  available_regex = /^(\s*config.i18n.available_locales).*$/
  if file_content("config/application.rb").match?(available_regex)
    gsub_file "config/application.rb", available_regex, "\\1 = #{supported_languages}"
  else
    application "config.i18n.available_locales = #{supported_languages}"
  end
end

#install_around_actionObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 70

def install_around_action
  return if languages.empty?
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController", indent(<<~EOM)
    around_action :switch_locale

    def switch_locale(&action)
      locale = params[:locale] || I18n.default_locale
      I18n.with_locale(locale, &action)
    end
  EOM
end

#install_gemObject



18
19
20
21
22
23
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 18

def install_gem
  return if gem_installed?("i18n-tasks")
  gem_group :development, :test do
    gem "i18n-tasks", "~> 1.0"
  end
end

#install_helper_tasksObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 25

def install_helper_tasks
  bundle_install do
    run "cp $(i18n-tasks gem-path)/templates/config/i18n-tasks.yml config/"
    run "cp $(i18n-tasks gem-path)/templates/rspec/i18n_spec.rb spec/"
  end
  insert_into_file "config/i18n-tasks.yml", "\n#{indent("- app/assets/builds", 4)}", after: "exclude:"
  uncomment_lines "config/i18n-tasks.yml", "ignore_missing:"
  insert_into_file "config/i18n-tasks.yml", indent(<<~EOM), after: "ignore_missing:\n"
    - 'shared.languages.*'
    - 'shared.header.{title,close,demo_banner,menu}'
  EOM
end

#install_nav_helperObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 58

def install_nav_helper
  inject_into_module "app/helpers/application_helper.rb", "ApplicationHelper", indent(<<~'EOH')
    def active_locale?(locale_string)
      locale_string.to_sym == I18n.locale
    end

    def language_span(locale_string)
      content_tag :span, t("shared.languages.#{locale_string}"), lang: locale_string, "xml:lang": locale_string
    end
  EOH
end

#install_routeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 82

def install_route
  return if languages.empty?
  return if file_content("config/routes.rb").match?(/scope "\(:locale\)"/)
  regex = /(^.+\.routes\.draw do\s*$)\n(.*)^end$/m
  gsub_file "config/routes.rb", regex, <<~'EOR'
    \1
      scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
        # Your application routes go here
        \2
      end
    end
  EOR
end

#install_translationsObject



38
39
40
41
42
43
44
45
46
# File 'lib/generators/rails_template18f/i18n/i18n_generator.rb', line 38

def install_translations
  app_name # reference app_name here so the instance var is set before entering the "inside" block
  inside "config/locales" do
    template "en.yml"
    languages.each do |lang|
      copy_file "#{lang}.yml"
    end
  end
end