So following is what I would like to do, be able to access I18n locale as a constant.For example:
if (I18n.locale == I18n.locales.en)
puts "You are using viewing page in english"
end
Is there a way to access these constants (I18n.locales.en is just example to clarify)? I can always write
if (I18n.locale.to_s == "en")
but I would like to avoid that. Since code is less readable with that approach.
You could shorten your second statement to be
if (I18n.locale == :en)rather than converting to a string, however that’s still missing the point of using Rails’ locale support. For example, with this yml file:You should just be able to do this without any conditional statements:
and it would give you the appropriate translation.
If you really wanted to be able to do something like your first example, then you could override
method_missingon theSymbolclass. If you did this:Then you could do this: