Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: change/i18n_routable
...
head fork: change/i18n_routable
  • 2 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 21, 2012
Erik Ogan and Thomas Shafer WIP moving translations to much lower in the stack b3a1d18
Erik Ogan and Thomas Shafer moved translations much lower in the stack a39f220
View
1  lib/i18n_routable.rb
@@ -6,3 +6,4 @@
require 'i18n_routable/mapper'
require 'i18n_routable/route_set'
require 'i18n_routable/translation_assistant'
+require 'i18n_routable/rack/mount/localizable_generatable_regexp'
View
6 lib/i18n_routable/config.rb
@@ -33,9 +33,15 @@ def display_locales
end
def convert_to_display_locale locale
+ return nil unless locale.present?
I18nRoutable.localize_config[:backend_to_display_locales][locale.to_sym]
end
+ def convert_to_backend_locale locale
+ return nil unless locale.present?
+ I18nRoutable.localize_config[:display_to_backend_locales][locale.to_sym]
+ end
+
def routes_for_locale locale
self.routes_for_locale_cache[locale] ||= begin
routes = I18n.translate('routes', :locale => locale)
View
17 lib/i18n_routable/mapper/localizable_scope.rb
@@ -24,6 +24,7 @@ def localize! options={}
I18nRoutable.localize_config = options.symbolize_keys.reverse_merge(default_localize_options)
validate_options!
setup_convert_to_display_locale!
+ setup_convert_to_backend_locale!
end
private
@@ -38,11 +39,25 @@ def default_localize_options
def setup_convert_to_display_locale!
I18nRoutable.localize_config[:backend_to_display_locales] = I18nRoutable.localize_config[:locales].inject({}) do |hsh, locale|
if locale.is_a?(Hash)
- hsh.merge(locale.invert)
+ hsh.merge!(locale.invert)
+ hsh.merge(locale.keys.first => locale.keys.first)
else
hsh.update(locale => locale)
end
end
+ I18nRoutable.localize_config[:backend_to_display_locales].merge! I18n.default_locale => I18n.default_locale
+ end
+
+ def setup_convert_to_backend_locale!
+ I18nRoutable.localize_config[:display_to_backend_locales] = I18nRoutable.localize_config[:locales].inject({}) do |hsh, locale|
+ if locale.is_a?(Hash)
+ hsh.merge!(locale)
+ hsh.merge(locale.values.first => locale.values.first)
+ else
+ hsh.update(locale => locale)
+ end
+ end
+ I18nRoutable.localize_config[:display_to_backend_locales].merge! I18n.default_locale => I18n.default_locale
end
def validate_options!
View
54 lib/i18n_routable/rack/mount/localizable_generatable_regexp.rb
@@ -0,0 +1,54 @@
+module I18nRoutable
+ module Rack
+ module Mount
+ module LocalizableGeneratableRegexp
+
+
+ # this method injects translations into the params based on required_params
+ # that start with i18n_
+ # :i18n_posts => 'puestos'
+ def inject_i18n_translations(params)
+
+ required_params_to_check = required_params - params.keys
+ return unless required_params_to_check
+
+ required_params_to_check.each do |required_param|
+ if required_param.to_s =~ /i18n_(.+)/
+ params[required_param] ||= I18nRoutable.translate_segment($1, params[:locale])
+ end
+ end
+
+ end
+
+ def generate_with_localize(params = {}, recall = {}, options = {})
+ # set the locale to the params or current
+ params[:locale] ||= I18nRoutable.convert_to_backend_locale(params[:locale]) || I18n.locale
+ # reject the locale unless we support that locale
+ params[:locale] = I18n.default_locale unless I18nRoutable.backend_locales.include?(params[:locale].to_sym)
+
+ merged = recall.merge(params)
+ should_inject_translations = required_defaults.present? && required_defaults.all? { |k, v| merged[k] == v }
+ inject_i18n_translations(params) if should_inject_translations
+
+ # delete the locale if it's the default (no locale in scope)
+ if params[:locale].to_s == I18n.default_locale.to_s
+ params.delete :locale
+ else
+ # reset the locale to the display version
+ params[:locale] = I18nRoutable.convert_to_display_locale(params[:locale])
+ end
+
+ # call super
+ generate_without_localize(params, recall, options)
+ end
+
+ def self.included base
+ base.send :alias_method_chain, :generate, :localize
+ end
+ end
+
+ end
+ end
+end
+
+Rack::Mount::GeneratableRegexp::InstanceMethods.send :include, I18nRoutable::Rack::Mount::LocalizableGeneratableRegexp
View
2  lib/i18n_routable/route_set.rb
@@ -1,5 +1,3 @@
require 'i18n_routable/route_set/reverse_route_lookup'
-require 'i18n_routable/route_set/localizable_url_helper'
ActionDispatch::Routing::RouteSet.send :include, I18nRoutable::RouteSet::ReverseRouteLookup
-ActionDispatch::Routing::RouteSet::NamedRouteCollection.send :include, I18nRoutable::RouteSet::LocalizableUrlHelper
View
49 lib/i18n_routable/route_set/localizable_url_helper.rb
@@ -1,49 +0,0 @@
-module I18nRoutable
- module RouteSet
- module LocalizableUrlHelper
-
- def define_hash_access_with_localize(route, name, kind, options)
- selector = hash_access_name(name, kind)
-
- # Rails.application.routes.named_routes[:edit_petition]
- i18n_segments = route.segment_keys.find_all{|k| k.to_s =~ /^i18n/}.inject({}){|hash, segment|
- hash.update segment => segment.to_s.match(/^i18n_(.*$)/)[1]
- }
-
- # We use module_eval to avoid leaks
- @module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
- def #{selector}(options = {}) # def hash_for_users_url(options = nil)
- route = _routes.named_routes[#{options[:use_route].inspect}]
-
- base_options = #{options.inspect}
-
- options[:locale] ||= I18n.locale
-
- #{i18n_segments.inspect}.each_pair do |i18n_segment, segment|
- base_options[i18n_segment] ||= begin
- I18nRoutable.translate_segment(segment, options[:locale])
- end
- end
-
- if options[:locale] == I18n.default_locale
- options.delete(:locale)
- else
- options[:locale] = I18nRoutable.convert_to_display_locale(options[:locale])
- end
-
- options.reverse_merge!(base_options) # options ? {:only_path=>false}.merge(options) : {:only_path=>false}
-
- options
- end # end
- protected :#{selector} # protected :hash_for_users_url
- RUBY
- helpers << selector
- end
-
- def self.included(base)
- base.alias_method_chain :define_hash_access, :localize
- end
-
- end
- end
-end
View
4 lib/i18n_routable/route_set/reverse_route_lookup.rb
@@ -8,10 +8,10 @@ module ReverseRouteLookup
# but in different locales
def base_named_route_for path, environment = {}
method = (environment[:method] || "GET").to_s.upcase
- path = Rack::Mount::Utils.normalize_path(path) unless path =~ %r{://}
+ path = ::Rack::Mount::Utils.normalize_path(path) unless path =~ %r{://}
begin
- env = Rack::MockRequest.env_for(path, {:method => method})
+ env = ::Rack::MockRequest.env_for(path, {:method => method})
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end
View
4 spec/controllers/test_controller_spec.rb
@@ -9,7 +9,7 @@ def foo
def use_url_for_with_implicit_params
# url_for should infer the controller from the current controller
- render :text => url_for(:action => 'foo', :only_path => true, :i18n_test => 'prueba', :locale => 'es')
+ render :text => url_for(:action => 'foo', :only_path => true, :locale => 'es')
end
@@ -41,7 +41,7 @@ def use_url_for_with_implicit_params
controller.send(:test_path).should eql '/test'
# path parameters should be set properly when doing this
- get :use_url_for_with_implicit_params, :i18n_testing_url_for => 'testing_url_for'
+ get :use_url_for_with_implicit_params
response.body.should == '/es/prueba'
end
View
10 spec/i18n_routable/i18n_routable_spec.rb
@@ -22,10 +22,12 @@ def base_named_route_for(options)
context 'validating config options' do
before do
@old_conversions_from_backend_to_display_locales = I18nRoutable.localize_config[:backend_to_display_locales]
+ @old_conversions_from_display_to_backend_locales = I18nRoutable.localize_config[:display_to_backend_locales]
end
after do
I18nRoutable.localize_config[:backend_to_display_locales] = @old_conversions_from_backend_to_display_locales
+ I18nRoutable.localize_config[:display_to_backend_locales] = @old_conversions_from_display_to_backend_locales
end
it 'should not support a symbol for :locales, it must take an array' do
@@ -203,23 +205,23 @@ def base_named_route_for(options)
it 'should return a localized route when using hash_for helpers passing in locale' do
I18n.locale = 'fr'
route_hash = hash_for_posts_url(:locale => 'es')
- route_hash.should eql :locale=>:es, :action=>"index", :controller=>"posts", :use_route=>"posts", :only_path=>false, :i18n_posts=>"puestos"
+ route_hash.should eql :locale=>'es', :action=>"index", :controller=>"posts", :use_route=>"posts", :only_path=>false
url_for(route_hash).should eql "http://www.example.com/es/puestos"
route_hash = hash_for_posts_url(:locale => :es)
- route_hash.should eql :locale=>:es, :action=>"index", :controller=>"posts", :use_route=>"posts", :only_path=>false, :i18n_posts=>"puestos"
+ route_hash.should eql :locale=>:es, :action=>"index", :controller=>"posts", :use_route=>"posts", :only_path=>false
url_for(route_hash).should eql "http://www.example.com/es/puestos"
end
it 'should respect I18n.locale' do
route_hash = hash_for_post_url(:id => 1)
- route_hash.should eql :id=>1, :action=>"show", :controller=>"posts", :use_route=>"post", :only_path=>false, :i18n_posts=>"posts"
+ route_hash.should eql :id=>1, :action=>"show", :controller=>"posts", :use_route=>"post", :only_path=>false
url_for(route_hash).should eql "http://www.example.com/posts/1"
I18n.locale = 'es'
route_hash = hash_for_post_url(:id => 1)
- route_hash.should eql :id=>1, :locale=>:es, :action=>"show", :controller=>"posts", :use_route=>"post", :only_path=>false, :i18n_posts=>"puestos"
+ route_hash.should eql :id=>1, :action=>"show", :controller=>"posts", :use_route=>"post", :only_path=>false
url_for(route_hash).should eql "http://www.example.com/es/puestos/1"
end

No commit comments for this range

Something went wrong with that request. Please try again.