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

Rails 4.2 compatibility: parameters of LocalizedMapping initializer super(...) have changed #47

Open
jensb opened this issue Jan 18, 2015 · 7 comments

Comments

@jensb
Copy link

jensb commented Jan 18, 2015

When trying out this gem in a new Rails 4.2 app, I get

$ rake routes
rake aborted!
ArgumentError: wrong number of arguments (6 for 3)
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:134:in `define_url_helper'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:289:in `block (2 levels) in included'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:227:in `match'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:289:in `block (2 levels) in included'
$APPPATH/config/routes.rb:6:in `block (4 levels) in <top (required)>'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:254:in `block (2 levels) in create_globalized_resources'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:183:in `skip_localization'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:252:in `block in create_globalized_resources'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:99:in `set_localizable_route'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:251:in `create_globalized_resources'
.../ruby/2.1.0/i18n_routing-16908a366e4f/lib/i18n_routing_rails4.rb:301:in `resources_with_i18n_routing'
$APPPATH/config/routes.rb:5:in `block (3 levels) in <top (required)>'
$APPPATH/config/routes.rb:4:in `block (2 levels) in <top (required)>'
$APPPATH/config/routes.rb:3:in `block in <top (required)>'
$APPPATH/config/routes.rb:1:in `<top (required)>'
$APPPATH/config/environment.rb:5:in `<top (required)>'

Any idea what this might be?

I'm using the rails4 branch of 'ncri' (which unfortunately doesn't have an issue tracker, so I post it here).

@jensb
Copy link
Author

jensb commented Jan 19, 2015

This patch seems to work:

diff --git a/lib/i18n_routing_rails4.rb b/lib/i18n_routing_rails4.rb
index 74f7cb4..c836ee0 100644
--- a/lib/i18n_routing_rails4.rb
+++ b/lib/i18n_routing_rails4.rb
@@ -131,8 +131,8 @@ module I18nRouting
       if !@set.named_routes.respond_to?(:define_localized_url_helper)
         @set.named_routes.class_eval <<-END_EVAL, __FILE__, __LINE__ + 1
           alias_method :localized_define_url_helper, :define_url_helper
-          def define_url_helper(route, name, options)
-            localized_define_url_helper(route, name, options)
+          def define_url_helper(route, name, options, components, route_name, route_set)
+            localized_define_url_helper(route, name, options, components, route_name, route_set)
             define_localized_url_helper(route, name, options)
           end
         END_EVAL

My simple example works (output 'rake routes').

Unfortunately, then all tests fail due to other errors, like 'match' being deprecated for 'get'. I'll try to fix those and then report back here.

@jensb jensb closed this as completed Jan 19, 2015
@jensb jensb reopened this Jan 19, 2015
@jensb
Copy link
Author

jensb commented Jan 20, 2015

I tried, but I got lost because the parameter naming (and numbering, and, it seems contents) of

   class LocalizedMapping < ActionDispatch::Routing::Mapper::Mapping

have changed a lot. See for example rails/rails@3b908cb. Can anybody have a look? Thanks!

@jensb jensb changed the title Rails 4.2 compatibility: ArgumentError in define_url_helper Rails 4.2 compatibility: parameters of LocalizedMapping initializer super(...) have changed Jan 20, 2015
@thomasdarde
Copy link

I now use this gem
https://github.com/enriclluelles/route_translator

@ncri
Copy link

ncri commented Feb 17, 2015

I actually got my fork working with rails 4.2 (well, at least resource routes, but normal named routes were not working anymore since rails 4 anyway). We look up translations a bit differently in our branch, but if anyone is interested i can let you know what I patched.

@jensb
Copy link
Author

jensb commented Jun 24, 2015

I only need normal named routes to be translated. But anyway, maybe I can get it running. What did you patch? Thanks :)

@jensb
Copy link
Author

jensb commented Jul 30, 2015

Any info would be appreciated!

@ncri
Copy link

ncri commented Aug 3, 2015

For named routes we have this in an initializer:

# ---------------------------------------
# Helpers for creating translated routes
# ---------------------------------------

module RouteTranslation

  ROUTE_HELPER_CONTAINER = [
    ActionController::Base,
    ActionView::Base,
    ActionMailer::Base,
    ActionDispatch::Routing::UrlFor,
    ActionController::TestCase,
    Rails.application.routes.named_routes.url_helpers_module
  ].freeze

  def translated_named_route route_name, to, params = {}
    available_locales = ['en', 'de', 'fr']    # use you available locales here
    available_locales.each do |locale|
      locale = locale.to_sym
      path = "#{locale}/#{yml_file_route_lookup(locale, route_name)}"
      as = "#{route_name}#{"_#{locale.to_s.downcase.sub('-','_')}"}"
      get_params = { path => to, as: as, locale: locale }.merge(params)
      get get_params
    end
    RouteTranslation.add_untranslated_helpers_to_controllers_and_views(route_name)
  end

  private

  # Taken (and slightly modified) from translate_routes gem: https://github.com/raul/translate_routes
  #
  # Add standard route helpers for default locale e.g.
  #   I18n.locale = :de
  #   people_path -> people_de_path
  #   I18n.locale = :fr
  #   people_path -> people_fr_path
  #
  def self.add_untranslated_helpers_to_controllers_and_views old_name
    ['path', 'url'].map do |suffix|
      new_helper_name = "#{old_name}_#{suffix}"

      ROUTE_HELPER_CONTAINER.each do |helper_container|
        helper_container.send :define_method, new_helper_name do |*args|
          send "#{old_name}_#{I18n.locale.to_s.downcase.sub('-','_')}_#{suffix}", *args
        end
      end
      # puts "Added #{new_helper_name} route helper."
      new_helper_name.to_sym
    end
  end

end

yml_file_route_lookup(locale, route_name) simply looks up the route in a yaml file for the given locale. For example yml_file_route_lookup('de', 'my_profile')could yield "mein_profil".

We then use translated_named_route in routes.rb like this:

translated_named_route 'my_profile', 'user_profiles#show'

Works well.

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

No branches or pull requests

3 participants