Skip to content

Instantly share code, notes, and snippets.

@mpxc8102
Last active August 29, 2015 14:19
Show Gist options
  • Save mpxc8102/9b92a411610fb9061fab to your computer and use it in GitHub Desktop.
Save mpxc8102/9b92a411610fb9061fab to your computer and use it in GitHub Desktop.
patching route_translator
# copy and paste these methods over the ones that are defined in route_translator-4.0.0/lib/route_translator/translator.rb
def self.translations_for(app, conditions, requirements, defaults, route_name, anchor, route_set, &block)
add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes)
available_locales.each do |locale|
new_conditions = conditions.dup
# LOOK HERE!!!!!!!!!!!
# this begin block is the only change i made to this method
# HELLO!!!!!!!!!!
# basically what we're doing is if a translation isn't available for the route, skip on creating it
begin
new_conditions[:path_info] = translate_path(conditions[:path_info], locale)
rescue I18n::MissingTranslationData
next
end
new_conditions[:parsed_path_info] = ActionDispatch::Journey::Parser.new.parse(new_conditions[:path_info]) if conditions[:parsed_path_info]
if new_conditions[:required_defaults] && !new_conditions[:required_defaults].include?(RouteTranslator.locale_param_key)
new_conditions[:required_defaults] << RouteTranslator.locale_param_key if new_conditions[:required_defaults]
end
new_defaults = defaults.merge(RouteTranslator.locale_param_key => locale.to_s.gsub('native_', ''))
new_requirements = requirements.merge(RouteTranslator.locale_param_key => locale.to_s)
new_route_name = translate_name(route_name, locale)
new_route_name = nil if new_route_name && route_set.named_routes.routes[new_route_name.to_sym] #TODO: Investigate this :(
block.call(app, new_conditions, new_requirements, new_defaults, new_route_name, anchor)
end
if RouteTranslator.config.generate_unnamed_unlocalized_routes
block.call(app, conditions, requirements, defaults, nil, anchor)
elsif RouteTranslator.config.generate_unlocalized_routes
block.call(app, conditions, requirements, defaults, route_name, anchor)
end
end
def self.translate_string(str, locale)
locale = "#{locale}".gsub('native_', '')
opts = {scope: :routes, locale: locale}
# if the locale is en, then if a translation isn't available, default back on the string we want to translate
# so we might be trying to translate /facebook for EN
# of course we don't want to have to define EN route translations in routes.yml
# so we return back /facebook
# BUT!! and this is the genius part
# if a translation isn't available for ES
# then a missing translation error will be raised
opts.merge!({default: str}) if locale == "en"
res = I18n.translate(str, opts)
URI.escape(res)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment