Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

upgrade convert_path_to_localized_regexp to use the Rack::Mount route…

… lexer to extract tokens, instead of using split
  • Loading branch information...
commit 8e1e8f343557e343f7c4998f793444fe1d4613a4 1 parent 56d5f83
Erik Ogan and Thomas Shafer authored erikogan committed
View
1  lib/i18n_routable.rb
@@ -1,5 +1,6 @@
require 'rails/all'
require 'active_support/all'
+require 'rack/mount/strexp'
require 'i18n_routable/version'
require 'i18n_routable/config'
View
50 lib/i18n_routable/translation_assistant.rb
@@ -1,7 +1,8 @@
+
module I18nRoutable
module TranslationAssistant
- def tokenized_segment(segment)
+ def tokenize_segment segment
"i18n_#{segment}".gsub '-', '__'
end
@@ -11,31 +12,42 @@ def untokenize_segment segment
# /posts => ["/:i18n_posts", ["posts"]]
def convert_path_to_localized_regexp path
- return [path,[]] if path =~ %r{^/+$} # Root url
-
- new_path, segments = '', []
-
- new_path << path.split(/(\(.+?\))/).map do |component|
- if component.starts_with?("(") || component == '//'
- component
- else
- component.split("/").map do |word|
- if word.blank? || word.starts_with?(":") || word.starts_with?("*") || translated_segments(word) == [word]
- word
- else
- segments << word
- ':' + tokenized_segment(word)
- end
- end.join("/")
+ new_path, i18n_segment, segments = '', '', []
+ prefixes = Hash.new {|h,k| h[k] = ''}
+ prefixes.merge! :PARAM => ':', :GLOB => '*'
+ route_lexer = ::Rack::Mount::StrexpParser.new.tap {|s| s.scan_setup path }
+ route_separators = /[#{ActionDispatch::Routing::SEPARATORS.join ''}]/
+ begin
+ type, token = route_lexer.next_token
+ # puts "RES: %6s : %s" % [type, token]
+
+ if type == :CHAR && token !~ route_separators
+ i18n_segment << token
+ next
+ end
+
+ if i18n_segment.present?
+ # Only add it to be translated if there are translations
+ if translated_segments(i18n_segment) != [i18n_segment]
+ new_path << ":#{tokenize_segment i18n_segment}"
+ segments << i18n_segment
+ else
+ new_path << i18n_segment
+ end
end
- end.join
+
+ i18n_segment = ''
+ new_path << prefixes[type] << token if token
+ end while type.present?
[new_path, segments]
end
+
+
def add_segment_constraints constraints, segments
segments.each do |segment|
- constraint_token = tokenized_segment segment
+ constraint_token = tokenize_segment segment
constraints[constraint_token.to_sym] ||= route_constraint_for_segment(segment)
end
end
View
7 spec/i18n_routable/translation_assistant_spec.rb
@@ -5,8 +5,9 @@
context '#convert_path_to_localized_regexp' do
+
def convert path
- subject.send(:convert_path_to_localized_regexp, path)
+ subject.convert_path_to_localized_regexp path
end
it 'should not segment sections that have no translations' do
@@ -24,8 +25,8 @@ def convert path
end
it 'should respect optional arguments within optional arguments' do
- convert("first/second(/:group(/:individual))").should eql ["/:i18n_first/:i18n_second(/:group(:/individual))", ["first", "second"]]
- convert("first/second(/group(/:group))").should eql ["/:i18n_first/:i18n_second(/:i18n_group(:/group))", ["first", "second", "group"]]
+ convert("posts/comments(/:users(/:individual))").should eql [":i18n_posts/:i18n_comments(/:users(/:individual))", ["posts", "comments"]]
+ convert("posts/comments(/users(/:users))").should eql [":i18n_posts/:i18n_comments(/:i18n_users(/:users))", ["posts", "comments", "users"]]
end
it 'should preserve order' do
Please sign in to comment.
Something went wrong with that request. Please try again.