Skip to content
  • Carlos Antonio da Silva's avatar
    4d18d813
    Respect locale set by controller in the failure app (#5567) · 4d18d813
    Carlos Antonio da Silva authored
    A common usage of I18n with different locales is to create some around
    callback in the application controller that sets the locale for the
    entire action, via params/url/user/etc., which ensure the locale is
    respected for the duration of that action, and resets at the end.
    
    Devise was not respecting the locale when the authenticate failed and
    triggered the failure app, because that happens in a warden middleware
    right up in the change, by that time the controller around callback had
    already reset the locale back to its default, and the failure app would
    just translate flash messages using the default locale.
    
    Now we are passing the current locale down to the failure app via warden
    options, and wrapping it with an around callback, which makes the
    failure app respect the set I18n locale by the controller at the time
    the authentication failure is triggered, working as expected. (much more
    like a normal controller would.)
    
    I chose to introduce a callback in the failure app so we could wrap the
    whole `respond` action processing rather than adding individual `locale`
    options to the `I18n.t` calls, because that should ensure other possible
    `I18n.t` calls from overridden failure apps would respect the set locale
    as well, and makes it more like one would implement in a controller. I
    don't recommend people using callbacks in their own failure apps though,
    as this is not going to be documented as a "feature" of failures apps,
    it's considered "internal" and could be refactored at any point.
    
    It is possible to override the locale with the new `i18n_locale` method,
    which simply defaults to the passed locale from the controller.
    
    Closes #5247
    Closes #5246
    
    Related to: #3052, #4823, and possible others already closed.
    Related to warden: (may be closed there afterwards)
    https://github.com/wardencommunity/warden/issues/180
    https://github.com/wardencommunity/warden/issues/170
    4d18d813
    Respect locale set by controller in the failure app (#5567)
    Carlos Antonio da Silva authored
    A common usage of I18n with different locales is to create some around
    callback in the application controller that sets the locale for the
    entire action, via params/url/user/etc., which ensure the locale is
    respected for the duration of that action, and resets at the end.
    
    Devise was not respecting the locale when the authenticate failed and
    triggered the failure app, because that happens in a warden middleware
    right up in the change, by that time the controller around callback had
    already reset the locale back to its default, and the failure app would
    just translate flash messages using the default locale.
    
    Now we are passing the current locale down to the failure app via warden
    options, and wrapping it with an around callback, which makes the
    failure app respect the set I18n locale by the controller at the time
    the authentication failure is triggered, working as expected. (much more
    like a normal controller would.)
    
    I chose to introduce a callback in the failure app so we could wrap the
    whole `respond` action processing rather than adding individual `locale`
    options to the `I18n.t` calls, because that should ensure other possible
    `I18n.t` calls from overridden failure apps would respect the set locale
    as well, and makes it more like one would implement in a controller. I
    don't recommend people using callbacks in their own failure apps though,
    as this is not going to be documented as a "feature" of failures apps,
    it's considered "internal" and could be refactored at any point.
    
    It is possible to override the locale with the new `i18n_locale` method,
    which simply defaults to the passed locale from the controller.
    
    Closes #5247
    Closes #5246
    
    Related to: #3052, #4823, and possible others already closed.
    Related to warden: (may be closed there afterwards)
    https://github.com/wardencommunity/warden/issues/180
    https://github.com/wardencommunity/warden/issues/170
Loading