Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use self instead of nil when rendering pages with Tilt (erb support) #35

Merged
merged 1 commit into from
May 21, 2016

Conversation

bogdanRada
Copy link
Contributor

@bogdanRada bogdanRada commented May 19, 2016

Hello,
I while back ago we worked on adding ERB support together. However i wanted to add I18n support for my application and i am currently using this middleware:

require 'i18n'
require 'i18n/backend/fallbacks'
require 'sinatra/support'
require 'sinatra_more'
require 'sprockets'
require 'sinatra/asset_pipeline'

module Sinatra
  module I18n
    module Helpers
      def t(*args)
        ::I18n::t(*args)
      end
    end
  end
end


class I18nMiddleware

  def initialize(app)
    @app = app
  end

  def call(env)
    root = File.dirname(File.dirname(__FILE__))
    I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
    I18n.load_path = Dir[File.join(root, 'locales', '*.yml')]
    I18n.backend.load_translations
    I18n.enforce_available_locales = true
    @turnout_page = Turnout.config.default_maintenance_page
    @turnout_page.send(:include, Sinatra::I18n::Helpers)
    @turnout_page.send(:include, Sprockets::Helpers)
    @sinatra_base.send(:include, SinatraMore::TagHelpers)
    @app.call(env)
  end



end

However in order to work Tilt needs to know about the current class that is used ( Turnout::MaintenancePage::Erb ) in order to have access to the helpers included in the class.

This works perfectly for me.

Let me know if you are ok whit this change. I could work on adding i18n support also to this gem if you think would be a nice idea. Of course would be a bit different though and with options to include custom helpers and to use the locale from browser. But wanted just to ask this question too.

@adamcrown
Copy link
Collaborator

Looks good thanks.

I'd be happy to merge in i18n support as long is it didn't complicate things too much or add any heavy dependencies. I'd also like to make sure it can adapt to the apps existing way of detecting i18n (domain, path, cookie, etc.). But if that's a bit too complicated I'd be okay with just supporting the Accept-Language header for now.

@adamcrown adamcrown merged commit fdc892f into biola:master May 21, 2016
@adamcrown
Copy link
Collaborator

Just released version 2.3.1 with this change included.

@bogdanRada
Copy link
Contributor Author

Awesome :) thank you very much. I'll work on i18n support and will open a pull request for that soon. Thanks for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants