-
Notifications
You must be signed in to change notification settings - Fork 8
/
railtie.rb
81 lines (70 loc) · 2.5 KB
/
railtie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module TextHelpers
class Railtie < Rails::Railtie
config.text_helpers = ActiveSupport::OrderedOptions.new
config.text_helpers.raise_on_missing_translations = Rails.env.test? || Rails.env.development?
initializer "text_helpers.action_view.extend_base" do
ActionView::Base.class_eval do
include TextHelpers::Translation
# Public: Derive a translation scope from the current view template.
#
# Determines an I18n-friendly scope for the current view file when possible,
# or falls back to "views.<controller>.<action>"
#
# Returns a String.
def translation_scope
matcher = /(?<path>.*)\/_?(?<view>[^\/.]+)(?<extension>\.html\.haml)?/
info = matcher.match(@virtual_path)
if info
path = info[:path].gsub('/', '.')
"views.#{path}.#{info[:view]}"
else
"views.#{params[:controller]}.#{params[:action]}"
end
end
end
end
initializer "text_helpers.action_mailer.extend_base" do
ActionMailer::Base.class_eval do
include TextHelpers::Translation
# Public: Provides a scope for I18n lookups.
#
# Should look like `mailers.<mailer>.<action>`
#
# Returns a String.
def translation_scope
"mailers.#{mailer_name.tr("/", ".").sub("_mailer", "")}.#{action_name}"
end
end
end
initializer "text_helpers.action_controller.extend_base" do
ActionController::Base.class_eval do
include TextHelpers::Translation
# Public: Provides a scope for I18n lookups.
#
# Should look like `controllers.<controller_name>`.
#
# Returns a String.
def translation_scope
"controllers.#{params[:controller]}"
end
end
end
initializer "text_helpers.i18n.add_load_paths" do |app|
locales = Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s
app.config.i18n.load_path += Dir[locales]
end
initializer "text_helpers.setup_exception_handling", after: 'after_initialize' do
next unless config.text_helpers.raise_on_missing_translations
if Rails::VERSION::MAJOR >= 7
if config.respond_to?(:i18n)
config.i18n.raise_on_missing_translations = true
end
else
if config.respond_to?(:action_view)
config.action_view.raise_on_missing_translations = true
end
end
TextHelpers.install_i18n_exception_handler
end
end
end