Module: I18nRouting::Mapper
- Defined in:
- lib/i18n_routing_rails3.rb
Class Method Summary collapse
-
.included(mod) ⇒ Object
Alias methods in order to handle i18n routes.
Instance Method Summary collapse
- #create_globalized_resources(type, *resources, &block) ⇒ Object
-
#initialize(*args) ⇒ Object
On Routing::Mapper initialization (when doing Application.routes.draw do …) prepare routing system to be i18n ready.
-
#localized(locales = I18n.available_locales, opts = {}) ⇒ Object
Rails 3 routing system Create a block for localized routes, in your routes.rb :.
-
#localized_branch(locale) ⇒ Object
Create a branch for create routes in the specified locale.
- #match(*args) ⇒ Object
- #resource_with_i18n_routing(*resources, &block) ⇒ Object
- #resources_with_i18n_routing(*resources, &block) ⇒ Object
-
#skip_localization ⇒ Object
Set we do not want to localize next resource.
Class Method Details
.included(mod) ⇒ Object
Alias methods in order to handle i18n routes
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/i18n_routing_rails3.rb', line 242 def self.included(mod) mod.send :alias_method_chain, :resource, :i18n_routing mod.send :alias_method_chain, :resources, :i18n_routing # Here we redefine some methods, in order to handle # correct path_names translation on the fly [:map_method, :member, :collection].each do |m| rfname = "#{m}_without_i18n_routing".to_sym mod.send :define_method, "#{m}_with_i18n_routing".to_sym do |*args, &block| if @localized_branch and @scope[:i18n_scope_level_resource] and @scope[:i18n_real_resource_name] o = @scope[:scope_level_resource] @scope[:scope_level_resource] = @scope[:i18n_scope_level_resource] pname = @scope[:path_names] || {} i = 1 while i < args.size and (String === args[i] or Symbol === args[i]) pname[args[i]] = args[i] i += 1 end scope(:path_names => I18nRouting.path_names(@scope[:i18n_real_resource_name], {:path_names => pname})) do send(rfname, *args, &block) end @scope[:scope_level_resource] = o return end send(rfname, *args, &block) end mod.send :alias_method_chain, m, :i18n_routing end end |
Instance Method Details
#create_globalized_resources(type, *resources, &block) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/i18n_routing_rails3.rb', line 211 def create_globalized_resources(type, *resources, &block) #puts "#{' ' * nested_deep}Call #{type} : #{resources.inspect} (#{@locales.inspect}) (#{@localized_branch}) (#{@skip_localization})" @scope[:nested_deep] ||= [] @scope[:nested_deep] << 1 cur_scope = nil if @locales localized = localized_resources(type, *resources, &block) if !@skip_localization ## We do not translate if we are in a translation branch : return if localized and nested_deep > 0 # Set the current standard resource in order to customize url helper : if !@localized_branch r = resource_from_params(type, *resources) cur_scope = (parent_resource and parent_resource.name == r.name) ? parent_resource : r end end set_localizable_route(cur_scope) do skip_localization do #puts "#{' ' * nested_deep} \\- Call original #{type} : for #{resources.inspect}}" send("#{type}_without_i18n_routing".to_sym, *resources, &block) end end @scope[:nested_deep].pop end |
#initialize(*args) ⇒ Object
On Routing::Mapper initialization (when doing Application.routes.draw do …) prepare routing system to be i18n ready
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/i18n_routing_rails3.rb', line 120 def initialize(*args) super # Add i18n_locale as valid conditions for Rack::Mount / And add also :locale, as Rails 3.0.4 removed it ... @valid_conditions = @set.instance_eval { @set }.instance_eval { @valid_conditions } [:i18n_locale, :locale].each do |k| @valid_conditions << k if !@valid_conditions.include?(k) @set.valid_conditions << k if !@set.valid_conditions.include?(k) end # Extends the current RouteSet in order to define localized helper for named routes # When calling define_url_helper, it calls define_localized_url_helper too. 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, kind, options) localized_define_url_helper(route, name, kind, options) define_localized_url_helper(route, name, kind, options) end END_EVAL @set.named_routes.extend I18nRouting::NamedRouteCollection end end |
#localized(locales = I18n.available_locales, opts = {}) ⇒ Object
Rails 3 routing system Create a block for localized routes, in your routes.rb :
localized do
resources :users
match 'about' => 'contents#about', :as => :about
end
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/i18n_routing_rails3.rb', line 153 def localized(locales = I18n.available_locales, opts = {}) # Add if not added Rails.root/config/locales/*.yml in the I18n.load_path if !@i18n_routing_path_set and defined?(Rails) and Rails.respond_to?(:root) and Rails.root I18n.load_path = (I18n.load_path << Dir[Rails.root.join('config', 'locales', '*.yml')]).flatten.uniq @i18n_routing_path_set = true end old_value = @locales @locales = locales @i18n_verbose ||= opts.delete(:verbose) yield ensure @locales = old_value end |
#localized_branch(locale) ⇒ Object
Create a branch for create routes in the specified locale
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/i18n_routing_rails3.rb', line 169 def localized_branch(locale) set_localizable_route(nil) do old = @localized_branch @localized_branch = locale localized([locale]) do yield end @localized_branch = old end end |
#match(*args) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/i18n_routing_rails3.rb', line 188 def match(*args) # Localize simple match only if there is no resource scope. if args.size == 1 and @locales and !parent_resource and args.last.is_a?(Hash) and args.first[:as] = Marshal.load(Marshal.dump(args.first)) # Dump is dirty but how to make deep cloning easily ? :/ path, to = .find { |name, value| name.is_a?(String) } .merge!(:to => to).delete(path) @locales.each do |locale| mapping = LocalizedMapping.new(locale, @set, @scope, path, ) if mapping.localizable? puts("[I18n] > localize %-10s: %40s (%s) => %s" % ['route', args.first[:as], locale, mapping.path]) if @i18n_verbose @set.add_route(*mapping.to_route) end end # Now, create the real match : return set_localizable_route(args.first[:as]) do super end end super end |
#resource_with_i18n_routing(*resources, &block) ⇒ Object
275 276 277 |
# File 'lib/i18n_routing_rails3.rb', line 275 def resource_with_i18n_routing(*resources, &block) create_globalized_resources(:resource, *resources, &block) end |
#resources_with_i18n_routing(*resources, &block) ⇒ Object
279 280 281 |
# File 'lib/i18n_routing_rails3.rb', line 279 def resources_with_i18n_routing(*resources, &block) create_globalized_resources(:resources, *resources, &block) end |
#skip_localization ⇒ Object
Set we do not want to localize next resource
181 182 183 184 185 186 |
# File 'lib/i18n_routing_rails3.rb', line 181 def skip_localization old = @skip_localization @skip_localization = @localized_branch ? nil : true yield @skip_localization = old end |