Skip to content

Instantly share code, notes, and snippets.

@diegok
Last active January 2, 2016 02:39
Show Gist options
  • Save diegok/8238365 to your computer and use it in GitHub Desktop.
Save diegok/8238365 to your computer and use it in GitHub Desktop.
# Reimplement "url_for" helper
my $mojo_url_for = *Mojolicious::Controller::url_for{CODE};
my $i18n_url_for = sub {
my $self = shift;
my $url = $self->$mojo_url_for(@_);
# Absolute URL
return $url if $url->is_abs;
# discard target if any
shift if @_ && (!(@_ % 2) || !ref $_[0]);
# unwrap params
my %params = @_ == 1 ? %{$_[-1]} : @_;
# Detect lang
if (my $lang = $params{lang} || $self->stash('lang')) {
my $path = $url->path || [];
# Root
if (!$path->[0]) {
$path->parts([ $lang ]);
}
# No language detected
elsif ( ref $langs ne 'ARRAY' or not scalar grep { $path->contains("/$_") } @$langs ) {
unshift @{ $path->parts }, $lang;
}
}
return $url;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment