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

Fix: keep mandatory format in translatated routes #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/rails-translate-routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This class knows nothing about Rails.root or Rails.application.routes,
# and therefore is easier to test without a Rails app.
class RailsTranslateRoutes
TRANSLATABLE_SEGMENT = /^([\w-]+)(\()?/.freeze
TRANSLATABLE_SEGMENT = /^([\w-]+)(\.?:[\w-]+)?(\()?/.freeze
LOCALE_PARAM_KEY = :locale.freeze
ROUTE_HELPER_CONTAINER = [
ActionController::Base,
Expand Down Expand Up @@ -339,16 +339,19 @@ def translate_path path, locale
end

# Tries to translate a single path segment. If the path segment
# contains sth. like an optional format "people(.:format)", only
# contains a dynamic part like "people.:format" or "people(.:format)", only
# "people" will be translated, if there is no translation, the path
# segment is blank or begins with a ":" (param key), the segment
# is returned untouched
def translate_path_segment segment, locale
return segment if segment.blank? or segment.starts_with?(":")

match = TRANSLATABLE_SEGMENT.match(segment)[1] rescue nil
match = TRANSLATABLE_SEGMENT.match(segment) rescue nil

(translate_string(match, locale) || segment)
translated_part = (match && match[1]) ? translate_string(match[1], locale) : nil
translated_segment = translated_part ? translated_part + (match[2] || '') : nil

translated_segment || segment
end

def translate_string str, locale
Expand Down