How to access the *current* I18n locale in a custom extension helper method

I am writing an extension that includes some helper methods for templates. However, I’m not seeing how I can get access the current locale inside this helper. (I have activate :i18n in my config.rb) Here is my simplified extension also in my config.rb:

activate :i18n, mount_at_root: false, langs: [:eng, :spa]

class MyExtension < Middleman::Extension

  def initialize(app, options_hash={}, &block)
    super
    app.send :include, ClassMethods
  end

  helpers do
    def some_helper_that_uses_current_locale
      # do some things
      I18n.locale # Doesn't work. This always returns "eng" (the default language).
    end
  end
end
::Middleman::Extensions.register(:my_extension, MyExtension)

activate :my_extension

Now in template when I use the following, it always outputs “eng” even when I’m viewing pages from within the “spa” locale:

<%= some_helper_that_uses_current_locale %>

I assume I get “eng” because the config is only loading initially. I’m not sure the method to use to get current locale each time it is called. Any ideas?

Nevermind. It works, it was an issue in another method.

1 Like