We use custom classes (value objects) to represent application-level locales:
# frozen_string_literal: true
class Locale
attr_reader :code
delegate :to_sym, to: :code
def self.[](code)
new(code: code)
end
def initialize(code:)
@code = code.to_sym
end
...
end
And pass it to I18n: I18n.t(..., locale: Locale[:en]).
While this approach is quite handy (there are, actually, additional Locale attributes; it can be integrated to ActiveRecord and ActiveModel and what not), it's a bit flaky (it depends on MRI implementation details) -- it works on 2.7.4 and does not on 3.0.0.
Seems like the only problem is method
def enforce_available_locales!(locale)
if locale != false && config.enforce_available_locales
raise I18n::InvalidLocale.new(locale) if !locale_available?(locale)
end
end
which can be enhanced to be able to handle wrappers that respond to #to_sym, i.e.
def enforce_available_locales!(locale)
if locale != false && config.enforce_available_locales
raise I18n::InvalidLocale.new(locale) if !locale_available?(locale.to_sym)
end
end
We use custom classes (value objects) to represent application-level locales:
And pass it to I18n:
I18n.t(..., locale: Locale[:en])
.While this approach is quite handy (there are, actually, additional
Locale
attributes; it can be integrated toActiveRecord
andActiveModel
and what not), it's a bit flaky (it depends on MRI implementation details) -- it works on 2.7.4 and does not on 3.0.0.Seems like the only problem is method
which can be enhanced to be able to handle wrappers that respond to
#to_sym
, i.e.